Consistency, CAP and distributed transactions
As soon as data lives on more than one machine (replicas, shards, services), a hard question appears: does every reader see the latest write? This lesson explains consistency models, the CAP and PACELC theorems, quorums, and how to keep data correct across services without a single database transaction.
Consistency models
| Model | Guarantee | Example |
|---|---|---|
| Strong (linearizable) | Every read sees the latest completed write, as if there were one copy | A bank balance, inventory for the last item, a username being free |
| Read-your-writes | You always see your own changes | Editing your profile |
| Monotonic reads | You never see data go "back in time" | Refreshing a feed |
| Causal | If B depends on A, everyone sees A before B | A reply never appears before the question |
| Eventual | If writes stop, all replicas eventually agree | Like counts, view counts, recommendations |
Stronger guarantees need more coordination between machines, which costs latency and availability. Good designs choose the weakest model each feature can tolerate.
The CAP theorem
In a distributed system, a network partition (machines unable to reach each other) will eventually happen. When it does, you must choose:
- Consistency (C): refuse requests that can't be answered with up-to-date data; or
- Availability (A): answer anyway, possibly with stale data, and reconcile later.
You can't have both during a partition (P isn't optional). So systems are described as CP (e.g. ZooKeeper, etcd, Spanner, a single-primary SQL database refusing writes it can't replicate) or AP (e.g. Cassandra and DynamoDB in their default modes, DNS, shopping carts that merge).
PACELC extends this: if there's a Partition, choose A or C; Else, in normal operation, choose between Latency and Consistency, because waiting for replicas costs time even when nothing is broken.
Quorums
Replicated databases such as Cassandra and DynamoDB keep N copies of each item. A write succeeds once W replicas confirm it; a read asks R replicas and takes the newest answer. If R + W > N, every read set overlaps every write set in at least one replica, so reads see the latest write:
Tuning W and R trades latency against consistency: W=1, R=1 is fastest but eventual; W=2, R=2 with N=3 is the classic balanced, consistent setting that still tolerates one replica being down.
Conflicts in AP systems
If two replicas accept different writes to the same item during a partition, they must be reconciled later:
- Last write wins (LWW): keep the write with the latest timestamp. Simple, but silently drops one update, and clocks drift.
- Merge application data: a shopping cart takes the union of items.
- CRDTs (conflict-free replicated data types): counters, sets and text structures designed so concurrent updates always merge correctly. They power collaborative editors and offline-first apps.
Transactions across services
Inside one database, a transaction makes several changes atomic. When an order touches the order service, payment service and inventory service, each with its own database, there's no single transaction.
Two-phase commit (2PC) coordinates participants (prepare, then commit), but blocks when the coordinator fails and couples services tightly, so it's rarely used between microservices.
The common alternative is a saga: a sequence of local transactions, each with a compensating action that undoes it if a later step fails:
Sagas give eventual consistency: for a moment, stock is reserved for an order that will be cancelled. Design for that (show "processing" states), make every step and compensation idempotent (they'll be retried), and use the transactional outbox so each step's event is published reliably.
Coordination services
Some problems genuinely need agreement between machines: electing a leader, distributed locks, configuration that everyone must see identically. Consensus algorithms (Raft, Paxos) solve them, and systems like etcd and ZooKeeper (and Kubernetes, which uses etcd) package them up. Use them rather than inventing your own: consensus is notoriously subtle.
Practice
- With N = 5, list the (W, R) pairs that guarantee reading the latest write. Which tolerates the most failed replicas for reads?
- Add a "send confirmation email" step to the saga. What should its compensating action be, and why is that awkward?
- For each feature, choose a consistency model: seat booking, a like counter, chat message order, a user's display name.
1.According to CAP, what must a system choose during a network partition?
2.With N = 3 replicas, which quorum guarantees reads see the latest write?
3.What is a saga?