Skip Lists: The Data Structure Behind Redis Sorted Sets

Redis sorted sets are backed by a skip list (plus a hash map for O(1) member existence checks). Understanding why explains the O(log N) inserts and fast range reads that make the timeline service work.

What a skip list is

A skip list is a probabilistic, multi-level linked list. The bottom level is a plain sorted linked list of all elements. Each higher level is a "fast lane" that skips over many elements, like express lanes on a highway.

level 3:  [1] ──────────────────────────────────────► [9001] ──► NULL
level 2:  [1] ──────────────► [5000] ───────────────► [9001] ──► NULL
level 1:  [1] ──► [2000] ──► [5000] ──► [8000] ──► [9001] ──► NULL
               (bottom level = full sorted list)

When you insert a new element, you flip a coin repeatedly to decide how many levels it gets promoted into. On average this keeps the structure balanced — O(log N) levels, O(log N) nodes per level.

Insert: O(log N)

To insert score 6000:

1. Start at the top level, scan right until you overshoot (6000 < 9001) → drop down
2. Level 2: scan right, 6000 > 5000, move forward → 6000 < 9001, drop down
3. Level 1: 6000 > 5000, move forward → 6000 < 8000, insert here
4. Randomly promote the new node to higher levels

level 2:  [1] ──────────► [5000] ──► [6000] ──► [9001]
level 1:  [1] ──► [2000] ──► [5000] ──► [6000] ──► [8000] ──► [9001]

Each level halves the search space, so total work is O(log N) — same as binary search on a sorted array, but without needing contiguous memory.

Range read: O(log N + K)

ZREVRANGE 0 19 (fetch newest 20):

  1. O(log N) — find the start position (the highest-score element) by descending levels
  2. O(K) — walk the bottom level left-to-right for K entries

The bottom level is a plain linked list, so the scan is a sequential pointer walk — very cache-friendly. This is the key advantage over a balanced BST, where an in-order traversal jumps around memory unpredictably.

Why Redis chose a skip list over a balanced BST

Skip list Balanced BST (red-black tree)
Insert O(log N) O(log N)
Range read O(log N + K) — sequential walk O(log N + K) — in-order traversal, pointer-chasing
Cache locality High (bottom level is contiguous-ish walk) Low (tree pointers scatter across memory)
Implementation Simple — no rotations or rebalancing Complex — rotations, color flips, rebalancing
Lock-free variants Easier to reason about Hard

Same asymptotic complexity, but skip lists have better constant factors for the range-heavy workload (ZREVRANGE is the hot path for every timeline read) and are far simpler to implement correctly.

The hash map's role

Redis sorted sets maintain two data structures internally:

  • Skip list — ordered by score; supports range queries and rank operations
  • Hash map — maps member → score; supports O(1) single-member lookup (ZSCORE) and de-duplication on insert

When you ZADD home_timeline:42 score=9001 member=9001, Redis updates both. The hash map is what makes re-delivering the same tweet ID idempotent — it can check in O(1) whether the member already exists.

Why this matters for the timeline at scale

  • 800 entries per user × 200M users — each individual ZSET is small (N=800), so O(log 800) ≈ 10 operations per insert. The cost is the number of ZADDs (one per follower), not the depth of any individual set.
  • ZREVRANGE is a hot-path sequential scan — reading the top 20 tweets is 10 pointer hops to find position + 20 sequential steps along the bottom level. Fast and predictable.
  • Trimming is O(log N + K) — ZREMRANGEBYRANK to evict old entries uses the same skip-list traversal, cheap on a 800-element set.