Lists
Lists are ordered sequences of strings, implemented as linked lists. They support push/pop from both ends in O(1) and are commonly used for queues, stacks, and activity feeds.
LPUSH / RPUSH / LPOP / RPOP
Push elements to the head or tail of a list, and pop elements from either end. Multiple elements can be pushed in a single call.
LPUSH key element [element ...]
RPUSH key element [element ...]
LPOP key [count]
RPOP key [count]> RPUSH queue:jobs "job:1" "job:2" "job:3"
(integer) 3
> LPOP queue:jobs
"job:1"
> RPOP queue:jobs
"job:3"
> LPUSH stack:undo "action:5"
(integer) 1LRANGE / LLEN / LINDEX
LRANGE returns elements in a range (0-based, inclusive). Use -1 for the last element. LLEN returns the list length. LINDEX returns the element at a given index.
> RPUSH feed:user1 "post:a" "post:b" "post:c" "post:d"
(integer) 4
> LRANGE feed:user1 0 -1
1) "post:a"
2) "post:b"
3) "post:c"
4) "post:d"
> LRANGE feed:user1 0 1
1) "post:a"
2) "post:b"
> LLEN feed:user1
(integer) 4
> LINDEX feed:user1 2
"post:c"LSET / LINSERT / LREM / LTRIM
Modify lists in place. LSET updates an element by index. LINSERT adds before or after a pivot. LREM removes occurrences. LTRIM keeps only a range and discards the rest.
> LSET feed:user1 0 "post:updated"
OK
> LINSERT feed:user1 BEFORE "post:c" "post:new"
(integer) 5
> LREM feed:user1 1 "post:d"
(integer) 1
> LTRIM feed:user1 0 99
OKLMOVE / RPOPLPUSH
Atomically move an element from one list to another. LMOVE is the generalized form; RPOPLPUSH pops from the tail of the source and pushes to the head of the destination.
LMOVE source destination LEFT|RIGHT LEFT|RIGHT> RPUSH queue:pending "task:1" "task:2"
(integer) 2
> LMOVE queue:pending queue:processing LEFT RIGHT
"task:1"
> RPOPLPUSH queue:pending queue:processing
"task:2"BLPOP / BRPOP / BLMOVE
Blocking variants that wait for elements when the list is empty. The timeout is in seconds (0 means block indefinitely). These are the foundation for reliable queue patterns.
BLPOP key [key ...] timeout
BRPOP key [key ...] timeout
BLMOVE source destination LEFT|RIGHT LEFT|RIGHT timeout> BLPOP queue:jobs 30
1) "queue:jobs"
2) "job:42"Command Reference
| Command | Description |
|---|---|
| LPUSH | Push elements to the head of a list |
| RPUSH | Push elements to the tail of a list |
| LPOP | Remove and return element from the head |
| RPOP | Remove and return element from the tail |
| LLEN | Get the length of a list |
| LRANGE | Get elements in a range |
| LINDEX | Get element by index |
| LSET | Set element at an index |
| LINSERT | Insert before or after a pivot element |
| LREM | Remove elements by value |
| LTRIM | Trim list to a specified range |
| LPOS | Find the position of an element |
| LMOVE | Atomically move element between lists |
| RPOPLPUSH | Pop from tail, push to head of another list |
| LPUSHX | Push to head only if the list exists |
| RPUSHX | Push to tail only if the list exists |
| BLPOP | Blocking pop from the head |
| BRPOP | Blocking pop from the tail |
| BLMOVE | Blocking move between lists |