The CAP theorem is one of the most important - yet often confusing - concepts in distributed systems. It directly shapes how you reason about trade-offs when designing scalable, fault-tolerant architectures, especially in system design interviews.
What is CAP Theorem?
At its core, the CAP theorem states that in a distributed system, you can only guarantee two out of three of the following properties:
Consistency(C)
- Every read receives the most recent write.
- All nodes see the same data at the same time.
- Example: If you update your display name, every subsequent request to any server should show the new name immediately.
Availability (A)
- Every request to a non-failing node gets a response.
- The response might not contain the latest data, but the system won't fail silently.
- Example: Even if one server is behind on replication, it still responds with the "best it has."
Partition Tolerance (P)
- The system continues working even if parts of it can't communicate due to network failures.
- Example: If the link between your USA and Europe servers breaks, both should still keep serving users.
The Key Insight
In real-world distributed systems, network partitions are inevitable - machines fail, networks drop packets, and datacenters lose connectivity. This means:
👉 You must design for Partition Tolerance (P). So the real trade-off is not "which two of three," but rather:
When a partition happens, do you prioritize Consistency © or Availability (A)?
Understanding CAP Theorem Through an Example
Imagine you're running a website with two servers - one in the USA and one in Europe. When a user updates their public profile (let's say their display name), here's what happens:
Normal Operation
Imagine you're running a website with two servers:
- One in the USA
- One in Europe
Here's what happens when things work as expected:
- User A (in the USA) updates their display name on the USA server.
- That update is replicated to the Europe server.
- User B (in Europe) views User A's profile and sees the updated name.
Everything looks seamless - this is basic replication at work.
When a Network Partition Occurs
Now, imagine the connection between the USA and Europe servers breaks. This is a network partition, and suddenly, we have a decision to make:
- Option A (Consistency first): Refuse to show User B any data until the servers can synchronize. User B gets an error, because we can't guarantee the name is up-to-date.
- Option B (Availability first): Show User B the profile using the Europe server's data - even if it might be stale.
This is where CAP theorem becomes practical - we must choose between consistency and availability.
Which Choice Makes Sense Here?
For our profile example, the answer is clear:
- Showing stale data (an old name) is better than showing no data at all.
- A temporary inconsistency is acceptable - the system can sync up later.
This design is AP (Availability + Partition Tolerance) with eventual consistency.
When to Choose Consistency
Some systems absolutely require consistency, even at the cost of availability:
- Ticket Booking Systems: Imagine if User A booked seat 6A on a flight, but due to a network partition, User B sees the seat as available and books it too. You'd have two people showing up for the same seat!
- E-commerce Inventory: If Amazon has one toothbrush left and the system shows it as available to multiple users during a network partition, they could oversell their inventory.
- Financial Systems: Stock trading platforms need to show accurate, up-to-date order books. Showing stale data could lead to trades at incorrect prices.
When to Choose Availability
The majority of systems can tolerate some inconsistency and should prioritize availability. In these cases, eventual consistency is fine. Meaning, the system will eventually become consistent, but it may take a few seconds or minutes.
- Social Media: If User A updates their profile picture, it's perfectly fine if User B sees the old picture for a few minutes.
- Content Platforms (like Netflix): If someone updates a movie description, showing the old description temporarily to some users isn't catastrophic.
- Review Sites (like Yelp): If a restaurant updates their hours, showing slightly outdated information briefly is better than showing no information at all.
The Guiding Question
When deciding between consistency and availability, ask yourself:
👉 "Would it be catastrophic if users briefly saw inconsistent data?"
- If yes → choose consistency.
- If no → choose availability.
Advanced CAP Theorem Considerations
As systems grow in complexity, the choice between consistency and availability isn't always binary. Modern distributed systems often adopt nuanced approaches that vary by feature and use case.
In practice, many real-world platforms need both availability and consistency - just applied differently across their workflows.
Example: BookMyShow
BookMyShow is a great example of how different parts of the same system demand different consistency models:
- Booking a Seat at a Movie/Show:
- Requires strong consistency.
- The system must ensure two users can't book the same seat, even during network partitions.
- Here, consistency is more important than availability - if necessary, the system will reject a booking request instead of risking double-booking.
- Browsing Event or Movie Details:
- Can prioritize availability.
- If the description of a movie or the show timing is slightly outdated due to replication lag, it's not catastrophic.
- Users would rather see "something" than get an error.
You might say:
"For a system like BookMyShow, I'd prioritize consistency in the booking flow to prevent seat conflicts, but I'd optimize for availability in less critical features like browsing movie details or reviews."
Top comments (0)