Skip to content

POP from sorted sets #1861

Closed
Closed

Description

@reezer

It would be nice if one could rpop and lpop from a sorted set.

ZADD sorted 5 foo
ZADD sorted 3 bar
ZADD sorted 8 baz
ZLPOP sorted

Would return bar, probably also the score or one would split the operations to return either the score or the member.

Activity

  1. reezer commented on Jan 10, 2015

    @reezer
    Author

    Probably a zrpop would also make sense, maybe even zblpop and zbrpop, which would allow for example creating a queue of repeatedly run tasks, which are ordered and processed based off timestamps.

  2. badboy commented on Jan 10, 2015

    @badboy
    Contributor

    workaround for now:

    MULTI
    ZRANGE sorted 0 0 WITHSCORES
    ZREMRANGEBYRANK sorted 0 0
    EXEC
    

    (-1, -1 for zrpop equivalent)

  3. reezer commented on Jan 10, 2015

    @reezer
    Author

    blocking would be cool though

  4. halaei commented on Oct 8, 2015

    @halaei
    Contributor

    blocking pop from sorted set makes it easy to implement priority queues with low latency.

  5. warrickhill commented on Sep 27, 2016

    @warrickhill

    +1

  6. chrisharrison commented on Nov 2, 2016

    @chrisharrison

    +1

  7. jmb0z commented on Nov 9, 2016

    @jmb0z

    +1

  8. merkylove commented on Nov 25, 2016

    @merkylove

    +1)

  9. james727 commented on Dec 2, 2016

    @james727

    +1

  10. rubberpants commented on Feb 21, 2017

    @rubberpants

    +1 This would be very nice to have.

  11. rfyiamcool commented on Mar 30, 2017

    @rfyiamcool

    +1

  12. denengineer commented on Aug 1, 2017

    @denengineer

    +1

  13. ChefAndy commented on Sep 12, 2017

    @ChefAndy

    +1

  14. rfyiamcool commented on Oct 12, 2017

    @rfyiamcool

    +1

  15. welovelain commented on Dec 28, 2017

    @welovelain

    +1

  16. lausycampari commented on Mar 9, 2018

    @lausycampari

    I came up with a workaround, for the situation where you need to pop() from a sorted list in a transactional way to prevent races, specifically for the case when the delete command relies on the output of the first select command. So first, you use zrange or something else to get the element that is to be 'popped', after that returns the response, assign a reference to it in memory, then use zrem to delete the element by its value (since we now have a reference to it), and now here's the trick: If zrem returns 0, then that means we've encountered a race condition, and a different reader 'popped' the same element at almost the exact same time, but beat us to it just barely. So in this case, since we know the other reader successfully deleted the element we were about to delete, we simply do a recursive call the function we're in, to try again from the beginning. Needless to say, if zrem returned a 1, then we know that we've successfully read and deleted the same element, without a race interference, and can safely return the popped element.

  17. sknuell commented on Apr 6, 2018

    @sknuell

    +1

  18. wluo commented on Apr 25, 2018

    @wluo

    +sqrt(-1)

  19. added a commit that references this issue on Apr 29, 2018
    438125b
  20. added a commit that references this issue on Dec 17, 2018
    0424633
  21. added a commit that references this issue on Jun 28, 2023
    e3e0a66
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