top of page
Writer's pictureSiddhesh Kadam

Redis List

Redis lists are string-valued linked lists. They are sorted by insertion order and can be added or removed at the top or bottom of the list. Redis lists are commonly used to implement stacks and queues, as well as to create queue management for background worker systems.


Redis lists have several advantages over standard data structures like arrays and linked lists. For example, they are extremely efficient in terms of memory usage and performance. Second, they are highly scalable, allowing them to store very large lists of data without experiencing performance issues. Third, they are fault-tolerant, meaning they can recover from failures without losing data.

1. Size Limitation


A Redis list can have a maximum of 4,294,967,295 elements. This is a very large number, and you are probably going to never need to store a list of this size in practise.


2. Create a List


In Redis, the LPUSH command adds one or more elements to the list's head (leftmost side). If the list does not exist, it is automatically created.

127.0.0.1:6379> LPUSH mylist python go java php perl
(integer) 5
127.0.0.1:6379> LRANGE mylist 0 5
1) "perl"
2) "php"
3) "java"
4) "go"
5) "python"
127.0.0.1:6379>

3. Append one or multiple elements to a list In Redis, the RPUSH command adds one or more elements to the tail (rightmost side) of a list. If the list does not exist, it is automatically created.

127.0.0.1:6379> RPUSH mylist ruby
(integer) 6
127.0.0.1:6379> LRANGE mylist 0 6
1) "perl"
2) "php"
3) "java"
4) "go"
5) "python"
6) "ruby"
127.0.0.1:6379>

4. Get an element from a list by its index


In Redis, the LINDEX command returns the element at the provided index in the list at the supplied key. Because the index is zero-based, 0 represents the first element, 1 represents the second element, and so on. Negative index can be used to mark entries at the bottom of a list. In this case, -1 indicates the last element, -2 the penultimate, and so on.

127.0.0.1:6379> LINDEX mylist 4
"python"
127.0.0.1:6379>

5. Insert an element before or after another element in a list In Redis, the LINSERT command inserts an entry into a list before or after a given element. The provided element is known as the "pivot."

127.0.0.1:6379> LINSERT mylist before php bash
(integer) 7
127.0.0.1:6379> LRANGE mylist 0 7
1) "perl"
2) "bash"
3) "php"
4) "java"
5) "go"
6) "python"
7) "ruby"
127.0.0.1:6379>

We added the element "bash" before "php" in the above example.

127.0.0.1:6379> LINSERT mylist after go c++
(integer) 8
127.0.0.1:6379> LRANGE mylist 0 8
1) "perl"
2) "bash"
3) "php"
4) "java"
5) "go"
6) "c++"
7) "python"
8) "ruby"
127.0.0.1:6379>

We added the element "c++" after "go" in the above example.


6. Remove and get the first elements in a list In Redis, the LPOP command removes and returns the first entry of a list. LPOP returns nil if the list is empty.

127.0.0.1:6379> LPOP mylist
"perl"
127.0.0.1:6379> LRANGE mylist 0 8
1) "bash"
2) "php"
3) "java"
4) "go"
5) "c++"
6) "python"
7) "ruby"
127.0.0.1:6379>

The element "perl" was deleted from a list and returned to the prompt in this example.


7. Get the length of a list


In Redis, the LLEN command returns the length of the list stored at the supplied key.

127.0.0.1:6379> LLEN mylist
(integer) 7
127.0.0.1:6379>

In total, there are 7 items in our list.


8. Remove elements from a list


In Redis, the LREM command removes the list stored at key's first count instances of elements equal to value. Either a positive or negative count argument is possible. A positive count eliminates count elements beginning at the list's beginning. Count elements are eliminated when the count is negative, beginning at the end of the list. Our list of elements is below.

127.0.0.1:6379> LRANGE mylist 0 11
 1) "php"
 2) "php"
 3) "Rlang"
 4) "html"
 5) "bash"
 6) "php"
 7) "java"
 8) "go"
 9) "c++"
10) "python"
11) "ruby"
127.0.0.1:6379>

Now lets remove all element named "php"

127.0.0.1:6379> LREM mylist 0 php
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 11
1) "Rlang"
2) "html"
3) "bash"
4) "java"
5) "go"
6) "c++"
7) "python"
8) "ruby"
127.0.0.1:6379>

As you can see, all "php" element in the list has been removed in the example above.

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page