CAP theorem reminds me of the time where I was confused more with the terminology somewhat same as with encounter with term nosql. When I first came across CAP theorem a decade ago my struggle was good chunk to understand the terminology. My brain was trying to decipher each term of CAP and then interlink each one of them.
So this was me back then lets expand the abbreviation first.
C- Consistency A- availability P — partition . Okay I understood each term and then how each one to linked to other? I was frantically reading definitions over internet articles and became more confused back then until a senior from my team back then explained what it really meant. It was like aha moment for me !
So if I have to explain a new engineer for the first time what it means. I would say this,
CAP theorem is a choice which you have to make between consistency and availability in case of network partition which has to be in tantum with the business problem you are solving. It’s not only a technical problem , your business have to decide if node communication lost (network partitioning) then how does your business want to behave do they want the system to be available and later figure out the consistency . Or the nature of the business is such that consistency is of at most importance more than system to be available.
Image created by Author using canva
Now we will look into some real life examples to concrete this idea further. Our first example is of a banking transaction to illustrate the CAP Theorem.
Banking System
Imagine a bank with two branches and a central account. These bank branches support three different operations:
Money Withdrawal
Money Deposit
Check Account Balance
Image created by Author using Meta AI
Image created by Author using canva
What are our trade offs choices?
Image created By Author using Canva
The CAP Theorem forces us to make trade-offs when designing distributed systems. In our banking example, I would think the business decision would lean towards CP system which means in case of network partition system will be unavailable instead of showing incorrect or inconsistent data for example imagine you have total balance of $1000 .
You deposited $500 more but network partition happened if the design is AP it still showed you $1000 because new deposit of $500 is not sync in yet. This will create panic and leave customers with low confidence in the imagine bank. ( That’s a sad customer scenario)
Rather if you receive system unavailable message and check back again in hour you saw $1500 you might not have the same grievances towards your imaginary bank.( Its some inconvenience to the customer but overall customer is happy that bank is reporting his/her money correctly)
Again if the system is unavailable all the time you can’t even make your transactions you might lean towards low confidence zone.( This scenario will make customers very unhappy).
Let’s explore another example where availability is prioritized over consistency in the context of the CAP Theorem.
This time imagine a large-scale e-commerce platform that spans multiple data centers. This platform allows users to place orders, check product availability, and view their order history. Here’s how the CAP trade-off plays out:
E-Commerce System
During a peak shopping season (Black Friday, for instance), the e-commerce system experiences an unprecedented surge in traffic.
Network partitions occur due to heavy load, temporary network glitches, or maintenance activities.
Image created by Author using Meta AI
Given the high traffic and network challenges, the system prioritizes availability over strict consistency.
Image created by Author using Canva
Amazon DynamoDB and Google Cloud Spanner are examples of databases that prioritize availability over strict consistency during network partitions1.
During high load or network disruptions, this e-commerce platform chooses to maintain availability for user interactions, even if it means relaxing strict consistency temporarily. The trade-off ensures that users can continue shopping without encountering system-wide failures, even if some data might be slightly outdated.
In essence, CAP Theorem tries to strike the Balance between Data Consistency, System Availability, and Fault Tolerance. Also something worth noting is its not binary decision it’s not either availability or consistency its actually the degree of gradient.
I’m diving deeper into Designing Data-Intensive Applications and will be sharing insights on specific whitepapers, concepts, and design patterns that capture my attention. If you’d like to join me on this exploration, consider following me to receive automatic notifications about my next article!