skip to content
Alvin Lucillo

Working with lists

/ 1 min read

A list in redis is usually used when the order of the values matters.

Notice from below that latest value added to the list is the leftmost, which is why when LPOP is performed, msg3 is removed. The opposite of this behavior is exhibited by RPOP. The former is for removing the value from the left side, and latter from the right. In the end, after performing LPOP and RPOP consecutively, only msg2 is left.

  ~ docker exec -it redis redis-cli
127.0.0.1:6379> LRANGE msg_queue 0 0
(empty array)

127.0.0.1:6379> LPUSH msg_queue msg1
(integer) 1

127.0.0.1:6379> LPUSH msg_queue msg2
(integer) 2

127.0.0.1:6379> LPUSH msg_queue msg3
(integer) 3

127.0.0.1:6379> LLEN msg_queue
(integer) 3

127.0.0.1:6379> LRANGE msg_queue 0 2
1) "msg3"
2) "msg2"
3) "msg1"

127.0.0.1:6379> LPOP msg_queue
"msg3"

127.0.0.1:6379> RPOP msg_queue
"msg1"

127.0.0.1:6379> LRANGE msg_queue 0 0
1) "msg2"

127.0.0.1:6379>