CAP y PACELC
Teorema CAP
Escrito por Darjus Kazakevic · revisado el
During a partition you choose; without one, PACELC says you are still choosing.
CAP is often quoted as “pick two of three”. That is misleading. A partition (nodes that cannot reach each other) happens in every real network, so a distributed system cannot opt out, and “CA” is not a real choice. The question is what happens during a partition. Does each side keep answering and risk different values (AP)? Or does the side without a majority refuse, to stay consistent (CP)?
PACELC adds the normal case, when there is no partition. Then you trade latency against consistency, because waiting for replicas to agree costs a round trip on every write. Within one region that is about a millisecond. Across regions it can be 100 ms.
En esencia — A replicated store gives up consistency or availability during a network split, and still trades latency for consistency when there is none.
Cuándo recurrir a él
Recurre a él cuando
- Data is replicated across nodes or regions that can lose contact
- Deciding which writes refuse and which diverge when quorum is lost
- Choosing a database for data that spans several regions
Reconsidéralo cuando
- A single-node database with no replicas — there is nothing to split
- A cache you can rebuild, where a stale answer is always acceptable
- Picking a database by its CAP label alone — check per operation
Conceptos clave
- Partition
- Nodes are running but cannot reach each other: a broken link, a bad switch, a long garbage-collection pause. To each side, the other looks dead. You cannot avoid partitions, only decide how to behave during one.
- CP (Consistent under partition)
- Nodes that cannot reach a majority refuse reads and writes. The data stays correct, but some clients get errors until the network heals.
- AP (Available under partition)
- Every node keeps answering, even when it cannot reach the others. Copies may differ and must be merged later, with last-write-wins, version vectors or CRDTs (data types that merge without conflicts).
- PACELC
- If Partition, choose Availability or Consistency; Else, choose Latency or Consistency. The second half describes every normal day: a store that waits for far replicas is consistent but slower.
- Quorum
- How many replicas must answer. With N copies, writes wait for W and reads ask R. If W + R > N, every read reaches at least one copy that has the latest write.
Reglas de diseño
- Declare each write's partition behaviour
- Decide per operation whether it refuses, or accepts and reconciles, when it cannot reach a quorum. Money refuses rather than diverges; a like counter diverges and merges.
- Price the everyday latency
- Without a partition you still pay: waiting for a majority on every write is consistency bought with latency. Make that choice per write too, not once for the whole store.
- Test with a real partition
- Behaviour you have never seen is behaviour you are guessing. Block traffic between nodes with a firewall rule or a chaos tool, and check what each operation returns and whether data diverges.
- Keep CAP's C apart from ACID's C
- CAP's C means every read sees the latest write across copies. ACID's C means a transaction keeps the data valid. A store can have one without the other.
Errores habituales
- Calling a whole database CP or AP. Many let you choose per request, such as Cassandra's consistency level.
- Assuming partitions are rare. Long GC pauses, overloaded switches and cloud network faults cause them regularly at scale.
- Last-write-wins with clocks that disagree. A write with an earlier timestamp silently loses, and data disappears.
- AP for data with a rule, such as stock that must not go below zero. Both sides of the split sell the last item.