Lists & Sets
Ordered sequences, blocking pops, membership, and set algebra
Lists keep elements in insertion order, duplicates allowed. Sets keep unique elements, unordered. Between them they cover feeds, queues, tags, and membership checks.
Lists
A list is a sequence you push to and pop from at either end in O(1). A small list is a single listpack (one compact block). Past about 8 KB it becomes a quicklist: a linked list of listpack blocks, so it stays compact and cheap at both ends.
The capped feed
LPUSH then LTRIM is the standard "latest N" pattern, such as recent
orders, activity streams, or log tails:
LPUSH app:feed:recent_orders order:5021
LTRIM app:feed:recent_orders 0 99 # never grows past 100
LRANGE app:feed:recent_orders 0 19 # page 1Lists as queues
Producers LPUSH; workers BRPOP, which blocks until an element
arrives (or a timeout), so idle workers don't poll:
# worker
BRPOP app:queue:emails 30 # wait up to 30s; returns [key, value]
# reliable version: atomically move the job to a per-worker "processing" list,
# so a crash doesn't lose it. Remove it with LREM after success.
BLMOVE app:queue:emails app:queue:emails:processing:w1 RIGHT LEFT 30Plain BRPOP removes the job the moment it's delivered. If the worker
crashes before finishing, the job is gone. BLMOVE to a processing list
is the classic fix, but you also need something that finds jobs stuck in
dead workers' lists and requeues them. Streams
with consumer groups do all of that for you, and are the better choice
for new queues.
Sets
A set holds unique strings with O(1) add, remove, and membership test.
Set algebra
The reason to choose sets over a list of IDs: server-side intersection, union, and difference.
Common uses: tagging and faceted filters, "friends in common"
(SINTER user:1:friends user:2:friends), deduplicating events, tracking
which users have seen something.
SINTER costs O(N·M) in the worst case, where N is the smallest set's
size and M the number of sets. Intersecting a 10-million-member set
with others on every request will show up in the slowlog. Keep the
inputs small, or precompute with SINTERSTORE and a TTL.
Encodings
SADD s 1 2 3 -> OBJECT ENCODING s "intset" (only integers, up to 512)
SADD s2 a b -> "listpack" (up to 128 small members)
-> "hashtable" (beyond either limit)An intset is a sorted array of integers. Membership is a binary search. It's tiny. A set of a few hundred numeric IDs costs a few hundred bytes per set this way.
Choosing between them
| Need | Use |
|---|---|
| Latest N items, in order | List + LTRIM |
| Simple work queue | List + BLMOVE, or better, a stream |
| "Is X in the group?" | Set |
| Unique items, intersections, unions | Set |
| Unique items with an order or score | Sorted set |
| Count unique items, approximately, in 12 KB | HyperLogLog |
Lab
LRANGE app:feed:recent_orders 0 4andLLENit.- In one terminal,
BRPOP app:queue:test 0(blocks). In another,LPUSH app:queue:test hello. Watch the first terminal return. SINTER app:category:peripherals app:tags:sale. Which products are peripherals on sale?- Create a set of 600 integers with a loop and check its encoding before and after the 512th.