Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Redis LPOP / RPOP operation atomic?

Tags:

redis

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 ?

like image 799
user2701063 Avatar asked Apr 16 '18 03:04

user2701063


1 Answers

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.

like image 170
for_stack Avatar answered Oct 15 '22 01:10

for_stack