Handbooks / System Design / Chapter 9
CAP, Consistency and Reliability
7 pages · ~5 min✓ Reviewed
Builds on Queues and Async Processing. Next up: Microservices and Service Communication.
CAP and PACELC Mental Model
1. CAP: CP vs AP
- Under a partition, pick Consistency (reads see the latest write) or Availability (every live node answers)
- Partitions are unavoidable, so the real choice is CP vs AP
- Partition heals: AP sides reconcile
| CP | AP | |
|---|---|---|
| Examples | etcd, ZooKeeper, HBase | Cassandra, DynamoDB default, Riak |
| Minority side | Rejects requests or errors | Keeps serving |
| Both sides | Only majority is correct | Diverge, then reconcile |
| Trade | Correct, may refuse | Always answers, may be stale |
2. PACELC
yesno
Strong vs Eventual Consistency
1. Strong vs eventual
| Strong (linearizable) | Eventual | |
|---|---|---|
| After a write returns | every later read sees it | reads can be stale |
| Replicas | coordinate across nodes | converge if writes stop |
| Cost | higher latency | low latency |
Quorums: R, W and N
1. The Three Numbers
- N = replica count
- W = acks needed for a write
- R = replicas consulted for a read
- Strong read rule: R + W > N, so the read set overlaps the latest write set
2. Tuning with N=3
| Setting | Reads | Writes | Failures |
|---|---|---|---|
Rate Limiting Algorithms
1. Purpose
- Cap requests per client per time window
- Protects a service from overload and abuse
- Reject with HTTP 429 plus a
Retry-Afterheader
2. The Algorithms
- Refillsteady rate
- Bucketup to capacity
- Requestspends 1 token
- Empty?reject with 429
Circuit Breakers and Retries with Backoff
1. Circuit Breaker States
- Closedcalls pass, failures counted
- Openthreshold hit: fail fast, no calls
- Half-Openafter timeout: few probes
- Probe succeeds → Closed; probe fails → back to Open
- Trip on failure-rate threshold over a rolling window
- Tune: open duration, probe count
- Fail fast with a fallback or cached response
2. Retry with Backoff
Availability Nines Cheat Sheet
1. Downtime per Nines
| Availability | Per year | Per month |
|---|---|---|
| 99% | 3.65 days | - |
| 99.9% | 8.76 hours | ≈ 43.8 min |
| 99.99% | 52.6 min | ≈ 4.4 min |
| 99.999% | 5.26 min | - |
Check yourself
N=5, W=2, R=3. A write is acked by replicas A and B. Can a read still return the old value?
- Yes: R+W = 5, which is not > 5.
- The read can hit C, D, E, and none of them saw the write.
- Fix: W=3, or R=4.
Three layers each try a call 3 times and the backend is down. How many calls hit the backend per user request?
- 27 (3 x 3 x 3), a retry storm.
- Retry at one layer only, with backoff and jitter.