Skip to main content

Chapter 4: Consistency Models in DynamoDB

Introduction

Consistency in distributed systems is one of the most critical concepts to understand when working with DynamoDB, and it is one of the topics most frequently tested in system design interviews. As a distributed database that replicates data across three Availability Zones within a region, DynamoDB must balance consistency, availability, and partition tolerance according to the CAP theorem. What makes DynamoDB’s consistency model practically interesting is that it gives you a per-request choice: you can read with eventual consistency (faster, cheaper, potentially stale) or strong consistency (slower, more expensive, always fresh). This per-request flexibility means you don’t have to commit to a single consistency level for your entire application — you can use strong consistency for critical reads (like checking account balances before a transfer) and eventual consistency for tolerant reads (like displaying a product catalog). This chapter explores DynamoDB’s consistency models, their implications, and how to choose the right consistency level for your application.

Understanding Distributed Consistency

The CAP Theorem

The CAP theorem states that a distributed system can provide at most two of the following three guarantees:
  • Consistency: All nodes see the same data at the same time
  • Availability: Every request receives a response (success or failure)
  • Partition Tolerance: The system continues to operate despite network partitions
DynamoDB chooses Availability and Partition Tolerance (AP), offering eventual consistency by default with an option for strong consistency on reads. It is worth noting that the CAP theorem, while a useful mental framework, is somewhat of a simplification. Daniel Abadi’s PACELC theorem provides a more nuanced view: even when there is no partition (the normal case), a system must choose between latency (L) and consistency (C). DynamoDB’s two consistency options map directly to this: eventually consistent reads optimize for latency, while strongly consistent reads optimize for consistency, even when the network is functioning perfectly.

Replication in DynamoDB

DynamoDB automatically replicates data across three Availability Zones within a region:

DynamoDB Consistency Models

Eventual Consistency (Default)

With eventually consistent reads, responses might not reflect recently completed writes. However, consistency is typically achieved within one second. Characteristics:
  • Best performance: Lower latency and higher throughput
  • Cost-efficient: Consumes half the RCUs of strongly consistent reads
  • Eventually consistent: Reads may return stale data temporarily
  • Default option: Used unless you specify otherwise
Timeline Example:

Strong Consistency

Strongly consistent reads return a result that reflects all successful writes prior to the read. Characteristics:
  • Strong guarantees: Always returns the most recent data
  • Higher cost: Consumes twice the RCUs of eventual consistency
  • Slightly higher latency: Must coordinate across replicas
  • Opt-in: Must explicitly request
Timeline Example:

Read-After-Write Consistency

DynamoDB provides read-after-write consistency for specific scenarios: Guaranteed Read-After-Write Consistency:
  1. GetItem with same key immediately after PutItem/UpdateItem
  2. Query/Scan of newly written items (within same session)
  3. Transactional reads (TransactGetItems)

Consistency Across Operations

GetItem and Query Operations

Scan Operations

BatchGetItem

Global Secondary Indexes

Important: GSIs only support eventual consistency.

Transactions

DynamoDB transactions provide ACID guarantees with serializable isolation.

Consistency Trade-offs

When to Use Each Consistency Model

Use Eventual Consistency When:

  1. High throughput is priority
  1. Cost optimization
  1. Reading from GSIs
  1. Caching scenarios

Use Strong Consistency When:

  1. Financial transactions
  1. Inventory management
  1. Read-after-write scenarios
  1. Critical data verification

Consistency in Distributed Scenarios

Multi-Region Considerations

When using DynamoDB Global Tables, additional consistency considerations apply:
Global Table Consistency Timeline:

Conflict Resolution

In multi-region setups, DynamoDB uses last-writer-wins based on timestamps:

Application-Level Consistency Patterns

Pattern 1: Optimistic Locking

Use version numbers to prevent lost updates:

Pattern 2: Idempotent Writes

Use client tokens to prevent duplicate writes:

Pattern 3: Event Sourcing

Store events immutably for eventual consistency:

Pattern 4: CQRS (Command Query Responsibility Segregation)

Separate read and write models:

Consistency Monitoring

Detecting Consistency Issues

CloudWatch Metrics

Interview Questions and Answers

Question 1: Explain the difference between eventual consistency and strong consistency in DynamoDB.

Answer: Eventual Consistency (default):
  • Reads may return stale data immediately after writes
  • Data becomes consistent typically within one second
  • Reads from any of the three replicas
  • Consumes half the RCUs (0.5 RCU per 4KB read)
  • Best for high throughput and cost optimization
  • Example: Social media feeds, analytics
Strong Consistency:
  • Always returns the most recent data
  • Reads coordinate across replicas to ensure latest value
  • Consumes double the RCUs (1 RCU per 4KB read)
  • Slightly higher latency due to coordination
  • Best for critical data accuracy
  • Example: Financial transactions, inventory
Key trade-off: Performance and cost vs. data freshness

Question 2: Can you use strong consistency with Global Secondary Indexes?

Answer: No. GSIs only support eventually consistent reads. Reason: GSIs are asynchronously updated from the base table. The propagation from base table to GSI typically takes milliseconds but is not instantaneous. Workaround:

Question 3: How does DynamoDB handle consistency in multi-region Global Tables?

Answer: Consistency Model: Global Tables provide eventual consistency across regions with last-writer-wins conflict resolution. How it works:
  1. Writes succeed in the local region immediately
  2. Changes replicate asynchronously to other regions
  3. Typical replication latency: < 1 second
  4. During conflicts, timestamp determines winner
Example:
Handling conflicts:
  • Use version numbers for optimistic locking
  • Design idempotent operations
  • Implement application-level conflict resolution
  • Consider region affinity for related writes

Question 4: What is read-after-write consistency and when does DynamoDB provide it?

Answer: Read-after-write consistency guarantees that a read immediately following a write will see that write. DynamoDB provides this for:
  1. GetItem/Query on the same partition key immediately after PutItem/UpdateItem
  2. Within the same request thread/session
  3. TransactGetItems following TransactWriteItems
Example:
Not provided for:
  • GSI reads (eventual only)
  • Different client sessions
  • BatchGetItem operations

Question 5: How would you implement a bank account system requiring strong consistency?

Answer:
Key points:
  • Use transactions for atomic multi-item updates
  • Always use strong consistency for balance reads
  • Implement version numbers for optimistic locking
  • Use conditional expressions to prevent invalid states

Question 6: What are the cost implications of using strong consistency?

Answer: Read Capacity Units (RCUs):
  • Eventual consistency: 0.5 RCU per 4KB read
  • Strong consistency: 1 RCU per 4KB read
  • Cost difference: 2x more expensive
Example calculation:
Recommendations:
  • Use eventual consistency by default
  • Reserve strong consistency for critical operations
  • Monitor your consistency usage patterns
  • Consider caching for frequently read data

Question 7: How do you test consistency behavior in your application?

Answer:

Question 8: Explain the CAP theorem and how DynamoDB fits into it.

Answer: CAP Theorem: In a distributed system, you can guarantee at most two of:
  • Consistency: All nodes see the same data
  • Availability: Every request gets a response
  • Partition tolerance: System works despite network failures
DynamoDB’s choice: AP (Availability + Partition Tolerance) Why:
  1. Availability is critical for most applications
  2. Network partitions are inevitable in distributed systems
  3. Partition tolerance is non-negotiable
How DynamoDB handles it:
  • Prioritizes availability during network partitions
  • Offers eventual consistency by default (favors A over C)
  • Provides strong consistency as an option (temporarily favors C)
  • Uses quorum-based replication (write to 2 of 3 replicas)
Example during partition:
Trade-off: Slight staleness (< 1s) for high availability

Question 9: How would you design a system that needs both high throughput and strong consistency?

Answer: Strategy: Hybrid approach using CQRS pattern
Key techniques:
  1. Use strong consistency only where critical (orders, inventory)
  2. Use eventual consistency for high-throughput reads (history, analytics)
  3. Implement caching for frequently accessed data
  4. Separate read and write models (CQRS)
  5. Use denormalization to reduce need for strong consistency

Question 10: What happens if you write to DynamoDB and immediately read from a GSI?

Answer: Short answer: The read might not see the write immediately. Detailed explanation: Write to base table:
Timeline:
Solutions:
  1. Design for eventual consistency:
  1. Use base table for immediate reads:
  1. Retry with exponential backoff:

Summary

Key Takeaways:
  1. Consistency Models:
    • Eventual consistency: Default, cheaper, higher throughput
    • Strong consistency: Opt-in, more expensive, guaranteed fresh data
  2. When to Use What:
    • Eventual: Analytics, caching, high-throughput scenarios
    • Strong: Financial data, inventory, critical operations
  3. Limitations:
    • GSIs only support eventual consistency
    • Global Tables use eventual consistency across regions
    • BatchGetItem doesn’t support strong consistency
  4. Best Practices:
    • Use eventual consistency by default
    • Reserve strong consistency for critical paths
    • Implement application-level consistency (versioning, idempotency)
    • Monitor consistency metrics
  5. Patterns:
    • Optimistic locking for conflict detection
    • CQRS for separating read/write models
    • Event sourcing for audit trails
    • Caching for ultra-high throughput
Understanding consistency models is crucial for building correct, performant, and cost-effective DynamoDB applications.