Redis Lists
A Redis list is an ordered sequence of strings. You can push and pop at both ends in constant time, which makes lists ideal for queues, stacks, activity feeds and "the last N items" logs. Elements can repeat, and their order is exactly the order in which you inserted them.
Pushing and reading
LPUSH adds elements to the left (head) and RPUSH to the right (tail). Both return the new length. LRANGE key start stop reads a range; 0 -1 means "everything".
RPUSH playlist "Song A" "Song B"
RPUSH playlist "Song C"
LPUSH playlist "Intro"
LRANGE playlist 0 -1
LLEN playlist
LINDEX playlist 0
LINDEX playlist -1redis> RPUSH playlist "Song A" "Song B"
(integer) 2
redis> RPUSH playlist "Song C"
(integer) 3
redis> LPUSH playlist "Intro"
(integer) 4
redis> LRANGE playlist 0 -1
1) "Intro"
2) "Song A"
3) "Song B"
4) "Song C"
redis> LLEN playlist
(integer) 4
redis> LINDEX playlist 0
"Intro"
redis> LINDEX playlist -1
"Song C"
Note that LPUSH a b c pushes one element at a time, so the list ends up as c b a:
LPUSH letters a b c
LRANGE letters 0 -1redis> LPUSH letters a b c
(integer) 3
redis> LRANGE letters 0 -1
1) "c"
2) "b"
3) "a"
Popping
LPOP and RPOP remove and return an element from an end. With a count they return several. Popping the last element deletes the key, because Redis never keeps empty lists.
RPUSH jobs email:1 email:2 email:3 email:4
LPOP jobs
RPOP jobs
LPOP jobs 2
LPOP jobs
EXISTS jobsredis> RPUSH jobs email:1 email:2 email:3 email:4
(integer) 4
redis> LPOP jobs
"email:1"
redis> RPOP jobs
"email:4"
redis> LPOP jobs 2
1) "email:2"
2) "email:3"
redis> LPOP jobs
(nil)
redis> EXISTS jobs
(integer) 0
Stack or queue?
The direction you push and pop decides the behaviour:
| Pattern | Push | Pop | Order |
|---|---|---|---|
| Queue (FIFO) | RPUSH |
LPOP |
first in, first out |
| Stack (LIFO) | LPUSH |
LPOP |
last in, first out |
# A stack of undo actions
LPUSH undo "typed hello" "deleted line" "pasted image"
LPOP undo
LPOP undoredis> LPUSH undo "typed hello" "deleted line" "pasted image"
(integer) 3
redis> LPOP undo
"pasted image"
redis> LPOP undo
"deleted line"
Changing elements in the middle
Lists also support positional operations, but they cost O(N) because Redis has to walk to the position:
| Command | Effect |
|---|---|
LSET key index value |
replace the element at an index |
LINSERT key BEFORE|AFTER pivot value |
insert next to the first matching element |
LREM key count value |
remove up to count occurrences (0 = all) |
LPOS key value |
find the index of an element |
RPUSH steps wash cut cook serve cut
LSET steps 0 rinse
LINSERT steps AFTER cook taste
LRANGE steps 0 -1
LPOS steps cut
LPOS steps cut COUNT 0
LREM steps 0 cut
LRANGE steps 0 -1redis> RPUSH steps wash cut cook serve cut
(integer) 5
redis> LSET steps 0 rinse
OK
redis> LINSERT steps AFTER cook taste
(integer) 6
redis> LRANGE steps 0 -1
1) "rinse"
2) "cut"
3) "cook"
4) "taste"
5) "serve"
6) "cut"
redis> LPOS steps cut
(integer) 1
redis> LPOS steps cut COUNT 0
1) (integer) 1
2) (integer) 5
redis> LREM steps 0 cut
(integer) 2
redis> LRANGE steps 0 -1
1) "rinse"
2) "cook"
3) "taste"
4) "serve"
Capped lists with LTRIM
LTRIM key start stop keeps only the given range and throws the rest away. Pushing and trimming together gives you a list that never grows past a fixed size, a classic pattern for "recent activity":
LPUSH recent:views product:1 product:2 product:3
LTRIM recent:views 0 2
LPUSH recent:views product:4
LTRIM recent:views 0 2
LRANGE recent:views 0 -1redis> LPUSH recent:views product:1 product:2 product:3
(integer) 3
redis> LTRIM recent:views 0 2
OK
redis> LPUSH recent:views product:4
(integer) 4
redis> LTRIM recent:views 0 2
OK
redis> LRANGE recent:views 0 -1
1) "product:4"
2) "product:3"
3) "product:2"
product:1, the oldest view, fell off the end.
Moving elements between lists
LMOVE source destination LEFT|RIGHT LEFT|RIGHT pops from one list and pushes to another in a single atomic step. It is the base of the reliable queue pattern: a worker moves a job into its own "processing" list, so the job is not lost if the worker crashes before finishing.
RPUSH queue:pending job:1 job:2 job:3
LMOVE queue:pending queue:processing LEFT RIGHT
LRANGE queue:pending 0 -1
LRANGE queue:processing 0 -1
LREM queue:processing 1 job:1
LLEN queue:processingredis> RPUSH queue:pending job:1 job:2 job:3
(integer) 3
redis> LMOVE queue:pending queue:processing LEFT RIGHT
"job:1"
redis> LRANGE queue:pending 0 -1
1) "job:2"
2) "job:3"
redis> LRANGE queue:processing 0 -1
1) "job:1"
redis> LREM queue:processing 1 job:1
(integer) 1
redis> LLEN queue:processing
(integer) 0
LMPOP pops from the first non-empty list among several keys, which is useful for priority queues:
RPUSH tasks:low cleanup
RPUSH tasks:high fix-outage
LMPOP 2 tasks:high tasks:low LEFT
LMPOP 2 tasks:high tasks:low LEFT
LMPOP 2 tasks:high tasks:low LEFTredis> RPUSH tasks:low cleanup
(integer) 1
redis> RPUSH tasks:high fix-outage
(integer) 1
redis> LMPOP 2 tasks:high tasks:low LEFT
1) "tasks:high"
2) 1) "fix-outage"
redis> LMPOP 2 tasks:high tasks:low LEFT
1) "tasks:low"
2) 1) "cleanup"
redis> LMPOP 2 tasks:high tasks:low LEFT
(nil)
The reply names the key it popped from, then the elements. When both lists are empty it returns (nil).
Blocking pops
Workers usually wait for jobs with blocking commands: BLPOP, BRPOP, BLMOVE and BLMPOP wait until an element arrives or a timeout passes, instead of polling in a loop.
redis> BLPOP queue:emails 5
(nil)
(5.01s)Here the worker waited 5 seconds and got nothing. If another client had pushed a job during that time, BLPOP would have returned it immediately. (Blocking commands are not run in these playgrounds, since they need a second client.)
Test yourself
After RPUSH q a b and LPUSH q c, what does LRANGE q 0 -1 return?
- a b c
- c a b
- b a c
- c b a
Which pair of commands makes a FIFO queue?
- LPUSH + LPOP
- RPUSH + RPOP
- RPUSH + LPOP
- LSET + LINDEX
What does LTRIM log 0 99 do?
- Deletes the first 100 elements
- Keeps only the first 100 elements
- Returns the first 100 elements
- Sets a limit for future pushes