Redis Sorted Set as a Unique FIFO Queue
Some time ago , I was implementing a scenario where I RPUSH elements to a List on Redis , to later retrieve them via LPOP, hence creating a FIFO queue.
Just the other day, when I needed to ensure the uniqueness of the values in the list , I found out that there’s no efficient or neat way of doing so , with a List on Redis, due to the very nature of Redis . On a related question on StackOverflow , the best answer would have 3 suggestions , with various trade-offs:
1-Using
LREM
and replacing it if it was found.2-Maintaining a separate
SET
in conjunction with yourLIST
3-Looping through the
LIST
until you find the item or reach the end.
None of them are good enough for production , unless you’re in dire need.
But fortunately , as I was checking the whole list of Redis commands, I happened to set my eyes on a new command I haven’t seen before: ZPOPMIN ! As the letter Z at the start of the name implies , for someone familiar with Redis , this is a command for Sorted Sets ;working somewhat similar to LPOP on a List.
With the help of this command , available since Redis 5.0.0(November 2018), you can use the Sorted Set as a FIFO queue with unique elements .
- using NX parameter on ZADD command -> ADD IF NOT EXISTS -> UNIQUE
- using Timestamp as score -> always increasing -> always MAX score -> Like RPUSH on a list
- minimum score = oldest element -> ZPOPMIN will always give the oldest element. Like LPOP on a List
Java Code Sample below :
Sample output: