Lists are some of the most common and valuable types in Redis. Not only because they provide a better memory experience compared to other types such as hashes, but because they are easy to use and manage.
Let us start with the basics.
Redis Create List
Since Redis is a key-value database, we need to create a key to hold a list of elements.
Redis uses the concept of left and right to add and remove elements. So, for example, to add an item to the left of the list, we use the LPUSH command. For right insertion, use the RPUSH command.
Take the example shown below:
(integer) 1
In the example command above, we use the LPUSH command to create a new key holding one element in the list.
After creation, we can add elements as shown in the example commands below:
Note: You can also use the RPUSH command, as shown in the example below:
Keep in mind that the order of insertion matters in a list.
HINT: A single list in Redis can hold up to 4 billion elements.
Redis Retrieve List Items
To fetch the elements stored in a list, we use the LRANGE command followed by the starting and ending offset values. The offset, in this case, represents the index of the element in the list.
Indexing for a Redis list starts at 0.
For example, to retrieve all elements from index 0, we can run the command:
Passing the list start offset as 0 and the last offset as -1 returns all elements in the list.
The command should return the elements in the list as shown:
1) “C++”
2) “C”
3) “Elixir”
4) “Ocaml”
5) “JavaScript”
6) “Python”
7) “SQL”
8) “Java”
9) “C#”
Redis LPOP and BLPOP commands
When it comes to removing elements in a list, you will encounter two main commands:
Note: You can learn about the LREM command in this article <- click here.
The LPOP command will remove and return the first element in the list. An example usage is as shown below:
"C++"
Since the LPOP command returns the actual value removed, it is suitable for tracking changes in your application.
The BLPOP command, on the other hand, is the blocking version of the LPOP command. It removes and returns the first element in a list. However, unlike the LPOP command, it blocks the connection for a specified duration until one is available.
Consider the example shown below:
We pass a non-existent key to the BLPOP command in the example command above. We also specify the block duration as 10 seconds. If there is no available element in the specified list after elapsed, the command returns nil.
The command above should return (after 10 seconds).
(nil)
(10.05s)
If the element does exist in the specified list, it deletes and returns it as shown:
1) "languages"
2) "C"
In this case, the command returns the key from which it deletes an element and the actual element removed.
It does not wait for the specified duration to be elapsed.
Conclusion
This tutorial covered Redis lists and how to use them in your database. We discussed: creating a Redis list, adding elements, retrieving list elements, and removing keys from a list.
Thanks for reading!!