Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 32: Synthesis and Future Directions

32.1 The Data Structure Spectrum

From fundamental to application-specific:

Abstraction Level
├── Fundamental: Array, List, Tree, Hash, Graph
│
├── Composite: Skip lists, Tries, Heaps, Bloom filters
│
├── Specialized: Segment trees, B-trees, LSM trees
│
├── Distributed: DHT, CRDT, Raft state machines
│
└── Application: Routing tables, Inverted indexes,
                Time series stores, Graph DBs

The layers are not just an organising convenience. each one is built by composing the layer beneath it, and tracing a structure down through the stack usually explains why it behaves the way it does.

A routing table is a trie is a tree is pointers into memory. An LSM tree is sorted runs plus a memtable plus Bloom filters, which are a bit array plus hash functions. A DHT is a hash function plus a ring plus a routing table plus failure detection. Nothing at the top of this diagram is novel at the bottom of it.

Read the other direction, the same diagram is a map of what changes as you ascend:

LevelThe binding constraintWhat you optimise
FundamentalCPU cycles, cache linesOperation count, locality
CompositeMemory footprintBits per element, pointer overhead
SpecializedDisk and page transfersI/O count, write amplification
DistributedNetwork round trips, partitionsCoordination avoided
ApplicationHuman requirementsThe right approximation

Complexity analysis is most useful at the top two rows and least useful at the bottom two. Nobody chooses between two distributed designs by comparing O(log n) to O(1); they compare round trips, failure modes, and what happens during a partition. The asymptotics have not become wrong, they have become the least interesting term.

32.2 Emerging Paradigms

Learned Data Structures

Machine learning models replacing traditional structures:

  • Learned indexes: Replace B-trees with neural networks predicting data positions
  • Learned cardinalities: Better statistics for query optimization
  • Learned compression: Adaptive compression based on data distribution

Neural B-tree:

Input: key
Output: predicted position + confidence interval

Training: Supervised learning on key distributions
Prediction: Binary search within confidence bounds

The reframing is genuine: an index is a function from key to position, and a model can approximate a function. A B-tree assumes nothing whatsoever about the key distribution, which makes it robust and also means it throws away information. Real key distributions (timestamps, auto-increment IDs, sorted identifiers) are highly regular, and a model that captures that regularity beats a structure that ignores it.

The honest status, five years on from the original paper: learned indexes remain mostly research, and the parts that shipped are elsewhere. Updates are the hard problem. The original design was read-only, and while ALEX and the PGM-index support updates, distribution shift can force retraining. Worst-case bounds disappear, which matters for anything adversarial. And “neural network” oversells it: the models that work are staged linear regressions, because inference has to cost nanoseconds.

What has landed from this line of work is less glamorous and more useful: learned cardinality estimation in query optimisers, learned cache-eviction policies, and learned Bloom filters. Chapter 19 covers the details.

Quantum Data Structures

Quantum computing offers new primitives:

  • Quantum search: O(√n) search (Grover’s algorithm)
  • Quantum random access memory (QRAM): Sub-linear access with superposition
  • Quantum fingerprints: Exponential space reduction for equivalence testing

Worth stating the caveats plainly, because this area attracts more enthusiasm than it currently earns.

Grover’s algorithm gives a quadratic speedup for unstructured search: O(√n) instead of O(n). That is real but modest, and it applies to unstructured search. A sorted array with binary search is already O(log n), which beats O(√n) comfortably. Grover helps where no structure exists to exploit, which is precisely the case where you would normally add an index.

QRAM is the deeper problem. Most quantum algorithms with impressive speedups assume a memory that can be queried in superposition, and no scalable QRAM has been built. The theoretical speedups are frequently accounted without the cost of loading classical data into quantum state, which can erase the advantage entirely.

The realistic near-term position: quantum computing will likely matter first for simulation, optimisation, and cryptography, not for data structures. The exception is cryptographic hashing: Grover halves the effective bit strength of a hash function, which is why post-quantum guidance recommends 256-bit hashes where 128 was sufficient. That is a live concern for Merkle trees and content-addressed storage.

32.3 The Road Ahead

Software-hardware co-design: As memory hierarchies deepen (NVM, CXL), data structures must adapt. Cache-oblivious structures gain importance.

Specialized accelerators: FPGAs and ASICs for network processing, search, and analytics push structure design toward hardware.

Declarative data structures: The boundary between algorithms and data structures blurs as query optimizers automatically choose structures based on workload patterns.

Two shifts already underway deserve to be added, because they are changing decisions today rather than eventually.

Vector search became infrastructure in about three years. Embedding models turned similarity search from a niche problem into a default component of application architecture, and the structures that serve it (HNSW graphs, IVF, product quantization) went from papers to production defaults faster than almost anything in this book’s history. Note what drove it: not a better algorithm, but a change in what data looked like. Structures follow workloads.

NVMe is quietly reopening settled questions. A great deal of received wisdom: B-trees over hash indexes on disk, LSM trees over in-place updates, “random I/O is catastrophic”. Was calibrated against spinning disks where a seek cost 10ms. On NVMe a random read costs about 10μs, a thousandfold improvement, and the gap between sequential and random access narrows from 10,000× to something closer to 10×. Several tradeoffs that were obvious in 2005 are now genuinely arguable. When the hardware assumptions under a piece of conventional wisdom change by three orders of magnitude, the wisdom deserves rechecking.

The general lesson across both: the structures that win are the ones that answer a question created by a hardware or workload shift. LSM trees won when write amplification on flash started to matter. HNSW won when embeddings created a new query type. Neither was primarily an algorithmic advance.

32.4 Principles for the Practitioner

  1. Measure before optimizing: Profile against real workloads
  2. Understand trade-offs: Every structure excels in some dimensions
  3. Prefer simplicity: Complex structures have hidden costs
  4. Plan for scale: Design for 10x growth
  5. Embrace approximation: Probabilistic structures often suffice
  6. Consider distribution: At scale, single-machine solutions fail
  7. Document assumptions: Workload characteristics drive structure choice

Four of those are worth sharpening, because as stated they are easy to agree with and hard to act on.

Measure the right thing. “Profile” usually means a wall-clock profiler, which tells you where time goes but not why. If a function is slow and its arithmetic is trivial, the answer is memory, and you need hardware counters to see it. An instructions-per-cycle figure below 1.0 with high cache misses means the layout is wrong and no algorithmic tuning will help: see Chapter 22.

Prefer simplicity, and mean it. The most common real-world mistake is not picking an O(n log n) structure where O(n) existed. It is picking a sophisticated structure whose constant factors, memory overhead, and bug surface exceed the benefit. Fibonacci heaps are the standing example: optimal on paper, beaten by binary heaps in practice, and vastly harder to get right. A linear scan over a contiguous array beats almost everything below a few hundred elements.

“Design for 10× growth” is a claim about which dimension grows. Ten times the data is a different problem from ten times the write rate, which is different again from ten times the concurrent readers. B-trees handle the first, LSM trees the second, immutable structures the third. Designing for unspecified “scale” produces systems that are complex in the wrong direction.

Embrace approximation, after checking the error direction. A Bloom filter’s false positives are safe as a cache filter and unsafe as an access-control check. The question is never just “is approximate good enough” but “what does a mistake cost, and which way does this structure make them” (Chapter 14).

Three more that this book has argued throughout:

  1. Identify the repeated question. Every algorithm asks one thing over and over. “which is nearest”, “have I seen this”, “would this create a cycle”. Name it, and the structure is usually obvious. Get it wrong and no optimisation will save you (Chapter 21).

  2. Abstract the interface, document the cost. A List backed by an array and one backed by a linked list have identical signatures and completely different performance. Hiding the layout is good design; hiding the cost is how O(n²) loops get written by accident.

  3. Write the invariant checker. For every structure there is a predicate that must hold after every operation. Written as code and asserted in debug builds, it converts a silent wrong answer a thousand operations later into a failure on the operation that caused it.

32.5 A Closing Thought

The through-line of these thirty-one chapters is that there are far fewer ideas here than there are structures. Almost everything in this book is one of a handful of moves, applied at a different scale:

  • Divide the space so most of it can be discarded: binary search, trees, tries, KD-trees, sharding.
  • Trade exactness for space: Bloom filters, HyperLogLog, sketches, learned indexes.
  • Trade space for time, or the reverse: indexes, caches, memoization, compression.
  • Batch the expensive thing: LSM trees, B-tree fanout, external sorting, request coalescing.
  • Share what didn’t change: persistent structures, copy-on-write, Git, snapshots, Merkle trees.
  • Randomize to defeat the adversary: skip lists, treaps, universal hashing, randomized pivots.
  • Never mutate; append and reference: logs, event sourcing, immutable collections, Kafka, Delta Lake.

Each of those appears at every level of the spectrum in §32.1, from cache lines to continents. A B-tree fans out to reduce disk transfers; a CDN hierarchy fans out to reduce origin requests; both are the same move against a different cost. Recognising which move a system is making is usually faster than learning the system.

New structures will keep arriving, and most will not last. The survivors are the ones answering a question that new hardware or a new workload just created. But they will be built from these same moves, because the moves are responses to constraints that have not changed: memory is a hierarchy, coordination is expensive, and you cannot have everything at once.

That last constraint is the one this book opened with, in Chapter 1: there is no free lunch. Every structure is fast at something because it agreed to be slow at something else. Knowing what a structure gave up is knowing when to use it.

Exercises

Difficulty: ★ check your understanding · ★★ apply it · ★★★ stretch.

32.1 ★ Name the move. For each technique, say which of the seven moves in §32.5 it makes, and in one sentence why: (a) Dynamo comparing Merkle trees to find divergent keys; (b) a database group commit that flushes the write-ahead log once for many transactions; (c) a Count-Min sketch for per-flow traffic; (d) Redis forking to write an RDB snapshot while the parent keeps serving writes; (e) a Chord finger table; (f) a Delta Lake checkpoint.

Solution

(a) Divide the space so most of it can be discarded. A matching subtree hash rules out every key below it, so the comparison only descends where differences are.

(b) Batch the expensive thing. An fsync costs about the same for one commit record as for a hundred, so paying it once per batch divides its cost among the transactions.

(c) Trade exactness for space. Fixed memory independent of the number of flows, in exchange for estimates that can only be too high.

(d) Share what didn’t change. After fork, parent and child share every memory page copy-on-write; only pages the parent modifies during the snapshot get copied.

(e) Divide the space so most of it can be discarded. Each hop jumps to the finger that most closely precedes the key, at least halving the remaining distance, so the lookup ignores most of the ring.

(f) Trade space for time. The checkpoint stores redundant state (everything the log already implies) so that readers can skip replaying the log from the start. It also sits on top of the “never mutate; append and reference” move that makes the log itself.

Several techniques make two moves at once, as (f) shows; naming the dominant one is the useful part.

32.2 ★ Put numbers on Grover. An unsorted table has n = 2^40 (about 10^12) records. Compare the expected number of probes to find one marked record by (a) linear scan, (b) Grover’s algorithm (about (π/4)√n oracle queries), (c) binary search after sorting. Then: how many Grover iterations does a preimage attack on a 128-bit hash need, and on a 256-bit hash?

Solution

(a) Linear scan: n/2 ≈ 5.5 × 10^11 probes on average.

(b) Grover: (π/4) × 2^20 ≈ 823,550 queries. A real speedup of more than five orders of magnitude, if a quantum memory holding the table existed (it does not; see the QRAM caveat).

(c) Binary search: log₂(2^40) = 40 comparisons, after a one-time sort. Grover loses to a classical index by a factor of about 20,000, which is the chapter’s point: Grover only competes where no structure can be built.

Hash preimages: Grover reduces a 2^k search to about 2^(k/2) iterations. A 128-bit hash falls to about 2^64, which is within reach of a large attacker, so it no longer counts as 128-bit secure. A 256-bit hash needs about 2^128, which keeps a comfortable margin. Hence the guidance to use 256-bit hashes for long-lived Merkle trees and content addresses.

32.3 ★★ Rechecking a settled question. Two designs serve a request. Design A does 2 random 4 KB page reads. Design B does 1 random read and then a 1 MB sequential read. Assume an HDD with 10 ms per random read and 200 MB/s sequential, and an NVMe SSD with 10 μs per random read and 3 GB/s sequential. Which design wins on each device, and by how much?

Hint

Compute the time for 1 MB of sequential transfer on each device first.

Solution

Sequential 1 MB: HDD 1/200 s = 5 ms; NVMe 1/3000 s ≈ 0.33 ms.

HDDNVMe
A: 2 random20 ms0.02 ms
B: 1 random + 1 MB sequential10 + 5 = 15 ms0.01 + 0.33 ≈ 0.34 ms

On the HDD, B wins by 25%: avoiding one seek is worth reading a whole megabyte. On NVMe, A wins by a factor of about 17: random reads are so cheap that reading extra data sequentially is now the expensive part.

Nothing about the algorithms changed. The ratio of random to sequential cost moved by orders of magnitude, and the preferred design flipped. Designs that read extra data to avoid seeks (large pages, wide sequential scans, aggressive prefetching) were tuned to the left column; that is the chapter’s “NVMe is quietly reopening settled questions” in two lines of arithmetic.

32.4 ★★ Which way does it err? For each use, name the structure’s error direction and decide whether it is safe: (a) a Bloom filter in front of an SSTable to skip disk reads for absent keys; (b) a Bloom filter of revoked session tokens, checked on each request; (c) a Bloom filter of users allowed into an admin panel; (d) billing each customer by a Count-Min sketch estimate of their API calls; (e) a HyperLogLog count of unique visitors on a public dashboard.

Solution

A Bloom filter has false positives (says “present” for an absent item) and never false negatives. A Count-Min sketch only overestimates. HyperLogLog has a symmetric relative error, typically around 1 to 2%.

(a) Safe. A false positive means one wasted disk read; a false negative cannot happen, so no stored key is ever missed.

(b) Safe for security. A revoked token is always caught. A false positive rejects a valid token, which costs a user a re-login. That is an availability cost, and the false-positive rate controls how often it happens.

(c) Unsafe. A false positive admits someone who is not on the list. The error falls on the side that matters, and no false-positive rate is low enough for access control.

(d) Unsafe as designed. Every error overcharges the customer, and colliding with a heavy customer can overcharge a light one by a lot. Bill from exact counts, and use the sketch for detecting heavy hitters or for alerting.

(e) Safe. A 1 to 2% error either way on a public visitor count costs nothing, and exact counting would need memory proportional to the number of visitors.

The same structure is right in (a) and (b) and wrong in (c): the question is never only “how often is it wrong?” but “which way, and what does that mistake cost here?”

32.5 ★★ Write the invariant checker. Principle 10 in §32.4 says to write a predicate that must hold after every operation. For a binary search tree whose nodes store size (the number of nodes in their subtree, used for order-statistic queries), write check(root) that asserts both the ordering invariant and the size invariant in one O(n) pass. Then show it catching an insert that forgets to update size on the way back up, on the first operation where the bug has an effect.

Hint

Checking only left.key < node.key < right.key at each node is not enough for the ordering invariant. Pass down the bounds that every key in a subtree must respect.

Solution
class Node:
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None
        self.size = 1  # number of nodes in this subtree


def size(node):
    return node.size if node else 0


def check(node, lo=None, hi=None):
    """Assert the BST and size invariants; return the subtree size."""
    if node is None:
        return 0
    assert lo is None or node.key > lo, f"{node.key} not > {lo}"
    assert hi is None or node.key < hi, f"{node.key} not < {hi}"
    n = 1 + check(node.left, lo, node.key) + check(node.right, node.key, hi)
    assert node.size == n, f"size of {node.key} is {node.size}, expected {n}"
    return n


def insert_buggy(root, key):
    if root is None:
        return Node(key)
    if key < root.key:
        root.left = insert_buggy(root.left, key)
    elif key > root.key:
        root.right = insert_buggy(root.right, key)
    return root  # forgot: root.size += 1 on the way back up


def insert(root, key):
    if root is None:
        return Node(key)
    if key < root.key:
        root.left = insert(root.left, key)
    elif key > root.key:
        root.right = insert(root.right, key)
    else:
        return root  # duplicate: nothing changes, so no size update
    root.size = 1 + size(root.left) + size(root.right)
    return root


root = None
for k in [50, 30, 70, 20, 40]:
    root = insert(root, k)
    check(root)
assert root.size == 5

bad = None
caught = None
for k in [50, 30, 70]:
    bad = insert_buggy(bad, k)
    try:
        check(bad)
    except AssertionError as e:
        caught = (k, str(e))
        break
assert caught == (30, "size of 50 is 1, expected 2")

check passes an open interval (lo, hi) down the tree: a node in the right subtree of 50 that sits in the left subtree of 70 must lie strictly between 50 and 70. Checking only parent against child would accept a 60 placed in the left subtree of 30, under 50’s left side, which is a broken tree. The same recursion returns the true subtree size, so verifying size costs nothing extra: one visit per node, O(n).

The buggy insert of 30 under 50 leaves 50’s size at 1, and check fails on that very insert with “size of 50 is 1, expected 2”. Without the checker, the tree still looks fine to every search and traversal; the bug would surface much later as a wrong answer from a rank or k-th smallest query, far from its cause. Run the checker in tests and debug builds only, since O(n) per operation would turn an O(log n) structure into an O(n) one.

32.6 ★★★ Ten times what? A profile service stores 10^8 user records in a B-tree with a fanout of about 200 per page, takes 5,000 writes per second, and serves 50,000 reads per second. For each of the three ways it can grow tenfold (data, write rate, concurrent readers), say what breaks first, what structural change you would make, and what that change costs. Quantify the data case.

Hint

For the data case, count B-tree levels: how many keys do 3 levels and 4 levels of fanout 200 address?

Solution

10× data (10^9 records). Three levels of fanout 200 address 200³ = 8 × 10⁶ keys; four levels address 1.6 × 10⁹. So 10⁸ and 10⁹ records both need 4 levels: tenfold data adds no extra level and no extra page reads per lookup. What does change is whether the upper levels stay in memory and whether the data fits on one machine. The structural change, if any, is sharding by key (consistent hashing or a fixed vnode map), which costs cross-shard queries and a rebalancing story. The B-tree itself handles this growth well, which is the chapter’s point.

10× writes (50,000 per second). In-place B-tree updates turn each write into random page writes plus WAL, and the write rate saturates the device or contends on hot pages. The change is an LSM tree: writes go to a WAL and an in-memory memtable, then are flushed as sorted runs, all sequential. The cost is read amplification (a lookup may check several levels, mitigated by Bloom filters), space amplification until compaction, and background compaction that competes for I/O and can cause latency spikes.

10× concurrent readers (500,000 reads per second). Readers contending with writers on locks, and one machine’s CPU and network, run out first. The change is to make reads not interfere with writes: MVCC or immutable snapshots, so a reader works on a fixed version with no locks, plus read replicas and caches to spread the load. The cost is staleness (a replica or snapshot can lag the latest write) and memory for keeping old versions until no reader needs them.

Each answer is a different structure, which is why “design for 10× scale” without naming the dimension gives no guidance. A system built for all three at once pays all three costs, even if only one kind of growth ever arrives.


Where this connects