Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Understanding the CAP Theorem
📖 Scenario: You are learning how distributed databases make trade-offs between consistency, availability, and partition tolerance. You will build a simple Python model to simulate the CAP theorem using dictionaries to represent database nodes, replication, and partition behavior.
🎯 Goal: Build a basic data structure that models distributed database nodes, replication, network partitions, and the CP vs AP trade-off when a partition occurs.
📋 What You'll Learn
Create a dictionary representing database nodes and their stored values
Simulate a write operation that replicates data to all nodes
Simulate a network partition that isolates one node
Implement CP and AP response strategies for reads during a partition
💡 Why This Matters
🌍 Real World
The CAP theorem guides every distributed database design. MongoDB, Cassandra, DynamoDB, and CockroachDB all make explicit CAP trade-offs that determine their behavior during network failures.
💼 Career
Understanding CAP trade-offs is essential for system design interviews and for choosing the right database for distributed applications.
Progress0 / 4 steps
1
Create database nodes
Create a dictionary called nodes with three entries: 'node_a', 'node_b', and 'node_c', each with value None representing empty initial state.
DBMS Theory
Hint
Each node starts with no data. Use None as the initial value for each node key.
2
Simulate a write with replication
Create a variable write_value set to 42. Use a for loop with variable node to iterate over nodes and set each node's value to write_value.
DBMS Theory
Hint
Loop through all node keys and assign the write_value to each one. This models full replication.
3
Simulate a network partition
Create a list called partitioned_nodes containing 'node_c'. Create a list called reachable_nodes containing 'node_a' and 'node_b'. Then update node_a and node_b to value 99 using a for loop with variable node over reachable_nodes. Node_c remains at 42 because it is partitioned.
DBMS Theory
Hint
The partition isolates node_c. Only node_a and node_b receive the new write (99). Node_c still has the old value (42).
4
Implement CP and AP read responses
Create a variable read_node set to 'node_c'. Create a variable cp_response: if read_node is in partitioned_nodes, set it to 'Error: node partitioned', else set it to nodes[read_node]. Create a variable ap_response set to nodes[read_node] regardless of partition status.
DBMS Theory
Hint
CP rejects reads from partitioned nodes to maintain consistency. AP returns whatever data the node has, even if stale.
Practice
(1/5)
1. What does the CAP theorem state about distributed systems?
easy
A. A distributed system can only guarantee two out of Consistency, Availability, and Partition tolerance at the same time.
B. A distributed system can guarantee all three: Consistency, Availability, and Partition tolerance simultaneously.
C. CAP theorem applies only to single-node databases.
D. CAP theorem states that Consistency is always more important than Availability.
Solution
Step 1: Understand the CAP theorem basics
The CAP theorem says a distributed system cannot guarantee Consistency, Availability, and Partition tolerance all at once.
Step 2: Identify the correct statement
Only two of these three properties can be guaranteed simultaneously in a distributed system.
Final Answer:
A distributed system can only guarantee two out of Consistency, Availability, and Partition tolerance at the same time. -> Option A
Quick Check:
CAP theorem = Two guarantees only [OK]
Hint: Remember CAP means pick any two out of three [OK]
Common Mistakes:
Thinking all three guarantees are possible simultaneously
Confusing CAP theorem with ACID properties
Assuming CAP applies to single-node systems
2. Which of the following is a correct example of a system prioritizing Availability over Consistency according to CAP theorem?
easy
A. A DNS system that always responds to queries even if some data is outdated.
B. A system that never allows network partitions.
C. A system that stops responding during network partitions to avoid inconsistent data.
D. A banking system that locks accounts during transactions to ensure exact balances.
Solution
Step 1: Identify Availability over Consistency example
Availability means the system always responds, even if data might be stale or inconsistent.
Step 2: Match example to definition
DNS systems prioritize Availability by responding to queries despite possible outdated data during partitions.
Final Answer:
A DNS system that always responds to queries even if some data is outdated. -> Option A
Quick Check:
Availability > Consistency example = DNS [OK]
Hint: Availability means always respond, even if data is stale [OK]
Common Mistakes:
Confusing locking with Availability
Thinking systems can avoid network partitions
Assuming consistency means always available
3. Consider a distributed database that chooses Consistency and Partition tolerance but sacrifices Availability during network failures. What happens when a network partition occurs?
medium
A. The system automatically heals the network partition.
B. The system continues to serve all requests with possibly stale data.
C. The system ignores partitions and may return inconsistent data.
D. The system refuses to respond to some requests to maintain data consistency.
Solution
Step 1: Analyze system choice of Consistency and Partition tolerance
Choosing Consistency and Partition tolerance means the system must maintain data correctness and handle network splits.
Step 2: Understand impact on Availability
To keep Consistency during partitions, the system sacrifices Availability by refusing some requests.
Final Answer:
The system refuses to respond to some requests to maintain data consistency. -> Option D
Hint: Consistency + Partition tolerance means some requests denied [OK]
Common Mistakes:
Assuming system serves stale data when consistency chosen
Thinking partitions are automatically fixed
Confusing availability with consistency guarantees
4. A developer claims their distributed system guarantees Consistency, Availability, and Partition tolerance simultaneously. What is the most likely issue?
medium
A. The system uses eventual consistency correctly.
B. The system is ignoring network partitions or not truly distributed.
C. The system is using a single-node database.
D. The system is using a caching layer to improve performance.
Solution
Step 1: Recall CAP theorem limitation
CAP theorem states all three guarantees cannot be met simultaneously in a distributed system.
Step 2: Identify why claim is incorrect
If a system claims all three, it likely ignores partitions or is not truly distributed.
Final Answer:
The system is ignoring network partitions or not truly distributed. -> Option B
Quick Check:
All three guarantees impossible in distributed system [OK]
Hint: All three guarantees mean system is not truly distributed [OK]
Common Mistakes:
Believing eventual consistency means full consistency
Confusing caching with consistency guarantees
Ignoring network partitions in design
5. You are designing a global e-commerce platform that must remain available during network partitions but can tolerate some temporary inconsistency. According to CAP theorem, which two properties should you prioritize?
hard
A. Consistency and Partition tolerance
B. Consistency and Availability
C. Availability and Partition tolerance
D. Only Availability
Solution
Step 1: Understand system requirements
The platform must remain available during network partitions and can tolerate temporary inconsistency.
Step 2: Match requirements to CAP properties
Availability and Partition tolerance must be prioritized, sacrificing strict Consistency temporarily.
Final Answer:
Availability and Partition tolerance -> Option C
Quick Check:
Available + Partition tolerant = temporary inconsistency allowed [OK]
Hint: Availability + Partition tolerance means accept temporary inconsistency [OK]
Common Mistakes:
Choosing Consistency when availability is required