System Design on consistency and transactions
A transaction is all or nothing, in one database
- Atomic: every change in the group happens, or none does.
- Isolated: transactions running together do not see each other's half-done work.
- Durable: once committed, the change survives a crash.
- This is easy inside one database and hard across two.
Strong consistency acts like a single copy
- Once a write finishes, every later read sees it, whichever replica answers.
- The formal name is linearizability. It needs coordination, which costs latency.
- Use it where a wrong answer costs money: balances, stock, unique names.
Eventual consistency lets replicas catch up later
- A write is accepted quickly and reaches every replica eventually.
- Until then, two readers can see different values.
- Fine for click counts on a short link. Not fine for who owns a custom link name.
In a network split, choose consistent or available
- A partition is when machines cannot reach each other. Networks do this.
- CAP theorem: during a partition, a node must refuse requests or risk wrong answers.
- You cannot opt out of partitions, so “pick two of three” is misleading.
- The choice can differ per feature inside the same system.
Without a split, you trade latency for consistency
- PACELC adds the normal case to CAP: else, choose latency or consistency.
- Waiting for replicas to confirm a write makes it consistent, and slower.
- Abadi argues this everyday trade shaped databases more than CAP did.
Quorums tune the trade per request
- Keep N copies. A write waits for W of them. A read asks R of them.
- If R + W > N, every read reaches at least one copy that has the latest write.
- Dynamo's common setting is N=3, W=2, R=2: one node can be slow or down.
| N, W, R | What you get |
|---|---|
| 3, 2, 2 | Balanced. Reads overlap the latest write. |
| 3, 3, 1 | Fast reads. Writes wait for every node. |
| 3, 1, 1 | Fastest. R + W is not above N: stale reads. |
A saga replaces a transaction across services
- Each service commits its own local transaction, then triggers the next step.
- If a step fails, compensating actions undo the earlier ones, like a refund.
- There is no isolation: others can see the half-finished state. Design for that.
All steps succeed
- Reserve stock
- Take payment
- Ship order
Payment fails
- Payment fails
- Release stockthe compensating action
- Order canceled