# CAP or PACELC: what trade-off do you make when nothing is broken?

By Vishal Kumar · Published 2026-11-04 · Updated 2026-11-13
Source: https://fungyaan.com/p/cap-pacelc
Tags: databases, distributed-systems, postgres, replication, consistency

**The verdict, which we'll earn with a real replica:** **CAP** says that when the network between copies of your data breaks (a *partition*), each copy must either stop answering (keep **C**onsistency) or answer with possibly stale data (keep **A**vailability). That's real, but partitions are rare. **PACELC** adds the trade-off you make on every request: **E**lse, when nothing is broken, you choose between **L**atency and **C**onsistency. Waiting for a replica to confirm a write makes every write slower. Not waiting means reads from that replica can be stale.

The case in one sentence: on a PostgreSQL primary and replica, reading your own write from an asynchronous replica right after committing returned the old value **99.7-100%** of the time. Making the commit wait until the replica had *applied* it brought that to **0%**, for about 1 ms more per commit on one machine.

Picture the symptom: a user saves their profile, the page reloads, and their old name stares back at them. Nothing crashed, no network broke, and yet the data is wrong. CAP has nothing to say about that afternoon. I've always admired how Daniel Abadi's PACELC took Eric Brewer's famous CAP idea and pointed it at ordinary days. CAP is about the storm; PACELC is about the weather you live in, and that turns out to be the more useful thing to understand. Let's measure both.

The code is in [database-tradeoffs-lab](https://github.com/the-vishal-kumar/database-tradeoffs-lab/tree/main/08-replicas) (`make replicas`), with a real streaming replica.

> **Case file**
>
> | | |
> |---|---|
> | **The case** | A user saves their profile and sees their old name |
> | **Suspects** | A network partition, "the replica is only milliseconds behind" |
> | **Culprit** | Reading your own write from an asynchronous replica |
> | **The fix** | Read-after-write from the primary, or `remote_apply` per transaction |
> | **Evidence** | PostgreSQL primary and streaming replica, `make replicas`, three runs |

## The two theories in one table

| | During a partition (CAP: P → A or C) | Normally (PACELC: E → L or C) |
|---|---|---|
| **Choose consistency** | Refuse writes (or reads) that can't reach enough copies | Every write waits for replicas: higher, less predictable latency |
| **Choose availability / latency** | Keep answering from the copy you can reach, and reconcile later | Writes return after one copy; reads from replicas may be behind |
| **Example: PostgreSQL** | Synchronous replica unreachable → commits wait | `synchronous_commit` per transaction: `local` … `remote_apply` |
| **Example: DynamoDB** | Designed to stay available; replication within a Region is managed | Reads are eventually consistent by default; `ConsistentRead: true` costs twice the read capacity |
| **Example: MongoDB** | A primary that loses its majority steps down; writes stop until a new primary is elected | `writeConcern: majority` (the default since 5.0) vs `w: 1`; `readConcern` and `readPreference` choose fresh or nearby |

## The first clue: how stale is a replica, really?

Intuition gets this one wrong, so it's worth measuring.

**The test:** 4 background sessions write continuously. 2,000 times: commit an increment on the primary, then immediately read that row from the replica. Is the replica's value older than what was just written? Three runs:

| `synchronous_commit` | The commit waits until… | Commit p50 | Stale reads from the replica |
|---|---|---:|---:|
| `local` | the primary has flushed its WAL | 1.2-1.7 ms | **99.7-100%** |
| `remote_write` | the replica has *received* the WAL (written, not flushed) | 1.8 ms | 92-100% |
| `on` (with a synchronous replica) | the replica has *flushed* the WAL to disk | 2.4 ms | **0.5-11%** |
| `remote_apply` | the replica has *applied* it, so queries there see it | 2.4-2.7 ms | **0%** in every run |

```mermaid
xychart-beta
  title "Commit p50 by synchronous_commit level (ms, low end of range)"
  x-axis ["local", "remote_write", "on", "remote_apply"]
  y-axis "ms"
  bar [1.2 , 1.8 , 2.4 , 2.4]
```

What the evidence says:

- **"The replica is only milliseconds behind" still means stale.** That's the comforting alibi, and it doesn't hold. Replica lag here was about 2 ms at the median. A user who saves a form and is redirected to a page that reads from a replica is faster than that, so they saw their old data almost every time.
- **`synchronous_commit = on` is about durability, not read consistency.** This is the tempting false lead. It guarantees that a committed transaction survives losing the primary, because the replica has it on disk. It does *not* guarantee the replica will show it yet: 0.5-11% of reads were still stale, and the rate varied a lot between runs. Only `remote_apply` makes the replica's reads include your commit.
- **The cost on one machine was about 1 ms per commit.** Across availability zones, each synchronous level adds at least a network round trip. Across regions, tens of milliseconds, on every write. That's the "L" in PACELC, paid all the time.

**Lag also comes in bursts.** One `INSERT` of 2 million rows generated 685 MB of WAL. Across three runs the replica fell 34-56 MB (0.5-0.8 s) behind, and took 0.65-1.1 s after the commit to catch up. Big batch jobs, migrations and backfills are when replica reads are most wrong.

## The second clue: when the replica is gone (the CAP moment)

Now for the rare case, which is rare enough that it's easy never to see it. So I staged it on purpose.

I named a synchronous replica that wasn't connected, as if the only replica had died, and tried to commit with the default `synchronous_commit = on`:

- **With `statement_timeout = 3s`,** the commit waited 9 seconds, until my client gave up. **`statement_timeout` doesn't cover the wait for a synchronous replica:** PostgreSQL stops the timer before the commit step. So without a replica, every commit on that primary hangs.
- **With another session cancelling it after 3 s** (what pressing Ctrl-C does), the client got *success*, plus a warning: *"canceling wait for synchronous replication due to user request"*, and the detail *"The transaction has already committed locally, but might not have been replicated to the standby."* The row **was** changed on the primary.

That's CAP made concrete: a theorem showing up as a warning message. Choosing consistency means writes stop when the replica is unreachable. The "cancel" isn't a rollback: the data is committed on the primary, but not confirmed elsewhere. Your application must treat a cancelled or timed-out commit as *unknown*, not *failed*, and retry idempotently.

## The reveal: a setting that keeps most of both

With more than one replica, `synchronous_standby_names = 'ANY 1 (r1, r2)'` lets commits continue as long as *one* replica answers. That's the usual production setting, and it's a neat piece of design: most of the consistency, without any single replica being able to stop the database.

With the trade-off measured, here's how it plays out in the situations you'll actually meet.

## Case 1: "I saved my profile and it shows the old name"

**Situation:** writes go to the primary, reads go to replicas to spread load, and the user sees their own change disappear.

Options, cheapest first:
- **Read your own writes from the primary** for a short while after a write: track "last write time" in the session (or a cookie) and route that user's reads to the primary for a few seconds.
- **Wait for the replica to catch up to your write.** Record the primary's WAL position after the commit (`pg_current_wal_lsn()`), and before reading from a replica, check it has replayed that far (`pg_last_wal_replay_lsn()`). Most database proxies and ORMs can't do this for you, but it's a few lines.
- **`synchronous_commit = remote_apply`** for the transactions that need it, set per transaction (`SET LOCAL`). Everyone else keeps the faster default.

```mermaid
flowchart TD
  A{"Does this read follow the user's own write?"} -->|no| B["Read from a replica"]
  A -->|yes| C{"Must it be served by a replica?"}
  C -->|no| D["Read from the primary for a few seconds"]
  C -->|yes| E["Wait for the replica to replay the WAL position, or remote_apply"]
```

## Case 2: users in several regions

**Situation:** users in Mumbai and Frankfurt, one database.

- **Single primary, replicas in each region:** local reads, but writes cross the world. Reads in the far region are behind by at least the replication delay, which is the *E → L* trade-off.
- **Multi-region writes** (DynamoDB global tables, Cassandra, CockroachDB, Spanner) move the trade-off into the database. DynamoDB global tables replicate asynchronously by default and resolve conflicts by last writer wins; since mid-2025 they can also be configured for multi-Region strong consistency, which pays the latency on writes. Spanner and CockroachDB keep consistency by paying cross-region latency on writes.
- **Pick per data type:** a user's profile can live in their home region; a global product catalogue can be eventually consistent; a payment ledger should pay for consistency.

## Case 3: analytics and reports on a replica

**Situation:** heavy reports moved off the primary to protect it (the [replicas, partitioning or sharding post](/p/replicas-partitioning-sharding) covers what replicas do and don't fix).

This is where stale reads are *fine*: a dashboard that's a second behind is still correct for its purpose. The risks run the other way:
- long queries on the replica can be cancelled by replication conflicts;
- turning on `hot_standby_feedback` to avoid that holds back cleanup on the primary (the [long-transaction post](/p/long-transaction-slows-database) measures what that costs).

Put reports on a replica, accept the staleness, and give them a separate replica from the one serving application reads.

## Case 4: DynamoDB and MongoDB, the same choice with different knobs

- **DynamoDB:** reads are eventually consistent unless you ask for `ConsistentRead`, which costs double the read capacity. Use it for read-after-write paths, and not for everything.
- **MongoDB:** `writeConcern: "majority"` (the default since 5.0) is about durability. `readConcern: "majority"` plus `readPreference: "primary"` is the fresh-read path. Reading from secondaries is the fast, possibly-stale path. Causally consistent sessions give you read-your-own-writes across secondaries.

> **Remember this**
> - CAP is the storm, PACELC is the weather: fresher answers cost latency every day.
> - An asynchronous replica returned the old value 99.7-100% of the time right after a write.
> - `synchronous_commit = on` buys durability, not fresh replica reads: only `remote_apply` does.

## What to do on Monday

1. **Assume replicas are behind, always.** Route reads that follow a write (or that make decisions) to the primary; send the rest to replicas.
2. **Keep `synchronous_commit = on` for durability with 2+ replicas** and `ANY 1 (…)`, so losing one replica doesn't stop writes. Use `remote_apply` only per transaction, where read-your-writes on a replica matters.
3. **Treat a commit that timed out or was cancelled as unknown**, and make writes idempotent so they're safe to retry.
4. **Watch replication lag** in bytes and seconds, and expect spikes during big batch jobs.
5. **For multi-region, decide per data type** which ones pay cross-region latency for consistency.

What I appreciate most about PACELC is that it makes the trade-off feel like a choice you get to make, request by request, rather than a disaster you wait for. That's a much friendlier way to design a system. For the next case, the [replicas, partitioning or sharding post](/p/replicas-partitioning-sharding) picks up where this one ends.

*Measured Nov 2026 on PostgreSQL 17.11, primary and replica in Docker on one laptop (so network latency is near zero; real networks add at least one round trip per synchronous level). Three runs for the stale-read table. DynamoDB and MongoDB behaviour from their documentation, checked Nov 2026. Code and full results: [database-tradeoffs-lab/08-replicas](https://github.com/the-vishal-kumar/database-tradeoffs-lab/tree/main/08-replicas).*
