codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🏗️

System Design

18 / 49 topics
15Distributed Databases16Consistency Models17Eventual Consistency18CAP Theorem
Tutorials/System Design/CAP Theorem
🏗️System Design

CAP Theorem

Updated 2026-05-15
10 min read

CAP Theorem

Introduction

In the realm of distributed systems, ensuring data consistency and availability while maintaining partition tolerance is a fundamental challenge. The CAP theorem, formulated by Eric Brewer in 1998, provides a theoretical framework to understand these trade-offs. It states that in any distributed system, you can only guarantee two out of three properties: Consistency, Availability, or Partition Tolerance.

Concept

The CAP theorem revolves around the following three properties:

  1. Consistency (C): Every read from the database returns the most recent write or an error.
  2. Availability (A): The system guarantees that a request always receives a response, even if it's not the most up-to-date data.
  3. Partition Tolerance (P): The system continues to operate despite arbitrary message loss or failure of some nodes.

According to the CAP theorem, it is impossible for a distributed system to simultaneously provide all three properties. Let's explore each property in detail:

Consistency

Consistency ensures that all nodes in the system see the same data at the same time. This is often achieved through strong consistency models where writes are propagated synchronously across all nodes.

Availability

Availability guarantees that every request receives a response, even if it means returning stale data. In this model, the system prioritizes responsiveness over ensuring that every read reflects the most recent write.

Partition Tolerance

Partition tolerance ensures that the system continues to function correctly even when communication between nodes is lost due to network partitions or failures. This is crucial for distributed systems operating in environments where network issues are common.

Examples

To better understand these concepts, let's consider some practical examples:

Example 1: Strongly Consistent System (C + P)

In a strongly consistent system, both consistency and partition tolerance are prioritized. However, this often comes at the cost of availability.

CodeBlock Example:

JavaScript
1// Example of a strongly consistent system
2function updateData(key, value) {
3// Synchronously update data across all nodes
4nodes.forEach(node => node.update(key, value));
5}
6
7function readData(key) {
8// Read the most recent write from any node
9return nodes[0].read(key);
10}

Tip: In this example, if a network partition occurs, some nodes may become isolated and unable to communicate with others. However, once communication is restored, all nodes will eventually converge on the same state.

Example 2: Eventually Consistent System (A + P)

An eventually consistent system prioritizes availability and partition tolerance over consistency. This model allows for asynchronous updates and eventual convergence of data across nodes.

CodeBlock Example:

JavaScript
1// Example of an eventually consistent system
2function updateData(key, value) {
3// Asynchronously update data on all nodes
4nodes.forEach(node => node.updateAsync(key, value));
5}
6
7function readData(key) {
8// Read the most recent write from any available node
9return nodes[0].read();
10}

Tip: In this scenario, even if a network partition occurs, the system can still respond to read requests by returning data from available nodes. Once communication is restored, nodes will eventually synchronize their states.

Example 3: System with Availability and Consistency (C + A)

A system that prioritizes consistency and availability often sacrifices partition tolerance. This model ensures that all reads return the most recent writes and that every request receives a response, but it may fail in the face of network partitions.

CodeBlock Example:

JavaScript
1// Example of a system with consistency and availability
2function updateData(key, value) {
3// Synchronously update data across all nodes
4nodes.forEach(node => node.update(key, value));
5}
6
7function readData(key) {
8// Read the most recent write from any node
9return nodes[0].read(key);
10}

Tip: In this case, if a network partition occurs, some nodes may become isolated and unable to communicate with others. The system will prioritize maintaining consistency and availability within the reachable subset of nodes.

What's Next?

Understanding the CAP theorem is crucial for designing robust distributed systems. In the next section, we will delve into "Microservices Architecture," exploring how these principles can be applied in building scalable and resilient applications.

By mastering the trade-offs between consistency, availability, and partition tolerance, you'll be well-equipped to design distributed systems that meet your application's specific needs.


PreviousEventual ConsistencyNext Microservices Architecture

Recommended Gear

Eventual ConsistencyMicroservices Architecture