Learn Labs
Data Structures

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.

LPUSH app:feed:recent_orders order:5021add to the head (left)RPUSH app:queue:emails job:1 job:2add to the tail (right)LPOP app:feed:recent_ordersremove from the headRPOP app:queue:emails 2remove 2 from the tailLRANGE app:feed:recent_orders 0 9first 10: O(S+N), cheap near the endsLLEN app:feed:recent_ordersO(1)LINDEX app:feed:recent_orders 0by position: O(N) in the middleLTRIM app:feed:recent_orders 0 99keep only the newest 100LMOVE src dst LEFT RIGHTatomically move one element between listsLPOS app:feed:recent_orders order:5010position of an element: O(N)

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 1

Lists 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 30

Sets

A set holds unique strings with O(1) add, remove, and membership test.

SADD app:tags:sale 1001 1003 1006add members; duplicates are ignoredSISMEMBER app:tags:sale 10011: O(1)SMISMEMBER app:tags:sale 1001 10021 0SREM app:tags:sale 1006SCARD app:tags:salemember count: O(1)SMEMBERS app:tags:saleall members: O(N)SSCAN app:tags:sale 0iterate safelySRANDMEMBER app:tags:sale 2random members, without removingSPOP app:tags:saleremove and return a random member

Set algebra

The reason to choose sets over a list of IDs: server-side intersection, union, and difference.

SINTER app:category:peripherals app:tags:saleperipherals on saleSUNION app:category:audio app:category:displayseither categorySDIFF app:category:peripherals app:tags:saleperipherals NOT on saleSINTERSTORE tmp:result app:category:peripherals app:tags:salestore the result as a keySINTERCARD 2 app:category:peripherals app:tags:sale LIMIT 10just the count

Common uses: tagging and faceted filters, "friends in common" (SINTER user:1:friends user:2:friends), deduplicating events, tracking which users have seen something.

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

NeedUse
Latest N items, in orderList + LTRIM
Simple work queueList + BLMOVE, or better, a stream
"Is X in the group?"Set
Unique items, intersections, unionsSet
Unique items with an order or scoreSorted set
Count unique items, approximately, in 12 KBHyperLogLog

Lab

  1. LRANGE app:feed:recent_orders 0 4 and LLEN it.
  2. In one terminal, BRPOP app:queue:test 0 (blocks). In another, LPUSH app:queue:test hello. Watch the first terminal return.
  3. SINTER app:category:peripherals app:tags:sale. Which products are peripherals on sale?
  4. Create a set of 600 integers with a loop and check its encoding before and after the 512th.

On this page