Reputation: 1099
I would like to user Redis as a remote timers server. What I need is a way to schedule timer from one server, and get a notification when this timer is fired on all other servers.
I have already implemented this mechanism using expired keys and keyspace notifications and it works.
The problem is that the way the EXPIRE mechanism is configured, when I'll have many timers they might not fire...(http://redis.io/commands/expire)
I was wondering if there is a way to change the 25% rule of expiring, into something else in order to make sure all timers will be triggered? I can live with 1-2 seconds delay but I need ALL of the timers to fire.
I remember seeing somewhere that this parameter is configurable, but I can't find the docs for it..
Upvotes: 3
Views: 2692
Reputation: 6371
You can`t set option to force redis to expire all keys at once, but you may set option to as close as possible to it. Please keep in mind how redis expire works. In two words:
Specifically this is what Redis does 10 times per second:
- Test 20 random keys from the set of keys with an associated expire.
- Delete all the keys found expired.
- If more than 25% of keys were expired, start again from step 1.
You may change this 10 times
behaviour with hz
option in redis config file. From the original documentation:
Redis calls an internal function to perform many background tasks, like closing connections of clients in timeout, purging expired keys that are never requested, and so forth.
Not all tasks are performed with the same frequency, but Redis checks for tasks to perform according to the specified "hz" value.
By default "hz" is set to 10. Raising the value will use more CPU when Redis is idle, but at the same time will make Redis more responsive when there are many keys expiring at the same time, and timeouts may be handled with more precision.
So you may change it 100 or even 1000. That would mean - 1000 is not less then 20,000 keys per seconds would be expired. It is important to understand that your instance of redis would consume a lot of CPU in idle state if you have much of timers.
Upvotes: 3