Skip to content

Systems Data Structures

  • Every structure here has a caller in the system you build. The hash maps index KV blocks and tokenizer merges, the heaps pick top-k tokens and the next request, the radix tree finds shared prompt prefixes, the Bloom filter screens duplicate paragraphs, and the ring routes requests to replicas.
  • Open addressing is a probe-length problem. Linear probing, Robin Hood, and Swiss tables differ in how they bound the worst probe, not the average one; the load factor decides when that bound breaks.
  • The same idea appears in three languages: C for the runtime (tl_vec, tl_map, the LRU, tl_topk_f32), Rust for the tokenizer and engine, Go for the gateway. The interface is the contract; the language is a detail.
  • Probabilistic structures trade certainty for space: a Bloom filter never misses a member and is wrong about non-members at a rate you choose.

Implement each structure from its chapter before reading the reference design (Abseil’s notes for Swiss tables, SGLang’s paper for the radix tree). The interview-pattern chapters in this track (01 Arrays & Hashing, 05 Trees, 15 Probabilistic Structures) are the warm-ups. In the course, ds.05 is the first hash-table chapter and ds.06 the first heap chapter; the C versions in Pass 6 build on them.


A data structure in a production system is chosen by its worst case under the system’s access pattern, not by its textbook complexity. The KV block pool needs a hash index that never stalls on a rehash and an LRU that evicts in O(1); the tokenizer needs a hash map tuned for small integer keys; the router needs a hash that moves few keys when a replica joins. Each chapter starts from the call site and derives the structure it needs.

Key ideas:

  • Robin Hood (ds.05, Rust): on insert, a key that has travelled further from its home slot steals the slot; deletion shifts back instead of leaving tombstones. Called by the L1.5 tokenizer for vocab and merge ranks.
  • Swiss table (ds.02, C): control bytes with 7 bits of the hash, matched 8 or 16 at a time (SWAR), load factor 7/8. Builds on the practice c/02 linear-probing baseline; called by the rt.04 prefix-hash index and KV-transfer dedup.

Key ideas:

  • tl_vec (ds.01): a type-erased growable array; the block tables of rt.04.
  • Intrusive list and LRU (ds.03): O(1) move-to-front and evict through embedded links; evictable cached KV blocks.

Key ideas:

  • Lazy-deletion heap (ds.06, Rust): generation counters invalidate stale entries; the BPE merge queue and the engine’s waiting queue.
  • Top-k (ds.04, optional C): a standalone size-kk min-heap over logits with ties to the lower index; parity uses fixture files from the Python reference.

Key ideas:

  • Radix tree over token ids (ds.07): match_prefix returns the longest cached prefix; locked nodes are never evicted. The L8.4 prefix cache.
  • Bloom filter (ds.08): m=−nln⁡p/(ln⁡2)2m = -n \ln p / (\ln 2)^2 bits and k=(m/n)ln⁡2k = (m/n) \ln 2 hashes for nn items at false-positive rate pp. The data.03 exact-dedup screen.
  • Consistent hashing with bounded loads (ds.09, Go): virtual nodes on a ring, and no replica takes more than ⌈c⋅avg⌉\lceil c \cdot \text{avg} \rceil keys. Gateway affinity routing in gw.05.
ModuleTopicKindPass
ds.05Robin Hood hash map (backward-shift delete); the first hash-table chapter (hashing, probing, load factor from first principles)build3
ds.06Binary heap with lazy deletion (generation counters); the first heap chapterbuild3
ds.08Bloom filterbuild3
ds.01Growable array tl_vec (type-erased)build6
ds.02Swiss table tl_map (u64 to u64); practice c/02 linear probing is the worked baseline; builds on the ds.05 chapter (the first hash-table chapter)build6
ds.03Intrusive list + LRUbuild6
ds.04Binary heap top-k tl_topk_f32 (ties: lower index wins); builds on the ds.06 chapter (the first heap chapter)build6
ds.07Radix tree over token ids with index-linked LRU leaf listbuild6
ds.09Consistent hash ring with bounded loadsbuild7

The engine flag --prefix-cache=hash|radix gives both ds.02 (hash index in C) and ds.07 (radix tree in Rust) a production call site; L10.4 benchmarks one against the other.

#ModuleChapterKindPass
1ds.01Growable array tl_vec (type-erased, optional C)side6
2ds.02Swiss table tl_map (u64 to u64, optional C)side6
3ds.03Intrusive list + LRU (optional C)side6
4ds.04Binary heap top-k in C (optional)side6
5ds.05Robin Hood hash map with backward-shift deletionbuild3
6ds.06Binary heap with lazy deletionbuild3
7ds.07Radix tree over token ids with index-linked LRU leaf listbuild6
8ds.08Bloom filterside3
9ds.09Consistent hash ring with bounded loadsbuild7
TrackConnection
Arrays & Hashingthe interview-pattern warm-up for the hash tables
Probabilistic Structuresbloom.py is the worked example for ds.08
tinyllm Part 8the KV block pool (rt.04) and the prefix cache (L8.4)
Gatewayaffinity routing over the bounded-load ring
Corpus Pipelinethe Bloom screen in exact dedup
CompanyPractice
Google (Abseil)Swiss tables as the default C++ hash map
SGLang, vLLMradix and hash prefix caches over paged KV blocks
Akamai, Vimeoconsistent hashing, and the bounded-load variant in production load balancers