Imagine you own a cafe with two branches. Both branches sell chocolate cakes and share their stock information.
One day, the internet connection between the two branches stops working.
Branch A sells the last chocolate cake, but Branch B does not know about it. Branch B still thinks one cake is available.
This situation helps us understand CAP theorem.
1. C — Consistency
Both branches should have correct, up-to-date information.
Branch A sold the last cake, so Branch B should not promise that same cake to another customer.
In simple words: Don't show incorrect or outdated data when strong consistency is required.
2. A — Availability
The café should keep serving customers.
Even though Branch B cannot contact Branch A, it continues accepting orders using the information it has. However, it might accept an order for a cake that is already sold out.
In simple words: Keep responding to customers, even when the latest information is unavailable.
3. P — Partition Tolerance
The café should handle a broken connection between its branches.
Even if Branch A and Branch B cannot communicate, the system must deal with the network failure.
In simple words: Handle communication failures between servers.
4. What Does CAP Theorem Mean?
When the network connection breaks, the café faces a choice.
Option 1: CP — Consistency + Partition Tolerance
Branch B says, "I can't verify the stock, so I'll wait before confirming this order."
- The café protects consistency.
- Some customers must wait or receive an error.
- Availability is sacrificed for affected requests.
Option 2: AP — Availability + Partition Tolerance
Branch B says, "I'll keep accepting orders even though I can't check Branch A's latest stock."
- Customers continue getting responses.
- Some information might be outdated or conflicting.
- The branches can reconcile their data when communication returns.
5. What About CA?
Consistency and availability can coexist when there is no network partition and the system's design allows it.
But when a partition occurs, a distributed system cannot guarantee both consistency and availability for every affected request.
6. Quick Comparison
| Property | Café example | Simple meaning |
|---|---|---|
| Consistency | Both branches know the correct cake stock | Keep data correct |
| Availability | Branch B continues accepting requests | Keep responding |
| Partition tolerance | Branches handle a broken connection | Handle network failures |
7. Interview Question
The internet connection between two café branches is down. Branch A may have sold the last chocolate cake. What should Branch B do if the café must prevent selling the same cake twice?
- A. Accept the order immediately using old stock information.
- B. Wait or reject the order until stock can be verified safely.
- C. Ignore the inventory records.
Answer
Option B is the safest choice if strong consistency is required. Branch B may have to delay or reject the affected request during the network partition.
Key Takeaway
- C — Consistency: Keep the data correct.
- A — Availability: Keep the service responding.
- P — Partition tolerance: Handle a broken connection.
CAP theorem: During a network partition, a distributed system cannot guarantee both consistency and availability for every request. It must make a trade-off for affected operations.
Top comments (0)