Skip to content
Sign inGet all access
Chapter 5·14 minFree

Lists, Queues and Streams

Ordered data, and the two ways to do messaging.

A list keeps insertion order and is cheap at both ends, so it is the first thing people reach for as a queue. This chapter is when that is right.

Pushing and reading

RPUSH adds at the right, LPUSH at the left, and both return the new length:

RPUSH q a b c
LRANGE q 0 -1
LPUSH q first
LRANGE q 0 -1
Output(integer) 31) "a"2) "b"3) "c"(integer) 41) "first"2) "a"3) "b"4) "c"

LRANGE q 0 -1 is the whole list: inclusive indexes, negative from the end. LINDEX reads one position:

RPUSH l a b c
LINDEX l 0
LINDEX l -1
LINDEX l 99
Output(integer) 3"a""c"(nil)

Popping

Both ends again, and a count pops several at once:

RPUSH q a b c d e
LPOP q
RPOP q
LPOP q 2
LRANGE q 0 -1
LPOP empty
Output(integer) 5"a""e"1) "b"2) "c"1) "d"(nil)

Popping the last element removes the key, as with hashes in chapter 4:

RPUSH q only
LPOP q
EXISTS q
TYPE q
Output(integer) 1"only"(integer) 0none

A list as a queue

Push one end, pop the other, and you have FIFO:

RPUSH q first second third
LPOP q
LPOP q
Output(integer) 3"first""second"

BLPOP

LPOP on an empty list returns immediately, so a worker would have to poll. BLPOP waits instead:

RPUSH jobs job1
BLPOP jobs 0
Output(integer) 11) "jobs"2) "job1"

LMOVE

The gap in 5.3 was that a popped job exists nowhere. LMOVE pops and pushes as one step:

RPUSH pending a b
LMOVE pending processing LEFT RIGHT
LRANGE pending 0 -1
LRANGE processing 0 -1
Output(integer) 2"a"1) "b"1) "a"

The job is in processing before work starts, so a sweeper can return it if the worker dies. Source and destination may match, rotating a list:

RPUSH ring a b c
LMOVE ring ring LEFT RIGHT
LRANGE ring 0 -1
Output(integer) 3"a"1) "b"2) "c"3) "a"

LTRIM

It keeps a range and discards the rest, which caps a list in place:

RPUSH feed a b c d e
LTRIM feed 0 2
LRANGE feed 0 -1
LLEN feed
Output(integer) 5OK1) "a"2) "b"3) "c"(integer) 3

Pub/Sub

PUBLISH sends to a channel and returns how many clients received it:

PUBLISH news hello
Output(integer) 0

With a subscriber connected, the same command returns 1 and the subscriber prints the message. It needs a second connection, because SUBSCRIBE blocks:

terminal
redis-cli SUBSCRIBE news
Output$ redis-cli SUBSCRIBE news1) "subscribe"2) "news"3) (integer) 11) "message"2) "news"3) "hello"

Streams

A stream is an append-only log with ids. XADD with * generates one from the clock:

terminal
redis-cli XADD events '*' action login
Output$ redis-cli XADD events '*' action login"1795761399055-0"

Milliseconds, a dash, then a sequence number. Explicit ids make the rest easier to see, and XRANGE - + reads everything:

XADD events 1-1 action login
XADD events 2-1 action logout
XLEN events
XRANGE events - +
Output"1-1""2-1"(integer) 21) 1) "1-1" 2) 1) "action" 2) "login"2) 1) "2-1" 2) 1) "action" 2) "logout"

Ids must increase, which is what makes the log append-only:

XADD s 5-1 f v
XADD s 3-1 f v
Output"5-1"(error) ERR The ID specified in XADD is equal or smaller than the target stream top item

Consumer groups

A group tracks what each consumer has taken and whether it finished. XREADGROUP with > means “entries nobody has yet”:

XADD tasks 1-1 job a
XADD tasks 2-1 job b
XGROUP CREATE tasks workers 0
XREADGROUP GROUP workers alice COUNT 1 STREAMS tasks >
Output"1-1""2-1"OK1) 1) "tasks" 2) 1) 1) "1-1" 2) 1) "job" 2) "a"

That entry is now pending — delivered, not confirmed:

continues
XPENDING tasks workers
Output1) (integer) 12) "1-1"3) "1-1"4) 1) 1) "alice" 2) "1"

XACK confirms it, and the pending list empties:

continues
XACK tasks workers 1-1
XPENDING tasks workers
Output(integer) 11) (integer) 02) (nil)3) (nil)4) (nil)

Choosing between the three

Stored? Survives a dead worker? Several readers?
Pub/Sub no no every subscriber gets a copy
List yes only with LMOVE one pops each item
Stream + group yes yes, pending until acked split, or copy per group