w3resource

Redis Lists: BRPOP

Redis BRPOP Command

Redis BRPOP command is used to block the connection when there are no elements to pop from any of the given lists or remove and get the last element in a list if available. It is a blocking list pop primitive. An element is popped from the tail of the first list that is non-empty.

Here are some common uses and scenarios for the "BRPOP" command:

  • Queue Processing: Implement blocking queue consumers that wait for new elements to process, retrieving from the end of the list.
  • Task Management: Handle tasks in a distributed system where workers wait for new tasks to be added to a list.
  • Event Handling: Block until events or messages are available in a list, making it suitable for real-time applications that require immediate processing.
  • Load Balancing: Distribute workload among multiple workers by having them block and wait for new tasks, improving resource utilization.
  • Timeout Handling: Specify a timeout to limit how long the command will block, allowing for flexible processing strategies and preventing indefinite blocking.

Syntax:

BRPOP LIST1 LIST2 .. LISTN TIMEOUT    

Available since

2.0.0.

Return Value

String reply, value of element stored at key or nil

Return Value Type

String

Example: Redis BRPOP

127.0.0.1:6379> RPUSH mycolor1 R G B
(integer) 3
127.0.0.1:6379> RPUSH mycolor2 Y O P
(integer) 3
127.0.0.1:6379> BRPOP mycolor mycolor1 mycolor2 30
1) "mycolor1"
2) "B"
127.0.0.1:6379> BRPOP mycolor mycolor1 mycolor2 30
1) "mycolor1"
2) "G"
127.0.0.1:6379> BRPOP mycolor mycolor1 mycolor2 30
1) "mycolor1"
2) "R"
127.0.0.1:6379> BRPOP mycolor mycolor1 mycolor2 30
1) "mycolor2"
2) "P"
127.0.0.1:6379> BRPOP mycolor mycolor1 mycolor2 30
1) "mycolor2"
2) "O"
127.0.0.1:6379> BRPOP mycolor mycolor1 mycolor2 30
1) "mycolor2"
2) "Y"
127.0.0.1:6379> BRPOP mycolor mycolor1 mycolor2 30

(nil)
(30.04s)

Above example will block the client for 30 seconds to execute any command. If any data comes in the specified key lists then it returns otherwise after 30 seconds nil value is returned.

Previous: BLPOP
Next: BRPOPLPUSH



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/redis/redis-brpop-key1-timeout.php