Skip to content

request for a command to turn sorted sets into scheduling queues with blocking pop support #2801

Closed

Description

@halaei

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 val

with 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_score from queue if such element exists at the moment.
  • Otherwise, it blocks for at most max_wait_time seconds until the first time t1 at which there is an element with score less than or equal to max_score - t0 + t1. Then it removes and returns such an element. This event may happen because of new elements added to the queue using zadd, or just by passing the time.
  • After max_wait_time seconds if no element is ready to be popped, the command will return null.

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

  1. Blocking pop from FIFO queue is already possible using blpop command.
  2. 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, blpop on 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 for zlpop command. But here there is a need for the blocking version of it (zblpop).

Activity

  1. jesuspc commented on Feb 18, 2016

    @jesuspc

    +1

  2. grvhi commented on Jun 17, 2016

    @grvhi

    +1

  3. debraj-work commented on Jan 2, 2017

    @debraj-work

    +1

  4. manast commented on Feb 28, 2017

    @manast

    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...

  5. halaei commented on Oct 8, 2018

    @halaei
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions