Chapter 4: Consistency Models in DynamoDB
Introduction
Consistency in distributed systems is one of the most critical concepts to understand when working with DynamoDB. As a distributed database that replicates data across multiple Availability Zones, DynamoDB must balance consistency, availability, and partition tolerance according to the CAP theorem. 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
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
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
Read-After-Write Consistency
DynamoDB provides read-after-write consistency for specific scenarios: Guaranteed Read-After-Write Consistency:- GetItem with same key immediately after PutItem/UpdateItem
- Query/Scan of newly written items (within same session)
- 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:
- High throughput is priority
- Cost optimization
- Reading from GSIs
- Caching scenarios
Use Strong Consistency When:
- Financial transactions
- Inventory management
- Read-after-write scenarios
- Critical data verification
Consistency in Distributed Scenarios
Multi-Region Considerations
When using DynamoDB Global Tables, additional consistency considerations apply: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
- 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
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:- Writes succeed in the local region immediately
- Changes replicate asynchronously to other regions
- Typical replication latency: < 1 second
- During conflicts, timestamp determines winner
- 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:- GetItem/Query on the same partition key immediately after PutItem/UpdateItem
- Within the same request thread/session
- TransactGetItems following TransactWriteItems
- GSI reads (eventual only)
- Different client sessions
- BatchGetItem operations
Question 5: How would you implement a bank account system requiring strong consistency?
Answer:- 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
- 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
- Availability is critical for most applications
- Network partitions are inevitable in distributed systems
- Partition tolerance is non-negotiable
- 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)
Question 9: How would you design a system that needs both high throughput and strong consistency?
Answer: Strategy: Hybrid approach using CQRS pattern- Use strong consistency only where critical (orders, inventory)
- Use eventual consistency for high-throughput reads (history, analytics)
- Implement caching for frequently accessed data
- Separate read and write models (CQRS)
- 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:- Design for eventual consistency:
- Use base table for immediate reads:
- Retry with exponential backoff:
Summary
Key Takeaways:-
Consistency Models:
- Eventual consistency: Default, cheaper, higher throughput
- Strong consistency: Opt-in, more expensive, guaranteed fresh data
-
When to Use What:
- Eventual: Analytics, caching, high-throughput scenarios
- Strong: Financial data, inventory, critical operations
-
Limitations:
- GSIs only support eventual consistency
- Global Tables use eventual consistency across regions
- BatchGetItem doesn’t support strong consistency
-
Best Practices:
- Use eventual consistency by default
- Reserve strong consistency for critical paths
- Implement application-level consistency (versioning, idempotency)
- Monitor consistency metrics
-
Patterns:
- Optimistic locking for conflict detection
- CQRS for separating read/write models
- Event sourcing for audit trails
- Caching for ultra-high throughput