liveBook · Manning

CAP theorem

As explained in PostgreSQL Mistakes and How to Avoid Them

The CAP theorem states that a distributed data system cannot simultaneously guarantee strong consistency, availability, and partition tolerance under network partition.

CAP theorem names an unavoidable distributed-systems tradeoff: consistency means reads return the latest data or error; availability means every request receives a response, possibly not the latest data; partition tolerance means operating while messages are dropped or delayed. A system cannot guarantee all three simultaneously. section 5.5

The practical mistake is treating bidirectional replication between independent PostgreSQL instances as a simple route to a highly available multi-master database. PostgreSQL replication uses a shared-nothing design, so independent nodes must communicate through replication connections; delays, dropped messages, and concurrent writes make the tradeoffs concrete. section 5.5

Under a network partition, choose deliberately between:

  • Consistency: reject or error rather than return data that may not be current. section 5.5
  • Availability: answer every request even though the response may not reflect the most recent data. section 5.5

PACELC extends the decision beyond failures: during a partition, choose availability or consistency; otherwise, choose between latency and loss of consistency. This matters for databases expected to stay online most of the time, not only during an outage. section 5.5

Two-way logical replication is not conflict handling

Two independent nodes can publish and subscribe to each other, then appear to replicate inserts successfully. But a locally generated identity value can collide with an already replicated primary key, producing a duplicate-key error. Schema changes can also leave a target missing a replicated column, causing the logical-replication worker to exit. Replication wiring alone does not resolve distributed conflicts or coordinate schema changes. section 5.5

Figure 5.2 Logical replication in both directions
figure

Use the topology diagram to inspect the two independent replication directions that make a seemingly simple multi-master setup vulnerable to distributed coordination problems. section 5.5

For production multi-master use, prefer an established multi-master solution rather than assembling one from unidirectional logical replication. Homemade distributed systems need a narrowly focused scope and must account for distributed serialization anomalies plus CAP and PACELC tradeoffs. section 5.7

How PostgreSQL Mistakes and How to Avoid Them covers it

How deep it goes
Defines all three CAP guarantees, extends them with PACELC, and illustrates failures through replication conflicts rather than formal proof.
Examples
Uses two-way PostgreSQL logical replication with identity-key collisions and a renamed replicated column that stops a replication worker.
What you can do after
Evaluate/understand why multi-master designs require explicit tradeoffs and established conflict-aware solutions.
How it connects
Connects consistency, availability, partition tolerance, latency, PACELC, logical replication, and distributed serialization anomalies.

Also explained in

Compare how 4 books explain CAP theorem →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of PostgreSQL Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.