Sub topic 3
Consistency trade-offs: CAP, PACELC and consistency models
This chapter assumes you know what active-active means, can read basic Python, and have used a relational or NoSQL database. It covers the theory you need before choosing a replication strategy. Later chapters apply it to replication, write ownership and conflict resolution.
Why consistency gets harder across regions
In a single region, a database can make every reader see the latest write because the nodes are close together. Between regions, the network adds tens to hundreds of milliseconds of delay. Two regions that both accept writes can also be cut off from each other.
Every multi-region data design must answer two questions:
- What does a user see when they read right after a write?
- What happens to reads and writes while regions cannot talk to each other?
CAP and PACELC are the vocabulary for these questions.
CAP in practical terms
The CAP theorem describes three properties of a distributed data store:
- Consistency (C): every read returns the most recent write, as if there were one copy of the data. This is linearizability, not the "C" in ACID.
- Availability (A): every request to a working node gets a non-error response.
- Partition tolerance (P): the system keeps working when messages between nodes are lost or delayed.
A network partition is a failure that splits nodes into groups that cannot reach each other. Across regions, partitions will happen, so P is not optional. The real choice applies only during a partition:
- CP: refuse some requests (usually writes in the minority side) to keep a single true history.
- AP: keep accepting requests on both sides and reconcile the differences later.
When there is no partition, a system can offer both consistency and availability. CAP says nothing about that normal case, which is where PACELC helps.
PACELC: the cost in normal operation
PACELC extends CAP: if there is a Partition, choose between Availability and Consistency. Else, when the system runs normally, choose between Latency and Consistency.
The "else" half matters most for active-active. To make a write visible everywhere before you acknowledge it, you must wait for a round trip to the farthest region. Acknowledging locally and replicating afterward is faster but leaves a window where other regions return old data.
| System style | During partition | Normal operation | Example behavior |
|---|---|---|---|
| PC/EC | Keeps consistency, rejects some requests | Keeps consistency, pays latency | Synchronous cross-region consensus |
| PA/EL | Keeps serving both sides | Favors low latency | Asynchronous multi-region replication with conflict resolution |
| PC/EL | Keeps consistency | Favors low latency | Single-writer primary with local replica reads |
Real products often let you choose per operation or per table, so treat these labels as a starting point and read the documentation for the exact guarantees.
Consistency models, from strongest to weakest
A consistency model is a promise about which values a read may return. These are the ones you will meet most often.
- Linearizable (strong) consistency. Operations appear to happen one at a time, in real-time order. A read after a completed write always sees it, in any region. Achieving this across regions requires coordination on each write.
- Sequential consistency. All clients see operations in one shared order, but that order need not match wall-clock time.
- Causal consistency. If operation B was caused by A (for example, a reply to a comment), everyone sees A before B. Unrelated operations may appear in different orders in different regions.
- Session guarantees. These apply within one client's session: read-your-writes (you see your own updates), monotonic reads (you never see data go backward in time), and monotonic writes (your writes apply in the order you made them).
- Eventual consistency. If writes stop, all replicas eventually hold the same value. It says nothing about what a read returns in the meantime.
Stronger models are easier to reason about and more expensive across regions. Many systems use eventual consistency globally and add session guarantees where users would notice a problem.
A worked example: stale reads after a write
The following simulation models two regions with asynchronous replication. A write lands in one region, and the other region sees it only after a delay. It shows the exact situation that read-your-writes protects against.
class Region:
def __init__(self, name):
self.name = name
self.data = {}
def write(self, key, value, version):
self.data[key] = (version, value)
def read(self, key):
return self.data.get(key, (0, None))
eu = Region("eu-west-1")
us = Region("us-east-1")
replication_queue = [] # (target_region, key, value, version)
def write_local(region, other, key, value, version):
region.write(key, value, version)
replication_queue.append((other, key, value, version)) # async: not applied yet
def replicate_all():
while replication_queue:
target, key, value, version = replication_queue.pop(0)
if version > target.read(key)[0]:
target.write(key, value, version)
write_local(eu, us, "profile:7", "dark-mode", 1)
print("EU read:", eu.read("profile:7")) # EU read: (1, 'dark-mode')
print("US read:", us.read("profile:7")) # US read: (0, None) stale
replicate_all()
print("US read after replication:", us.read("profile:7")) # (1, 'dark-mode')The stale US read is not a bug in this design. It is the price of acknowledging the write without waiting for the other region.
Making eventual consistency tolerable
You can often keep asynchronous replication and still avoid most user-visible surprises.
- Pin sessions to a region. Route a user to the same region for the length of a session, so their reads follow their own writes. If that region fails, accept that the user may briefly see older data after failover.
- Carry a version token. After a write, return the version to the client. On the next read, if the local replica is behind that version, wait briefly or read from the region that took the write.
- Read from the owner for critical paths. For a balance check before a payment, read from the region that owns the data, not the nearest replica.
- Keep stale data visibly harmless. Show "updated a moment ago" or design screens so a missing recent item does not break the flow.
Choosing per data type
Not all data in one application needs the same model. A common split looks like this:
| Data | Typical need | Common approach |
|---|---|---|
| Product catalog, content | Stale for seconds is fine | Eventual consistency, local reads |
| User preferences | Users expect to see their own changes | Session guarantees or region pinning |
| Counters, likes | Concurrent updates must not be lost | CRDT or merge-based replication |
| Account balances, inventory | No double spending | Single owner or synchronous consensus |
| Uniqueness (usernames, order numbers) | One global winner | Coordinated write or a single owning region |
Common errors
- Claiming "CA" for a multi-region system. Between regions you cannot opt out of partitions, so the choice is between CP and AP behavior.
- Confusing CAP consistency with ACID consistency. CAP's "C" is about all replicas agreeing on one history. ACID's "C" is about preserving database invariants.
- Ignoring the latency half. A design can be fine during partitions and still be too slow because every write waits on a cross-region round trip.
- Assuming "eventual" means "soon." Replication lag can grow into minutes under load or a degraded link. Measure it, because you will need that number later.
- Applying one model to everything. Strong consistency on a catalog wastes latency. Eventual consistency on a ledger loses money.
Summary of decisions
- Partitions are a given across regions. Decide in advance whether each piece of data fails closed (CP) or stays available and reconciles later (AP).
- In normal operation, every step toward stronger consistency costs cross-region latency.
- Use the weakest model that keeps the user experience and business rules correct, and strengthen only the data that needs it.
Exercise
An e-commerce service runs active-active in two regions with asynchronous replication. A customer adds an item to their cart in region A, the page reloads through region B, and the cart appears empty. Which consistency guarantee was violated, and name two ways to fix it.
Show answer
Read-your-writes (a session guarantee) was violated, because region B had not yet received the write from region A. Fixes: pin the user's session to one region so reads follow writes; return a version token after the write and make region B wait or read from region A if it is behind that version; or keep the cart in a store owned by a single region and read from it.
Exercise
A team says their multi-region database is "CA" because it is consistent and available in every test they run. What is wrong with that claim, and what should they ask instead?
Show answer
Their tests did not include a network partition, and partitions cannot be ruled out between regions. CAP only forces a choice during a partition, so they should ask what the system does when the regions cannot communicate: reject writes on the minority side (CP) or accept writes on both sides and reconcile later (AP). They should then test that behavior directly.
Exercise
Using PACELC, explain why acknowledging a write only after all regions have stored it improves consistency but hurts a user in a distant region.
Show answer
In the "else" case (no partition), PACELC makes you choose between latency and consistency. Waiting for every region adds at least one round trip to the farthest region to every write, so the write acknowledgment is slow, especially for the distant user. In return, no region can serve a stale read of that write. Acknowledging locally is faster but allows stale reads elsewhere until replication completes.