Course map

Topic 140 / Advanced

Distributed state management

Chapter 8.6 · State at Scale

What and why

Every earlier topic in Part 8 is a technique for a specific state problem — replicas agreeing (→ 130–132), locks expiring (→ 137), writes conflicting (→ 134); this topic is the organizing question that comes before any of those techniques are chosen: for a given piece of state, where does it live, and what mechanism actually keeps each copy correct? Distributed state management is the deliberate placement of state as partitioned (each piece lives on exactly one owning node — → 73), replicated (the same piece lives on several nodes for durability and read capacity — → 72), or derived (a cache, index, or read model computed from some other system's state, never itself the source of truth — → 81). Builds on: → 73 (partitioning and sharding).

Diagram

Placement Where it lives What keeps it correct
Partitioned exactly one owning node per key the owning node's own transaction (→ 70)
Replicated several nodes, same data the replication protocol + chosen consistency model (→ 72, 129)
Derived computed from a source of truth a rebuild/refresh path; never written to directly

How it works

  1. For any piece of state, name its single source of truth first — the one place a conflicting value is resolved in that system's favor, by definition.
  2. If partitioned, correctness is local: the owning node's own transaction guarantees handle it, and the only distributed question is routing the right requests to the right owner (→ 73).
  3. If replicated, correctness depends on the consistency model chosen for it (→ 129) and the replication topology (→ 72) — a property of the whole replica set, not one node.
  4. If derived (a cache, a search index, a CQRS read model), it is never the authority — a bug in the derivation needs only a rebuild, not a recovery, provided the source of truth was never itself only the derived copy.
  5. The most common failure is accidentally treating a derived copy as a source of truth — once something downstream depends on a cache being right, and nothing can regenerate it, it has stopped being a cache.
  6. Mixed systems are normal: one order record might be partitioned by order id, have its inventory count protected by a lease (→ 137) during a hold, and appear in a derived search index updated asynchronously — each layer uses the mechanism matched to its own correctness need.

Practical example

(illustrative). A product's price lives in one partitioned, authoritative database row. A read-through cache (→ 83) holds a derived copy for fast reads, and a search index holds another derived copy for filtering by price range. If the cache and search index briefly disagree with the database after a price change, that's an expected, bounded staleness window (→ 85) — not data loss, because neither copy is the source of truth; if the authoritative row itself were lost, there would be nothing to rebuild either copy from.

When to use it

  • Use when: classifying any state as partitioned, replicated, or derived before choosing a specific technology.
  • Avoid when: letting a derived copy's lack of a write path quietly turn it into the only copy of some fact (a counter incremented only in a cache, never persisted).
  • Failure scenarios: a cache treated as durable, so an eviction becomes real data loss; a replicated set with no agreed consistency model, so different parts of a codebase assume different guarantees about the same data.
  • Simpler alternative: a single, partitioned, authoritative store with no replicas or derived copies, until a measured need (durability, read scale, query shape) requires them.

Practice

(Decision kind — judge scenarios and defend the choice; 15–20 min, no lab services). For 4 pieces of state in a food-delivery system (a restaurant's menu, a courier's current GPS location, an order's running total, a "popular dishes this week" list), classify each as partitioned, replicated, or derived, and justify by naming its source of truth. Done when every answer names a source of truth, and the "popular dishes" list is correctly identified as derived.

Key takeaway

Before choosing a distributed-state technique, classify the state as partitioned, replicated, or derived and name its source of truth — the classification determines which of Part 8's mechanisms applies. Done when you can: classify any piece of state in a described system and name what keeps it correct. Used next in → 141, 142.

References

Kleppmann and Riccomini, Designing Data-Intensive Applications — see references.md.

Source directory

Search the guide