Reputation: 131
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 ?
Upvotes: 13
Views: 5174
Reputation: 23041
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.
Upvotes: 14