Skip to content
intermediatePhase 43 · System Design Foundations

Consistency

Understand strong vs eventual consistency in distributed systems.

45m
0 problems
Topic Progress0%

Strong vs Eventual Consistency

Consistency describes how up-to-date data is across all nodes in a distributed system.

Strong Consistency

Write: Client A writes X = 1 to Node 1

Before acknowledging write:
Node 1 ──── Replicate ────→ Node 2 (confirms)
                           Node 3 (confirms)

After all confirm:
Node 1: X = 1  ✓
Node 2: X = 1  ✓
Node 3: X = 1  ✓

Read from ANY node returns X = 1

Eventual Consistency

Write: Client A writes X = 1 to Node 1

Immediately after write:
Node 1: X = 1  ✓
Node 2: X = 0  (old value)
Node 3: X = 0  (old value)

After some time (milliseconds to seconds):
Node 1: X = 1  ✓
Node 2: X = 1  ✓
Node 3: X = 1  ✓

Read from different nodes may return different values temporarily

Comparison

Aspect Strong Consistency Eventual Consistency
Read after write Always returns new value May return old value
Performance Slower (must replicate) Faster (write and return)
Availability Lower (must wait for replicas) Higher (always available)
Use case Banking, inventory Social media, analytics
Protocol Raft, Paxos Gossip protocol

Read-Your-Writes Consistency

Client A writes X = 1
Client A reads X → Always returns 1 (even if other nodes lag)

Client B reads X → May return 0 or 1 (depending on which node)

This is a middle ground between strong and eventual consistency.

Consistency Models

Different consistency models offer different guarantees about data visibility.

Consistency Model Spectrum

Stronger ←──────────────────────────────────→ Weaker

Linearizability > Sequential > Causal > Eventual

Linearizability: Most strict, most expensive
Eventual: Most relaxed, most performant

Consistency Models

Model Guarantee Performance Use Case
Linearizability All operations appear instant Slow Financial, inventory
Sequential Operations appear in some order Medium Chat, collaboration
Causal Causally related ops in order Medium Social feeds
Eventual All nodes converge eventually Fast Analytics, caching

Linearizability

Timeline:

T1: Client A writes X = 1
T2: Client B writes X = 2
T3: Client C reads X → Must see 2 (or later)

All reads see the most recent write.
Equivalent to single-node performance.

Causal Consistency

Scenario: Social media post

T1: User A posts "Hello" (causes User B's reply)
T2: User B replies "Hi there"
T3: User C reads feed

Causal consistency ensures:
- If User C sees "Hi there", they also see "Hello"
- Causally related events appear in order

Eventual Consistency Details

Write to Node 1:
Node 1: X = 1 (immediately)
Node 2: X = 0 (stale)
Node 3: X = 0 (stale)

Gossip Protocol:
Node 1 gossips to Node 2: "X = 1"
Node 2 updates: X = 1
Node 2 gossips to Node 3: "X = 1"
Node 3 updates: X = 1

Convergence: All nodes eventually have X = 1
Time to converge: Typically milliseconds to seconds

Consistency in Practice

System              Consistency Model
──────────────────────────────────────
Banking             Linearizability
Inventory           Linearizability
Chat                Sequential
Social Feed         Causal
Analytics           Eventual
DNS                 Eventual
Cache               Eventual

Consistency Tradeoffs

Choosing consistency level involves tradeoffs between correctness, performance, and availability.

The Tradeoff Triangle

        Consistency
           /\
          /  \
         /    \
        /      \
       /________\
  Availability  Performance

You can optimize for two, but not all three.

Common Tradeoffs

Scenario Choose Tradeoff
Banking Strong consistency Lower performance
Social media Eventual consistency May show stale data
Shopping cart Read-your-writes Complex implementation
Analytics Eventual consistency May miss recent data

Consistency vs Performance

Strong Consistency:
- Write must replicate to all nodes before acknowledgment
- Latency = Network RTT × Number of nodes
- Throughput limited by slowest replica

Eventual Consistency:
- Write acknowledged immediately
- Background replication
- Higher throughput, lower latency

Example:
100ms network RTT, 3 replicas

Strong: Write takes 200ms (wait for 2 replicas)
Eventual: Write takes 10ms (local write only)

Consistency vs Availability

Strong Consistency:
- If replica is down, write may fail
- System becomes unavailable during network partitions

Eventual Consistency:
- Write succeeds even if replicas are down
- System remains available during partitions

CAP Theorem: You can only have 2 of 3:
- Consistency
- Availability
- Partition Tolerance

Design Decisions

Q: What consistency level do you need?

A: It depends on the use case:

1. Can you tolerate stale data?
   - Yes → Eventual consistency
   - No → Strong consistency

2. Is availability critical?
   - Yes → Eventual consistency
   - No → Strong consistency OK

3. Is performance critical?
   - Yes → Eventual consistency
   - No → Strong consistency OK

4. Are operations causally related?
   - Yes → Causal consistency
   - No → Eventual consistency

Practice Problems

0/3solved
Design Consistency System

Design a scalable Consistency system. Cover high-level architecture, data model, and API design.

Solution
// Complete system design:
// - Functional + Non-functional requirements
// - Capacity estimation
// - Data model (SQL/NoSQL choice)
// - API endpoints
// - Component architecture
// - Scaling strategy
// - Monitoring & reliability
Consistency Scaling

How would you scale Consistency to handle 10x the current load? Identify bottlenecks and solutions.

Solution
// Scaling approach:
// 1. Load balancing
// 2. Database sharding/replication
// 3. Cache layer (Redis)
// 4. CDN for static assets
// 5. Async processing (queues)
// 6. Microservices decomposition
Consistency Failure Modes

Analyze potential failure modes for Consistency and design mitigation strategies.

Solution
// Failure mitigation:
// 1. Redundancy (multi-AZ)
// 2. Circuit breakers
// 3. Retry with backoff
// 4. Dead letter queues
// 5. Health checks
// 6. Graceful degradation

Quiz

1. What is the main difference between strong and eventual consistency?

Question 1 options

2. Which consistency model is best for a banking application?

Question 2 options

3. What is causal consistency?

Question 3 options

4. What is the tradeoff of strong consistency?

Question 4 options

Flashcards

Question

What is strong consistency?

Answer

All nodes have the same data immediately after a write. Reads from any node return the most recent value. Slower but ensures correctness.

Question

What is eventual consistency?

Answer

Nodes may temporarily have different values after a write, but eventually converge to the same value. Faster but may return stale data.

Question

What is causal consistency?

Answer

Causally related operations appear in order across all nodes. Unrelated operations may be seen in different orders. Middle ground between strong and eventual.

Question

What is read-your-writes consistency?

Answer

A client always sees their own writes, even if other nodes haven't caught up yet. Common in user-facing applications.

Question

What is Consistency?

Answer

Consistency is a key concept in system design.

Revision Notes

Key Takeaways

  • 1.Strong consistency ensures immediate data visibility across all nodes
  • 2.Eventual consistency trades correctness for performance and availability
  • 3.Causal consistency preserves order of related operations
  • 4.Choose consistency level based on use case requirements
  • 5.Consistency, availability, and performance involve tradeoffs

Interview Tips

  • Always discuss the consistency requirements for each component
  • Explain the tradeoffs between consistency and performance
  • Consider read-your-writes consistency for user-facing features
  • Use specific examples: banking needs strong, social media needs eventual

Cheat Sheet

Consistency - Cheat Sheet

Consistency Spectrum:
Stronger ←──────────────→ Weaker
Linearizability > Sequential > Causal > Eventual

Strong Consistency:

  • All nodes have same data immediately
  • Slower (must replicate)
  • Lower availability
  • Use: Banking, inventory

Eventual Consistency:

  • Nodes converge eventually
  • Faster (write and return)
  • Higher availability
  • Use: Social media, analytics

Causal Consistency:

  • Causally related ops in order
  • Middle ground
  • Use: Chat, social feeds

Tradeoffs:
Consistency ↔ Performance ↔ Availability
You can optimize for 2, not all 3