I am trying to build FIFO queue in Redis, but I am just worried about concurrency. What if 2 clients try to do RPOP operation simultaneously?
If RPOP/LPOP is not atomic then how can I achieve atomicity using MULTI/EXEC ?
Is Redis LPOP / RPOP operation atomic?
Yes, both LPOP
and RPOP
are atomic.
What if 2 clients try to do RPOP operation simultaneously?
If the size of the LIST
is equal to or greater than 2
, both clients get a different item. If the LIST
has only one item, only one client gets the item, and the other client gets null reply. If the LIST
is empty, both clients get null reply.
Another Solution
You can also use BLPOP
or BRPOP
to implement the FIFO
. These two commands are also atomic and will block for empty LIST
. See the doc for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With