> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# CAP Theorem

> Summarizes CAP theorem trade offs in distributed systems, choosing between consistency and availability during network partitions, with examples and design guidance

The CAP theorem describes a fundamental trade-off in distributed systems that store and replicate data across multiple machines. It states that in the presence of a network partition, a distributed system can guarantee at most two of the following three properties:

* **Consistency (C)** — After a successful write, every subsequent read returns the latest value, no matter which node serves the request.
* **Availability (A)** — Every request receives a response (success or failure); the system does not hang or refuse to respond.
* **Partition tolerance (P)** — The system continues to operate despite arbitrary message loss or network failures between nodes.

When the network is healthy (no partition), a system can appear both consistent and available. But when a partition occurs — nodes remain running but cannot communicate — you cannot simultaneously guarantee both consistency and availability.

How you design the system depends on which property you choose to prioritize during partitions. Because partitions are inevitable in real networks, most practical systems must tolerate them and therefore choose between consistency and availability during the partition window.

Example scenario

Imagine a photo app that stores data on a primary database and two replicas. If a network partition separates the primary from the replicas, the machines are still running but cannot synchronize. After the partition, users continue making requests: someone opens their feed while another person likes a photo. Since the nodes cannot talk to each other, you broadly have two options:

* Serve reads and writes on both sides despite divergence. This choice preserves availability but sacrifices strict consistency.
* Block or reject some operations until nodes can coordinate. This preserves consistency but reduces availability.

![A stylized diagram of the CAP theorem labeled "Consistency, Availability, Partition Tolerance." It shows a user app and server talking to a primary and replica database separated by a network partition, with differing like counts on each replica.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Databases/CAP-Theorem/cap-theorem-partition-primary-replica.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=c38c7bbc233f7d6bbdbfab3b55ab003f)

Which to choose — Availability or Consistency?

* Favor **Availability (AP)** when temporary divergence is acceptable and responsiveness is critical. Social feeds, counters, and many user-facing features tolerate eventual consistency and can reconcile later.
* Favor **Consistency (CP)** when correctness is critical and must never be violated, even if that means refusing requests during a partition. Examples include banking, inventory management, and other systems where double-spend or over-commitment would be catastrophic.

Concrete example: racing writes vs. money

For likes on a photo, small discrepancies (e.g., one replica shows 1,000 likes while another shows 1,001) are usually acceptable for a short time. In that case, prioritize availability and reconcile after the partition.

But for a paid promotion or account balance, if both sides allow the same $10 charge during a partition, the system could end up charging $20 in total. For financial or inventory systems, you must prioritize consistency: refuse operations that can't be safely executed until nodes can agree.

![A stylized diagram showing a mobile "boost post" request, an app server, and two databases (primary and replica) split by a network partition that both record a 10 charge. The caption highlights the CAP tradeoff with the result "10 spent twice = \$20."](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Databases/CAP-Theorem/boost-post-network-partition-double-charge.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=643f4e5f4ec2359b72017a7de089a959)

Quick reference table

| Choice | Guarantees during partition | Typical use cases |
| - | - | - |
| CP (Consistency + Partition tolerance) | Strong consistency preserved; some requests may be denied or delayed | Banking, inventory, financial ledgers |
| AP (Availability + Partition tolerance) | System remains responsive; replicas may diverge and require reconciliation | Social feeds, likes, analytics, caching |
| CA (Consistency + Availability) | Only possible when partitions cannot occur (not realistic for distributed systems across unreliable networks) | Single-node or tightly-coupled systems with reliable networking |

Design guidance

* Classify each data type by correctness requirements (e.g., eventual consistency OK vs. strict correctness required).
* Use techniques that match your choice:
  * For AP systems: eventual consistency, conflict-free replicated data types (CRDTs), last-write-wins, background reconciliation.
  * For CP systems: distributed locking, consensus protocols (e.g., Raft, Paxos), strong leader-based replication, two-phase commit.
* Consider hybrid approaches: use CP for transactional data and AP for less-critical, high-throughput data. Many real-world systems mix strategies depending on the operation.

CAP does not require permanently forgoing either consistency or availability; it only means you cannot guarantee both during a network partition. Choose the trade-off based on data correctness requirements and the acceptable tolerance for temporary unavailability or inconsistency.

Further reading and references

* [CAP theorem — Wikipedia](https://en.wikipedia.org/wiki/CAP_theorem)
* Eric Brewer’s original talk (Brewer’s conjecture) and follow-ups
* “Dynamo: Amazon’s Highly Available Key-value Store” — an example of an AP-oriented design
* [Raft Consensus Algorithm](https://raft.github.io/) — practical approach for CP systems

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/devops-interview-prep/module/1ee75c71-d566-42df-8cc2-7aa8e79b0f04/lesson/4751d245-0218-4fe3-96e9-899f71327f0a)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.