In addition, when all the elements are popped from the list, the associated key will be deleted from the Redis key space. Redis list is the ideal candidate for the low latency applications because the insertion and deletion operations have constant time complexity at the HEAD et TAIL. It doesn’t matter if five or thousand elements are in the list. The time for the insertion and deletion takes a constant time near the left and right ends.
There are some practical needs to remove the elements from the tail or head of a given list. The BRPOP et BLPOP commands are introduced from the Redis version 2.0.0 to fulfill the mentioned requirement. The BRPOP command is evaluated in this guide.
The BRPOP Command
The BRPOP is the advanced version of the RPOP command. The RPOP command pops an element from the tail of the list that is stored at a given key. As the name suggests, the BRPOP command is the Blocking version of the RPOP. When the key doesn’t exist, The BRPOP command won’t return with the nil value right away as in the RPOP command. Instead, it waits until a new list is created at that key. Upon the creation of a new list at the mentioned key, the BRPOP command pops the tail element. Also, this command accepts the multiple keys and only pop the elements from the first non-empty key.
The following is the syntax for the BRPOP command:
BRPOP list_key [list_key …] timeout
list_key: This is the key of the list.
timeout: This is the timeout value in seconds where the client is blocked until this timeout reaches.
Usually, the BRPOP command returns an array output:
If a non-empty list is specified, the command returns the popped element value and the key of the containing list as in the following format:
When no single element is to be popped from any of the lists specified and the timeout has expired, the nil value is returned.
Example: Radiodifusión Station Playlist Manipulation with BRPOP
Let’s assume that a radiodifusión station needs to play songs from a daily playlist. The playlist contains the song IDs to play. The songs should be ordered in the insertion order. Each song is picked from the end of the playlist to play.
We can use the Redis list data structure to implement the mentioned playlist and the list operations to manipulate the playlist songs. Let’s create a playlist called MidNightPlaylist and add some song IDs as shown in the following. The LPUSH command is used for that:
lpush MidNightPlaylist song001 song004 song010 song100 song101
This would create a list as shown in the following:
head –> song101 | song100 | song010 | song004 | song001 <– tail
Non-Blocking Behavior of the BRPOP Command
Now is the time to start a show. We should pick some songs from the end of the playlist. Hence, song001 should be removed from the playlist and it has to be played by the recorder. We use the BRPOP command to pop the song001 from the playlist and to get the song ID to the client listener.
The timeout argument is specified as five seconds. Since the MidNightPlaylist contains elements, the BRPOP command behaves in a non-blocking way. Hence, it pops and returns the tail element to the client.
Blocking Behavior of the BRPOP Command
The previous results can be achieved through the RPOP command as well. Hence, let’s look into the vivo advantage of the BRPOP command with the blocking behavior. Let’s remove all the elements from the MidNightPlaylist key using the RPOP command.
This removes all the remaining elements from the list and the MidNightPlaylist key is deleted from the Redis key space as well.
Let’s execute the BRPOP command with 60 seconds timeout and the non-existing key MidNightPlaylist. Now, the command behaves in a blocking manner. It waits for the key to be created and the element to be present in the playlist MidNightPlaylist.
Let’s push an element to the MidNightPlaylist via another terminal window that is connected to the same Redis data store.
lpush MidNightPlaylist song400
Upon the creation of the MidNightPlaylist list with the element song400, the client window which executed the BRPOP command pops the element song400 from the playlist and returns the song ID as output instantly.
Pick a Song from Multiple Playlists
There can be multiple playlists created by the radiodifusión station. Hence, we should pick a song from the given playlists at a given time. Let’s assume we got three playlists: MidNightPlaylist1, MidNightPlaylist2, and MidNightPlaylist3. The MidNightPlaylist1 is already empty while the other two playlists are non-empty.
lpush MidNightPlaylist2 song1002 song1005
lpush MidNightPlaylist3 song3000
Let’s call the BRPOP command with all the three keys as shown in the following:
brpop MidNightPlaylist1 MidNightPlaylist2 MidNightPlaylist3 10
Since the first key MidNightPlaylist1 is empty, it is ignored by the command here. It checks for the first non-empty key from the available key list. Hence, the command locates the MidNightPlaylist2 as the first non-empty key from the order of keys. As expected, the song1002 is removed from the MidNightPlaylist2.
The BRPOP command has constant time complexity near the head and tail when a single key is specified. The time complexity becomes O(N) when multiple keys are specified in the command. Furthermore, this command is very efficient to use in the low latency applications such as a queue, stack, timeline in social media, etc.
To summarize, a Redis list is a collection of string elements stored at a given key while sorted in the insertion order. Several commands are available to operate on the Redis lists with constant time complexity near the head and tail. As stated, The BRPOP command is used to remove the elements from the right side of the Redis list stored at a given key with the support of blocking. The BRPOP command blocks the client connection when no elements are available to remove from the specified lists. As you know, this command accepts multiple keys where an element pops from the first non-empty list where each key is checked in the order in which they are passed to the command.