Complete Guide to Redis Lists
When it comes to data structures, Redis offers a wide range of options to efficiently manage and manipulate your data and one of them is list. A list is an ordered collection of values associated with a unique index. Unlike arrays, Redis lists are not limited to a fixed size and can expand or shrink on per need basis. In real, it works like linked lists under the hood, which means adding or removing elements from the beginning or the end of the list is an efficient operation here having a time complexity of O(1) that is constant.
Important Topics for the Redis Lists
- Redis Lists Commands
- 1. keys Command
- 2. lpush Command
- 3. lrange Command
- 4. rpush Command
- 5. llen Command
- 6. lpop Command
- 7. rpop Command
- 8. lset Command
- 9. linsert Command
- 10. lindex Command
- 11. lpushx Command
- 12. rpushx Command
- 13. sort Command
- 14. lrem Command
- 16. ltrim Command
- 17. blpop Command
- 18. brpop Command
- 19. blmove Command
- 20. lmpop Command
- How to Treat a list like a queue?
- How to Treat a list like a stack?
- Conclusion
Redis Lists Commands
1. keys Command
Syntax: keys *
Explanation: Shows all the stored variables of key-value pairs
Example: keys *
2. lpush Command
Syntax: lpush list_name list_item
Explanation: Pushing item in list from top
Example: lpush Fruits Apple Mango Banana
3. lrange Command
Syntax: lrange list_name range
Explanation: Shows all list items 0 to -1
Example: lrange Fruits 0 -1
4. rpush Command
Syntax: rpush list_name list_item
Explanation: Pushing item in list from bottom
Example: rpush Fruits Litchi Guava Pineapple
5. llen Command
Syntax: llen list_name
Explanation: List length/no. of items
Example: llen Fruits
6. lpop Command
Syntax: lpop list_name
Explanation: Removes item from top of the list
Example: lpop Fruits
7. rpop Command
Syntax: rpop list_name
Explanation: Removes item from bottom of the list
Example: rpop Fruits
8. lset Command
Syntax: lset list_name index_no list_item_name
Explanation: Modifies list item name at particular index
Example: lset Fruits 3 Grapes
9. linsert Command
Syntax: linsert list_name before/after list_item “Add_this_list_item”
Explanation: Adds a list item before a particular list item
Example: linsert Fruits before Grapes Watermelon
10. lindex Command
Syntax: lindex list_name index_no
Explanation: Gives the item_name at a particular index
Example: lindex Fruits 4
11. lpushx Command
Syntax: lpushx list_name list_item
Explanation: Adds the list_item from top, it first checks if that lists exists or not
Example: lpushx Vegetables Lemon
12. rpushx Command
Syntax: rpushx list_name list_item
Explanation: Similar to above one, but adds item from bottom
Example: rpushx Dryfruits Almonds
13. sort Command
Syntax: sort list_name ALPHA
Explanation: Sorts lists in ascending order, for descending order use keyword “desc” before ALPHA keyword {ALPHA keyword for string}. In case of integers or any other type you don’t have to write “ALPHA” keyword
Example: sort Fruits ALPHA
14. lrem Command
Syntax: lrem list_name no_of_item word
Explanation: Removes “n” no. of same element from the list
Example: lrem Non_Veg -2 Egg
15. lmove Command
Syntax: lmove list_name1 list_name2 where_to_place_it_in_list2 place_of_item_in_list1
Explanation: Individual List element gets moved from list 1 to list 2, by using “Left” and “Right” keyword. Where “left” and “right simply indicating item in left or right.
Example: lmove Fruits MyBasket Right Left
16. ltrim Command
Syntax: ltrim list_name range
Explanation: It makes sure that the list will be trimed to specified length
Example: ltrim MyBasket 0 3
17. blpop Command
Syntax: blpop list_name1 list_name2 time
Explanation: It is the blocking version of lpop cmd, it will block ‘lpop’ cmd till there aren’t any value to pop it or till timeout. If till timeout source list ‘List1’ didn’t contain any value then it will return nill.
Example: blpop WebTechStack AppTechStack 20
18. brpop Command
Syntax: brpop List1 List2 time
Explanation: It is the blocking version of rpop cmd, it will block ‘rpop’ cmd till there aren’t any value to pop it or till timeout. If till timeout source list ‘List1’ didn’t contain any value then it will return nill.
Example: brpop WebTechStack AppTechStack 20
19. blmove Command
Syntax: blmove list_name_source list_name_destination destination_left/right source_left/right time
Explanation: It blocks lmove cmd till the source is empty or till a client doesn’t push any value in it under given time constraints. In this example, first we made 2 different lists “WebTechStack” and “AppTechStack” then made them empty, as this cmd won’t work if source (WebTechStack) will contain any element. Then we executed the command, and it started waiting for client to push some values under given time “20s” then after timeout it showed ‘nil’ because no vale was pushed by client.
Example: blmove WebTechStack AppTechStack LEFT LEFT 20
20. lmpop Command
Syntax: lmpop time numkey left/right count
Explanation: It pops one or more elements from first non-empty list. Its similar to lpop, rpop, blpop, brpop like cmds. It will return ‘nill’ when no element can be poped. First it popped elements of first list till it get empty, then it popped 2nd list.
Example: lmpop 2 Web App LEFT 3
How to Treat a list like a queue?
To treat a list like a queue in Redis, you can use the following commands:
- LPUSH to add items to the head of the queue.
- RPOP to remove items from the tail of the queue.
For example, to create a queue for bike repairs, you would use the following command:
LPUSH bikes:repairs bike:1
This will add the bike with the ID 1 to the head of the queue.
To process the next bike in the queue, you would use the following command:
RPOP bikes:repairs
This will remove the first bike from the queue and return its ID.
You can also use the following commands to manage your queue:
- LLEN to get the length of the queue.
- LRANGE to get a range of items from the queue.
- LREM to remove items from the queue.
For example, to get the length of the bikes:repairs queue, you would use the following command:
LLEN bikes:repairs
This will return the number of bikes in the queue.
To get the first two bikes in the queue, you would use the following command:
LRANGE bikes:repairs 0 1
This will return a list containing the IDs of the first two bikes in the queue.
To remove the bike with the ID 1 from the queue, you would use the following command:
LREM bikes:repairs 1 1
This will remove the first occurrence of the bike with the ID 1 from the queue.
You can use these commands to implement a variety of queue-based applications in Redis, such as task queues, message queues, and job queues.
How to Treat a list like a stack?
To treat a list like a stack in Redis, you can use the following commands:
- RPUSH to push items onto the stack.
- LPOP to pop items off the stack.
For example, to create a stack for storing temporary data, you would use the following command:
RPUSH temp_data item1
This will push the item item1 onto the stack.
To get the item at the top of the stack, you would use the following command:
LPOP temp_data
This will return the item item1 and remove it from the stack.
You can also use the following commands to manage your stack:
- LLEN to get the length of the stack.
- LRANGE to get a range of items from the stack.
- LREM to remove items from the stack.
For example, to get the length of the temp_data stack, you would use the following command:
LLEN temp_data
This will return the number of items on the stack.
To get the first two items on the stack, you would use the following command:
LRANGE temp_data 0 1
This will return a list containing the first two items on the stack.
To remove the item item1 from the stack, you would use the following command:
LREM temp_data 1 1
This will remove the first occurrence of the item item1 from the stack.
You can use these commands to implement a variety of stack-based applications in Redis, such as undo/redo functionality, function call stacks, and expression evaluation stacks.
Conclusion
Redis lists provides a base for building high-performance applications. By understanding the Redis lists and their operations, developers can leverage the full potential of Redis to enhance their applications’ functionality and responsiveness.
Contact Us