I'm implementing a first in first out queue system where certain players have a higher priority based on their rank.
The system I've designed at the moment uses zadd
with their score being the rank priority. That all works fine, as in it automatically orders the set by their rank priority. I then use zpopmin
to grab the first person with the lowest score. It correctly grabs the person with the lowest score but as you can guess, there's no first in first out priority. A player who joins the queue with the same priority as someone else has a random chance of getting in, instead of being the person who joined the queue first.
I thought about using https://redis.io/commands#list instead but there's no sorting by "score". I feel like if I used that I'd have to sort the entire queue by their rank every time I went to retrieve the first person in the queue, and lindex
would be wrong since there's no sorting.
Any recommendations on how to tackle a system like this?
when multiple elements have the same score,redis sorted set are ordered lexicographically. But, if I have understood your problem correctly you basically want to sort them by insertion time instead of lexicographical ordering. if this is what you want you can add the timestamp to the beginning of the element of your sorted items like follows:
ZADD myzset 1 "1593281577266:useroneid"
ZADD myzset 1 "1593281577467:usertwoid"
let me know if this is what you wanted.