In this lesson, you will learn how to map your application’s functional requirements to specific CAP theorem trade-offs to choose the right consistency and availability settings.
The Reality of Distributed Constraints
The CAP theorem dictates that in the event of a network partition (P), a distributed system must choose between consistency (C)—ensuring every read receives the most recent write—and availability (A)—ensuring every request receives a non-error response, even if it might be stale.
In practice, we rarely choose between "absolute consistency" or "total availability." Instead, we evaluate our specific data access patterns to configure our database clusters.
Practical Evaluation
Consider a system like a Real-time Leaderboard (which we previously built in Redis). If the network segments, do you prioritize showing the exact current rank, or do you prioritize system uptime?
-
Prioritizing Consistency (CP): Use this for financial ledgers or inventory counts. If a write cannot be replicated to a majority of nodes due to a partition, the system rejects the operation.
- Configuration example: In a wide-column store, you might set the consistency level to
QUORUMorALL.
-- For critical operations, ensure majority agreement UPDATE user_balances SET amount = 500 WHERE user_id = 123 USING CONSISTENCY QUORUM; - Configuration example: In a wide-column store, you might set the consistency level to
-
Prioritizing Availability (AP): Use this for social media feeds or "likes." It is better to show a slightly stale count than to return an error to the user.
- Configuration example: You allow
ONEorLOCAL_ONEconsistency, accepting that different nodes might see different values until the background process resolves the conflict.
- Configuration example: You allow
Choosing Your Setting
When evaluating your requirements, ask yourself: "What happens if a user sees data that is 5 seconds old?"
- If the answer is "It's a disaster," you are locked into a CP model. You must design your system to handle potential downtime during network instability.
- If the answer is "It's a minor inconvenience," you are free to design for AP. This allows you to leverage your distributed architecture for high performance and uptime, even when parts of the cluster are unreachable.
Key Takeaway: Choose consistency levels based on the cost of stale data versus the cost of system downtime for your specific business use case.