Bird
Raised Fist0
HLDsystem_design~12 mins

The CAP theorem in HLD - Architecture Diagram

Choose your learning style10 modes available
System Overview - The CAP theorem

The CAP theorem explains the trade-offs in distributed systems between Consistency, Availability, and Partition tolerance. It states that a system can only guarantee two of these three properties at the same time. This helps architects decide which properties to prioritize based on system needs.

Architecture Diagram
          +-------------------+          +-------------------+
          |    Client A       |          |    Client B       |
          +---------+---------+          +---------+---------+
                    |                              |
                    |                              |
          +---------v---------+          +---------v---------+
          |   Load Balancer   |          |   Load Balancer   |
          +---------+---------+          +---------+---------+
                    |                              |
                    +--------------+---------------+
                                   |
                         +---------v---------+
                         |   Distributed     |
                         |   Database Cluster|
                         +---------+---------+
                                   |
                  +----------------+----------------+
                  |                                 |
          +-------v-------+                 +-------v-------+
          | Partition 1   |                 | Partition 2   |
          | (Subset of DB)|                 | (Subset of DB)|
          +---------------+                 +---------------+
Components
Client A
client
User or application sending requests to the system
Client B
client
Another user or application sending requests to the system
Load Balancer
load_balancer
Distributes incoming requests evenly to database nodes
Distributed Database Cluster
database_cluster
Stores data across multiple nodes to provide redundancy and scalability
Partition 1
database_node
One part of the database cluster holding a subset of data
Partition 2
database_node
Another part of the database cluster holding a subset of data
Request Flow - 6 Hops
Client A → Load Balancer
Load Balancer → Distributed Database Cluster
Distributed Database Cluster → Partition 1 or Partition 2
Partition 1 or Partition 2 → Distributed Database Cluster
Distributed Database Cluster → Load Balancer
Load Balancer → Client A
Failure Scenario
Component Fails:Network Partition between Partition 1 and Partition 2
Impact:Partitions cannot communicate, causing potential inconsistency or unavailability depending on system choice
Mitigation:System must choose to prioritize either Consistency (reject some requests) or Availability (serve stale data) during partition
Architecture Quiz - 3 Questions
Test your understanding
According to the CAP theorem, which of the following is impossible for a distributed system to guarantee simultaneously?
AAll three: Consistency, Availability, and Partition tolerance
BConsistency and Availability
CConsistency and Partition tolerance
DAvailability and Partition tolerance
Design Principle
The CAP theorem guides system architects to understand that in distributed systems, network failures force a choice between consistency and availability. Designing systems requires prioritizing which two properties best fit the application's needs.

Practice

(1/5)
1. Which two properties can a distributed system guarantee at the same time according to the CAP theorem?
easy
A. Consistency and Availability
B. Availability and Partition tolerance
C. Consistency and Partition tolerance
D. Consistency, Availability, and Partition tolerance all together

Solution

  1. Step 1: Understand the CAP theorem basics

    The CAP theorem states that a distributed system can only guarantee two out of three properties: Consistency, Availability, and Partition tolerance.
  2. Step 2: Identify which pairs are possible

    Systems can guarantee Availability and Partition tolerance or Consistency and Partition tolerance, but not all three simultaneously. However, only Availability and Partition tolerance can be guaranteed simultaneously in the presence of network partitions.
  3. Final Answer:

    Availability and Partition tolerance -> Option B
  4. Quick Check:

    CAP theorem = two properties only [OK]
Hint: Remember CAP means pick only two properties [OK]
Common Mistakes:
  • Thinking all three properties can be guaranteed simultaneously
  • Confusing Availability with Partition tolerance
  • Ignoring network partitions in distributed systems
2. Which of the following is the correct definition of Partition tolerance in the CAP theorem?
easy
A. The system can handle unlimited data growth
B. All nodes always return the same data at the same time
C. The system is always available to respond to requests
D. The system continues to operate despite network failures between nodes

Solution

  1. Step 1: Define Partition tolerance

    Partition tolerance means the system keeps working even if some network connections between nodes fail or messages are lost.
  2. Step 2: Match definitions to options

    The system continues to operate despite network failures between nodes correctly describes Partition tolerance as handling network failures. Other options describe consistency, availability, or scalability.
  3. Final Answer:

    The system continues to operate despite network failures between nodes -> Option D
  4. Quick Check:

    Partition tolerance = handle network splits [OK]
Hint: Partition tolerance means surviving network splits [OK]
Common Mistakes:
  • Confusing Partition tolerance with Availability
  • Thinking Partition tolerance means data consistency
  • Assuming Partition tolerance means unlimited scaling
3. Consider a distributed database that chooses Availability and Partition tolerance over Consistency. What is the expected behavior during a network partition?
medium
A. The system may return stale or inconsistent data but remains available
B. The system rejects all writes until the partition is resolved
C. The system becomes unavailable to maintain consistency
D. The system automatically merges all data without conflicts

Solution

  1. Step 1: Identify chosen CAP properties

    The system chooses Availability and Partition tolerance, sacrificing Consistency.
  2. Step 2: Analyze behavior during network partition

    Because it prioritizes Availability, the system stays up and responds to requests, but data may be inconsistent or stale.
  3. Final Answer:

    The system may return stale or inconsistent data but remains available -> Option A
  4. Quick Check:

    Availability + Partition tolerance = possible inconsistency [OK]
Hint: Availability + Partition tolerance means possible stale data [OK]
Common Mistakes:
  • Assuming system rejects writes to keep consistency
  • Thinking system becomes unavailable during partition
  • Believing automatic conflict-free merges always happen
4. A developer claims their distributed system guarantees Consistency, Availability, and Partition tolerance simultaneously. What is the most likely issue?
medium
A. The system is using eventual consistency, not strong consistency
B. The system is only tested on a single node
C. The system is ignoring network partitions or failures
D. The system uses synchronous replication

Solution

  1. Step 1: Recall CAP theorem limitation

    The CAP theorem states all three properties cannot be guaranteed simultaneously in a distributed system.
  2. Step 2: Identify the likely mistake

    If the system claims all three, it probably ignores or does not handle network partitions properly, which is unrealistic in real distributed environments.
  3. Final Answer:

    The system is ignoring network partitions or failures -> Option C
  4. Quick Check:

    CAP theorem forbids all three together [OK]
Hint: All three CAP properties together is impossible [OK]
Common Mistakes:
  • Assuming eventual consistency means full consistency
  • Thinking synchronous replication solves CAP limits
  • Ignoring real network failures in testing
5. You are designing a global online banking system where transactions must be accurate and consistent even during network failures. According to the CAP theorem, which two properties should you prioritize and why?
hard
A. Consistency and Partition tolerance, to maintain accurate data despite network partitions, even if some requests are delayed
B. Availability and Partition tolerance, to keep the system running even if data is temporarily inconsistent
C. All three properties equally, because banking requires perfect reliability
D. Consistency and Availability, to ensure all users see the same data and system is always responsive

Solution

  1. Step 1: Understand banking system requirements

    Banking needs accurate, consistent transactions even if network issues occur, so data correctness is critical.
  2. Step 2: Apply CAP theorem to design choice

    To keep data consistent during network partitions, the system must prioritize Consistency and Partition tolerance, accepting some availability delays.
  3. Final Answer:

    Consistency and Partition tolerance, to maintain accurate data despite network partitions, even if some requests are delayed -> Option A
  4. Quick Check:

    Banking = Consistency + Partition tolerance [OK]
Hint: Banking needs consistency over availability during partitions [OK]
Common Mistakes:
  • Choosing availability over consistency for critical data
  • Assuming all three CAP properties can be met
  • Ignoring impact of network partitions on data accuracy
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard