Core Concepts
CAP Theorem
Follow an inventory update and a feed read through a network split to see what consistency and availability mean for each.
The CAP theorem describes what a service with copies on several machines can promise during a network partition, when live machines cannot reliably exchange messages. The service cannot guarantee both that every live machine returns a valid result for the requests it receives and that reads and writes behave like operations on one copy in real-time order.
What CAP means
| Letter | Means here |
|---|---|
| Consistency | Operations behave as though there is one copy of the data. Each takes effect at one instant between request and response. A read starting after a completed write sees that write or a later one. This is linearizability. |
| Availability | Every live node that receives the operation eventually returns a valid result. An error caused by refusing to serve and an indefinite wait both give up this guarantee. |
| Partition tolerance | The design accounts for links failing while nodes remain alive. |
If a read overlaps a write, either the old or the new value can be valid, depending on how those operations can be ordered. Once the write has completed, a later read must see that write or a later one. CAP concerns the guarantees a design can make if its links split. While the links work, a design can offer both consistency and availability.
CAP availability is per operation during the split. The availability target for the whole service is measured over time or requests.
Gilbert and Lynch's CAP theorem · Herlihy and Wing on linearizability
The choice in practice
When to choose CP: inventory checkout
The right to sell the last item belongs to one warehouse. The other warehouse still has a copy of the old count when the link splits.
- Keep consistency (CP). The authorized warehouse can sell the item while it retains exclusive authority. The isolated warehouse asks its customer to retry. After the link recovers, that warehouse learns the new stock count.
- Keep availability (AP). Both warehouses can confirm a sale using their local count. Two customers could leave with a confirmation for one item. Reconciliation cannot fulfill both promises.
An inventory service that confirms purchases needs a rule that prevents two sides from selling the same unit.
When to choose AP: social feed
Two regions each hold a copy of a timeline. One region receives a new post while the link is down. A user then opens the app on the side that missed the update.
- Keep consistency (CP). The isolated region waits or refuses the feed read if it cannot establish the current value.
- Keep availability (AP). The isolated region answers using the posts it has. The feed remains usable, and the missed post can appear after the link recovers.
If both regions also accept likes, the design needs a rule to reconcile those changes. You can make that decision separately from how reads are served.
Watch CP vs AP
Follow the same three-node cluster through a split. A and B can still talk. C has clients but cannot reach either of them.
For a CP design, a common way to establish authority is a quorum. This means a set of nodes large enough to confirm a decision under the protocol. In this three-node example the quorum is two. C cannot confirm writes and must wait or refuse a read whose freshness it cannot establish. It may still have the old value locally.
For the AP feed read, both sides continue serving local copies. If a product also accepts updates independently on both sides, it needs a merge policy or a rule that chooses which update wins.
When to lean which way
For a seat booking or a transfer, a stale or conflicting success can break a promise the product made. For a restaurant listing or a recommendation feed, an older answer can still be useful. Name the particular operation. One product can confirm inventory through a single authority and serve its feed from local copies.
What the choice does to the design
A write acknowledgement from a replica answers a narrower question. It tells us a copy received the update. A CP design also needs a rule for which leader can serve during the split, which committed writes the next leader must preserve, and how reads see that history. Fencing prevents a former leader from making further writes after it loses authority. Waiting for a replica that may disappear from the next quorum leaves the ownership and read questions open. Data Replication covers the acknowledgement and failover mechanisms.
Even with healthy links, an operation can spend time waiting for remote replicas. PACELC names both trade-offs. During a partition (P), consider availability (A) and consistency (C). Else (E), consider latency (L) and consistency (C). Abadi's PACELC paper
Interview habit
- Name the operation and draw the split.
- At each live node, say whether a client gets a result, waits, or receives an error.
- For a result, state what the client is guaranteed to see. For a write, state who can commit it.
- Explain how the sides recover when communication resumes.
Common mix-ups
- Treating a healthy-network result as a guarantee for a partition.
- Calling a whole product AP because it uses local caches. Explain which live nodes can serve the named operation.
- Using a replica acknowledgement as proof that a failover preserves one committed history.
- Forgetting to reconcile updates accepted independently during the split.
See also
- Availability - how to measure acceptable service outages.
- Data Replication - how copies receive writes and how a replacement primary takes over.
- Scalability - what changes when a request depends on multiple nodes.
- Consistency Models - the read guarantees a client can observe.
- Idempotency - how to retry a request without applying its effect twice.
- Quorum (forthcoming) - when sets of replicas must agree and what that agreement establishes.
- Consensus Algorithms (forthcoming) - how replicas agree on an ordered history.
Used in
- Coding Interview Platform - editors can keep working during a disconnect. The shared document converges when their updates can travel again.
- Online Chess - a move needs an authorized game owner to preserve one sequence. A former owner must lose the ability to accept moves when a replacement takes over.
- Batched Inference Service - Redis
WAITimproves the chance a replacement primary has the result. That acknowledgement alone leaves failover and consistency guarantees open. Redis WAIT - Web Crawler (P2P) - the system needs an ownership rule for a key range when the peers that normally coordinate it cannot talk.
Done reading? Mark it so it sticks in your dashboard.