Skip list
A sorted linked list with extra layers of "express lane" pointers, so a search skips over many elements at a time and runs in O(log n). It exists because it gives balanced-tree performance with a far simpler implementation, no rebalancing, and natural support for ranges, which is why Redis sorted sets and LSM memtables use it.
What it costs
| Operation | Cost | Note |
|---|---|---|
| Lookup by key / score | O(log n) expected | Start at the top layer, move right while the next key is smaller, drop a layer, repeat. |
| Insert | O(log n) expected | Find the position, pick a random height (coin flips), splice into each layer up to that height. No rebalancing. |
| Delete | O(log n) expected | Unlink from every layer the node appears in. |
| Range by rank or score | O(log n + k) | Find the start, then walk the bottom layer. With span counts per pointer, "the 800th element" is also O(log n). |
Memory: About 1.33 pointers per element on average with a 1/4 promotion probability, so roughly 1.3× the footprint of a plain linked list; Redis stores each sorted-set member once plus 16 bytes of score and pointers.
Four layers over one sorted list
Searching for score 42 starts at the top layer and moves right while the next node is smaller, dropping a layer each time it overshoots. Every element is in layer 0, so a range query walks the bottom row.
How it works
- Layer 0 is an ordinary sorted linked list holding every element. Each element also has a random height: with probability 1/4 it gets a pointer in layer 1, with probability 1/16 in layer 2, and so on. Higher layers are sparser lists over the same elements.
- A search starts at the top-left. At each node it looks at the next pointer in the current layer: if that key is still less than the target, move right; otherwise drop down one layer and repeat. At layer 0 the next node is either the target or the insertion point.
- An insert runs the same search, remembering the last node visited in each layer. It then flips coins to pick a height and splices the new node in after each remembered node up to that height. Nothing else in the structure moves, which is why there is no rebalancing step.
- A delete runs the search and unlinks the node from each layer where it appears.
- For rank queries each pointer additionally stores how many layer-0 elements it skips (its span). Summing spans while descending gives the rank of any element in O(log n), and "give me elements 100 to 120 by rank" becomes a descent plus a short walk.
Why it is shaped this way
- The expected number of nodes visited per layer is constant and the number of layers is about log base 4 of n, so searches are O(log n) without ever measuring or fixing balance.
- Because every element is in the layer-0 list, range scans are just pointer walks, and because inserts only touch neighbouring pointers it is easy to make concurrent and lock-free.
Worked example
Timeline cache in the news feed, one Redis sorted set per user
- Each user’s home timeline is a sorted set of up to 800 post ids scored by created_at. 400M users × 800 entries × about 40 B each (8-byte id, 8-byte score, pointers) = 12.8 TB across the cache fleet.
- Reading the first page: ZREVRANGEBYSCORE from +inf down, limit 20. The search descends about log4(800) ≈ 5 layers to the top score, then walks 20 nodes along layer 0: about 25 pointer hops, a few microseconds.
- A fan-out worker inserting a new post for a follower with 800 entries: one descent to find the position by score (5 hops), splice into 1–2 layers on average, then ZREMRANGEBYRANK to drop the oldest entry if the set is over 800: another descent using spans. Two O(log n) operations, well under 10 µs.
- Why not a balanced tree: Redis needs range by score, range by rank, and reverse iteration; the skip list gives all three with one simple structure, and a hash on the side gives O(1) score lookup by member.
- The LSM memtable in the chat store is the same structure: 128 MB of (conversation_id, seq) keys kept sorted as they arrive, so a flush writes them out in order with a single walk of layer 0.
When it is the wrong choice
- On-disk indexes: a skip list’s pointers are scattered across memory, which is fine in RAM but means one disk read per hop; a B-tree packs hundreds of keys per page for exactly this reason.
- Pure key lookups with no ordering or ranges: a hash table is O(1) and smaller.
- Memory-tight environments where 30% pointer overhead matters and the data is static: a sorted array with binary search has zero overhead.
Interviewer follow-ups
Answer out loud before you open each one.
How does it get O(log n) without any balancing?
Heights are random: each element has a 1/4 chance of reaching the next layer, so layer k holds about n / 4^k elements and there are about log4(n) layers. Within one layer a search visits a constant expected number of nodes before dropping down. Randomness gives balance in expectation instead of by rule.
Why do Redis sorted sets use a skip list and a hash table together?
The skip list provides ordering: range by score, range by rank, reverse scans. The hash table maps member to score in O(1), so ZSCORE and the lookup needed before an update do not walk the list. Each member is stored once; the two structures share the pointer.
How does ZRANGE by rank find the 800th element in O(log n)?
Each forward pointer stores a span, the number of layer-0 elements it jumps over. While descending, the search adds spans as it moves right and stops when adding the next span would exceed the target rank. The sum of spans is the rank, so position queries cost the same as key queries.
Why is a skip list a good memtable for an LSM tree?
Inserts are O(log n) with no rebalancing, concurrent inserts can proceed with fine-grained or lock-free updates to neighbouring pointers, and flushing the memtable is a single walk along layer 0 that emits keys already in sorted order.
Where it shows up
- Building blocks
- Precompute on write, read cheaply · Page results with a cursor · Run work later or on a schedule · Write-optimised vs read-optimised storage · Rank instead of sorting by time · Track online status cheaply
- Problems
- News Feed (Twitter / X timeline) · Chat App (WhatsApp / Messenger) · Notification System