Chapter 15: Systems Data Structures
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 ; the tokenizer needs a hash map tuned for small integer keys and a priority queue it can delete from; the router needs a hash that moves few keys when a replica joins. This chapter builds each of those structures from its call site, deriving what it must do before showing how it does it.
Concepts from other sections (32)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Block pool: one slab of fixed-size KV blocks handed out by id. Read more: 21.8 A paged KV block pool in C in AI Engineering
- LRU (least recently used): an eviction policy that reclaims the element whose last use is oldest. Read more: 15.3 An intrusive list and an LRU
- Radix tree: a trie whose single-child chains are merged into labelled edges. Read more: 15.7 A radix tree over token ids
- Bloom filter: an -bit array with hash probes per item, answering membership with no false negatives. Read more: 15.8 A Bloom filter
- Open addressing: storing every entry in the slot array itself, resolving collisions by probing other slots. Read more: 15.5 A Robin Hood hash map
- Linear probing: on a collision, trying the next slot, wrapping at the end. Read more: 15.5 A Robin Hood hash map
- Load factor (): entries divided by slots; here kept at or below 7/8. Read more: 15.5 A Robin Hood hash map
- Backward-shift deletion: moving the following run back one slot after a removal, instead of leaving a tombstone. Read more: 15.5 A Robin Hood hash map
- SWAR: SIMD within a register; testing all bytes of a 64-bit word with a few integer operations. Read more: 15.2 A Swiss table
- Binary heap: a complete binary tree in an array in which no child comes out before its parent. Read more: 15.6 A binary heap with lazy deletion
- Lazy deletion: marking an item dead and discarding it only when it reaches the root. Read more: 15.6 A binary heap with lazy deletion
- Bounded loads: a cap of per node, with over-cap keys walking clockwise to the next node below it. Read more: 15.9 A consistent hash ring with bounded loads
- Intrusive list: a linked list whose link fields live inside the elements, so linking needs no allocation. Read more: 15.3 An intrusive list and an LRU
- Geometric growth: multiplying the capacity by a constant factor when full, which makes appends amortized. Read more: 15.1 A growable array
- Block table: the per-sequence list of KV block ids, one per 16 tokens. Read more: 15.1 A growable array
- KV pool: the GPU memory left for KV cache after weights and activation headroom; it sets maximum concurrency. Read more: 20.4.6 Load tests and the deployment runbook in AI Engineering
- Control byte: one byte per slot holding EMPTY, DELETED, or the key’s 7-bit fingerprint. Read more: 15.2 A Swiss table
- Tombstone: a DELETED control byte that a probe must walk past. Read more: 15.2 A Swiss table
- cached block: a block with no running request on it that is still findable by hash or in the radix tree, and can be evicted. Read more: 23.5 Block manager with prefix cache (none, hash, radix) in AI Engineering
- logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0 in AI Engineering
- Robin Hood insertion: a carried entry takes the slot of any resident with smaller displacement. Read more: 15.5 A Robin Hood hash map
- Heapify (Floyd’s method): building a heap in O(n) by sifting down every internal node from the bottom up. Read more: 18.4 Binary heap
- Generation counter: a per-slot counter, copied into handles, that makes old handles mismatch after the slot is reused. Read more: 15.6 A binary heap with lazy deletion
- Trie: a rooted tree whose edges carry characters so that each node spells the string on its path. Read more: 12.2 Trees and tries, longest-prefix match in Mathematics
- Prefix cache: a map from token prefixes of past prompts to the KV blocks already computed for them. Read more: 21.4 A radix prefix cache in AI Engineering
- Double hashing: generating probes from two hashes. Read more: 15.8 A Bloom filter
- FNV-1a: a byte-at-a-time hash that XORs each byte in and then multiplies by a prime. Read more: 15.8 A Bloom filter
- SplitMix64: the mixer used to derive child seeds that depend only on the seed and the purpose. Read more: 12.3 Modular arithmetic, hashing, and PCG32 in Python in Mathematics
- Virtual node (vnode): one of a node’s many positions on the ring, used to even out arc shares. Read more: 15.9 A consistent hash ring with bounded loads
- Affinity routing: sending requests with the same prefix to the same engine so its prefix cache stays warm. Read more: 15.9 A consistent hash ring with bounded loads
- Consistent hashing: placing nodes and keys in one hash space so that a membership change moves only nearby keys. Read more: 15.9 A consistent hash ring with bounded loads
- Load balancer: a component that spreads traffic across instances of a service by round-robin, least connections, or consistent hashing, at layer 4 (TCP) or layer 7 (HTTP). Read more: Chapter 1: System Design in Systems Engineering
Every structure here has a caller in the system the books build. The hash maps index KV blocks and tokenizer merges, the heaps pick top- 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. The same ideas appear in three languages: C for the runtime (tl_vec, tl_map, the LRU, tl_topk_f32), Rust for the tokenizer and engine, and Go for the gateway. The interface is the contract; the language is a detail.
Two themes run through the chapter. First, 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, and the load factor decides when that bound breaks. Second, probabilistic structures trade certainty for space: a Bloom filter never misses a member and is wrong about non-members at a rate you choose, and a consistent hash ring trades exact balance for minimal movement, then wins balance back with a load cap.
Learning objectives
- Build open-addressing hash tables (Robin Hood with backward-shift deletion, and a Swiss table with SWAR control-byte matching) and explain how each bounds probe length at load 7/8.
- Build binary heaps for a stable priority queue with lazy deletion and for allocation-free top- selection.
- Build allocation-free and failure-atomic containers in C: a growable array and an intrusive LRU list.
- Build a radix tree over token ids with an index-linked LRU of unlocked leaves for prefix caching.
- Size a Bloom filter from and , and route keys with a consistent hash ring under bounded loads.
Before you begin
Section titled “Before you begin”You should be comfortable with the language primers in Programming and Algorithms: C, Rust, and Go. Load factors and hashing probability come from Discrete Math 2 in Mathematics. The interview-pattern chapters of this book, Arrays and Hashing, Trees, and Probabilistic Structures, are good warm-ups; bloom.py in the last of these is the worked example behind the Bloom filter section. The practice C drills 01 to 04 build linear probing and an intrusive list as baselines.
Try to implement each structure from its section before reading the reference design: Abseil’s notes for Swiss tables, SGLang’s paper for the radix tree.
In this chapter
Section titled “In this chapter”The sections are numbered by where they sit in the C runtime, but two of them are foundations for the rest. A Robin Hood hash map is the chapter’s first hash-table section, developing hashing, probing, and the load factor from first principles, and A binary heap with lazy deletion is its first heap section. If these ideas are new to you, read those two first; the Swiss table and C top- build on them.
- A growable array. The type-erased C array
tl_vec, with geometric growth for amortized appends and a failure-atomic growth order; it holds each sequence’s block table in the KV pool. - A Swiss table. The C map
tl_mapfrom u64 to u64: 7-bit fingerprints in control bytes matched 8 at a time with SWAR, load 7/8, and tombstones only where a probe may have passed. It is the KV pool’s prefix-hash index. - An intrusive list and an LRU. Links embedded in each element and a sentinel head give touch and evict with no allocation, for the pool’s evictable cached blocks.
- Heap top-k in C. An optional standalone C unit: a size- min-heap over logits in the caller’s buffers, with ties to the lower index and parity with the Python sampler through shared fixtures.
- A Robin Hood hash map. The Rust map behind the tokenizer’s vocabulary and merge table: Fx hashing from the top bits, Robin Hood insertion, early-exit lookup, and backward-shift deletion.
- A binary heap with lazy deletion. A stable Rust heap with heapify, plus handles and generation counters for removal; the BPE merge queue and the engine’s waiting queue.
- A radix tree over token ids. A compressed trie in granules of one KV block, with
match_prefixreturning the longest cached prefix, locks that pin running requests, and an index-linked LRU of leaves. It is the radix prefix cache. - A Bloom filter. bits and hashes, double hashing from FNV-1a and SplitMix64, and a portable byte format shared with the corpus pipeline’s exact-dedup screen.
- A consistent hash ring with bounded loads. A Go ring with virtual nodes where no replica takes more than keys; it drives the gateway’s affinity routing.
The engine flag --prefix-cache=hash|radix gives both the Swiss table (hash index in C) and the radix tree (in Rust) a production call site, and Block manager with prefix cache in AI Engineering benchmarks one against the other.
Connections to other books
Section titled “Connections to other books”These structures are used throughout the other books:
- The KV block pool and the prefix cache in Inference in AI Engineering use the growable array, the Swiss table, the LRU, and the radix tree.
- The Gateway chapter of AI Engineering uses the bounded-load ring for affinity routing.
- The Corpus Pipeline chapter of Data Engineering uses the Bloom screen in exact dedup.
The same designs run in production: Google’s Abseil makes Swiss tables the default C++ hash map; SGLang and vLLM keep radix and hash prefix caches over paged KV blocks; and Akamai and Vimeo use consistent hashing, including the bounded-load variant, in production load balancers.
Further reading
Section titled “Further reading”- Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms (4th ed.), chapters 6 (heaps) and 11 (hash tables).
- Abseil, Swiss Tables design notes (free).
- Celis, Robin Hood Hashing (1986).
- Bloom, Space/Time Trade-offs in Hash Coding with Allowable Errors (1970).
- Mirrokni, Thorup, and Zadimoghaddam, Consistent Hashing with Bounded Loads (free).
- Zheng et al., SGLang: Efficient Execution of Structured Language Model Programs (RadixAttention, free).