Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is BRPOP implemented in Redis?

Tags:

redis

I'm trying to understand how BRPOP for lists works in Redis. We use Redis as a message queue for background workers. A web process LPUSHs to Redis lists and workers pick them up with BRPOP.

The 'blocking' aspect of BRPOP (and other list commands) eludes me: Does blocking in this context mean that Redis keeps an internal, ordered list of clients that are waiting on a given list with BRPOP, and sends only 1 list item to one client? so with each pop, a single client gets a list item, and that item is removed.

Some of the Redis docs seem to indicate 'blocking' at the connection level, which if true, seems that multiple clients could not brpop a given list at the same time.

like image 557
Luke W Avatar asked Dec 23 '16 03:12

Luke W


1 Answers

Yes, Redis maintains a list of clients in the order of time they have been waiting for a particular list. For more clarity, read this documentation for BLPOP, which is implemented in the same way as BRPOP.

"If multiple clients are blocked for the same key, the first client to be served is the one that was waiting for more time (the first that blocked for the key). Once a client is unblocked it does not retain any priority, when it blocks again with the next call to BLPOP it will be served accordingly to the number of clients already blocked for the same key, that will all be served before it (from the first to the last that blocked)."

For documentation in detail: https://redis.io/commands/blpop

like image 84
Alok Singhal Avatar answered Nov 09 '22 20:11

Alok Singhal