CRDTs
Most of the consistency tools in system design work by preventing conflicts. A single leader orders all writes. A lock makes everyone wait their turn. Consensus gets a majority to agree before anything is committed. These are correct and well understood, but they all need replicas to talk to each other before a write is accepted, and that costs latency and availability. A user in Sydney waits on a round trip to Virginia; a phone in airplane mode can't write at all.
CRDTs (Conflict-free Replicated Data Types) take the opposite approach: let every replica accept writes immediately, on its own, and design the data type so that any two replicas that have seen the same updates end up in the same state, no matter what order they saw them in. There are conflicts in the sense that people edit the same thing at once, but there's never a conflict the system can't resolve on its own, deterministically, without asking anyone.
That makes CRDTs the natural tool for a specific set of interview problems: collaborative editors (/system-design/google-docs, /system-design/excalidraw), offline-first apps, active-active multi-region databases, presence and counters that are written in many places at once. They also come up as a follow-up whenever you propose multi-leader replication: "what happens when two regions update the same record?" We'll build up:
- Why last-writer-wins isn't enough, and what "converge" really means.
- The two styles of CRDT, and the math property that makes merges safe.
- Counters, registers, sets and maps, with code.
- Sequence CRDTs for collaborative text, and how they compare with operational transformation.
- The costs (metadata, tombstones, invariants), and how to use CRDTs in an interview.
The Problem: Concurrent Writes on Different Replicas
Picture a shopping cart replicated in two regions, with users routed to the closest one. A user adds a book on their laptop (served by us-east) and, at the same moment, adds headphones on their phone (served by eu-west). Each region accepts its write locally, then replicates to the other.
What should the cart contain? Obviously both items. But if the cart is stored as one value and the system resolves conflicts with last-writer-wins (LWW), it compares timestamps, keeps whichever write is newer, and throws the other away. The user loses an item, and nothing ever reports an error.
LWW isn't always wrong. For a profile field like "display name," keeping the latest value is exactly what users expect. The problem is that LWW treats the whole value as one unit and discards information. A CRDT instead keeps enough information about each update that the merge can combine them meaningfully: the cart becomes a set where both adds survive.
The guarantee CRDTs provide is called strong eventual consistency:
- Eventual delivery: every update eventually reaches every replica.
- Convergence: any two replicas that have received the same set of updates are in the same state, immediately, with no further rounds of conflict resolution.
Plain eventual consistency only promises that replicas converge "eventually," often by some process that may need human help (like Git merge conflicts or Dynamo's siblings, which we'll come back to). Strong eventual consistency says convergence is automatic and deterministic.