Repository navigation
Description
There are widely used frameworks and libraries that use Redis to implement scheduling queues. So I think it is not a bad idea to provide a command that blocks until the time an item is ready to be popped from a scheduling queue.
How to implement an scheduling queue
In order to implemenet an scheduling queue sorted set can be used with score of elements being Unix time. Push can be done with zadd, and pop with zrangebyscore and zremrangebyrank combined in a Lua script like this:
local val = redis.call('zrangebyscore', KEYS[1], '-inf', KEYS[2], 'WITHSCORES', 'LIMIT' ,0, KEYS[3])
if(next(val) ~= nil) then
redis.call('zremrangebyrank', KEYS[1], 0, #val / 2 - 1
end
return valwith KEYS[1] being the name of the sorted set, KEYS[2] being the current Unix time, and KEYS[3] being the maximum number of elements to be returned.
But wen the queue is empty, or when there is no element ready to be popped (with score being expired), it sounds nearly impossible to do better than sleep() in a loop. To make things better, I propose the following command.
zwatch queue [max_score max_wait_time]
Let say the command is executed at time t0. If max_score is not provided, the default will be t0. The default for max_wait_time will be +inf. The command do as followed:
- It removes and immediately returns an elements with score less than or equal to
max_scorefromqueueif such element exists at the moment. - Otherwise, it blocks for at most
max_wait_timeseconds until the first timet1at which there is an element with score less than or equal tomax_score - t0 + t1. Then it removes and returns such an element. This event may happen because of new elements added to the queue usingzadd, or just by passing the time. - After
max_wait_timeseconds if no element is ready to be popped, the command will returnnull.
Again, although this command may sounds off the scope at the first glance, and may be hard to implement, the number of popular scheduling queue implementations that use Redis, and the lack of such a feature that I think will be appreciated, should give this proposal a chance to be considered.
PS: Other types of queues with blocking pop support
- Blocking pop from FIFO queue is already possible using
blpopcommand. - A priority queue that supports blocking pop can be implemented using a sorted set and a list combined. Every time an item is pushed to the sorted set, a signaling item is pushed to the list as well. This way, in order to wait for an item from the queue,
blpopon the list comes in handy. And of course, by every item popped from the sorted set, one item should be popped from the list and vice versa. There is a related issue that request forzlpopcommand. But here there is a need for the blocking version of it (zblpop).
Activity
jesuspc commentedon Feb 18, 2016
+1
grvhi commentedon Jun 17, 2016
+1
debraj-work commentedon Jan 2, 2017
+1
manast commentedon Feb 28, 2017
In your second example for a priority queue you also need support for calling blocking commands in lua scripts, the reason is that you want to update both the list and the zset atomically in the same operation, so as soon as blpop is executed you want to remove an element from the zset in the same transaction...
halaei commentedon Oct 8, 2018
Since redis modules api is introduced, with blocking and timer APIs, I spent some time implementin something more useful and specific for the purpose I had in mind 3 years ago: laravel queue redis module
As the name suggests, it's data structure is fully compatible with laravel's original redis queue driver. It can be used with any programming language or framework though. So this can be closed now.