DynamoDB consistency models

5 minutes 5 Questions

Amazon DynamoDB offers two consistency models that developers must understand for optimal application design: Eventually Consistent Reads and Strongly Consistent Reads. **Eventually Consistent Reads (Default)** This is the default read consistency model in DynamoDB. When you perform a read operation, the response might not reflect the results of a recently completed write operation. DynamoDB typically achieves consistency across all copies of data within one second. This option provides the best read performance and consumes fewer read capacity units (RCUs). One eventually consistent read consumes 0.5 RCUs for items up to 4 KB. **Strongly Consistent Reads** When you request a strongly consistent read, DynamoDB returns a response with the most up-to-date data, reflecting all successful write operations that occurred before the read. This option is essential when your application requires the latest data. However, strongly consistent reads consume more resources - one strongly consistent read uses 1 RCU for items up to 4 KB. **Key Considerations for Developers:** 1. **Performance vs. Accuracy**: Choose eventually consistent reads when slight delays in data accuracy are acceptable, as they offer better throughput and lower costs. 2. **API Configuration**: Set the ConsistentRead parameter to true in GetItem, Query, or Scan operations to enable strong consistency. 3. **Global Tables**: Strongly consistent reads are not supported for global tables' replicas in different regions. 4. **Cost Implications**: Eventually consistent reads are more cost-effective since they consume half the read capacity. 5. **Use Cases**: Banking transactions or inventory systems typically require strong consistency, while social media feeds can tolerate eventual consistency. Understanding these models helps developers design efficient applications that balance data accuracy requirements with performance and cost optimization in their DynamoDB implementations.

DynamoDB Consistency Models - Complete Guide

Why DynamoDB Consistency Models Are Important

Understanding consistency models is crucial for AWS Developer Associate exam success and real-world application design. DynamoDB offers different consistency options that affect data accuracy, performance, and cost. Choosing the wrong consistency model can lead to stale data reads or unnecessary expenses.

What Are DynamoDB Consistency Models?

DynamoDB provides two primary consistency models for read operations:

1. Eventually Consistent Reads (Default)
- Data is replicated across multiple Availability Zones
- Reads might not reflect the most recent write operation
- Typically achieves consistency within one second
- Consumes half the read capacity units compared to strongly consistent reads
- Best for applications that can tolerate slightly stale data

2. Strongly Consistent Reads
- Returns the most up-to-date data
- Reflects all successful write operations prior to the read
- Consumes twice the read capacity units of eventually consistent reads
- May have higher latency
- Required when applications need the latest data

How It Works

DynamoDB stores data across three geographically distributed facilities. When you write data:

1. The write is acknowledged after being stored in at least two facilities
2. Data then propagates to the third facility
3. Eventually consistent reads may retrieve data from any facility
4. Strongly consistent reads retrieve data from the leader node, ensuring the latest version

Configuring Consistency

To request a strongly consistent read, set the ConsistentRead parameter to true in your API calls:
- GetItem
- Query
- Scan

Note: Global Secondary Indexes (GSIs) only support eventually consistent reads. Local Secondary Indexes (LSIs) support both consistency models.

Capacity Unit Calculations

For items up to 4 KB:
- Eventually Consistent Read: 0.5 RCU per read
- Strongly Consistent Read: 1 RCU per read

For larger items, round up to the nearest 4 KB increment.

Exam Tips: Answering Questions on DynamoDB Consistency Models

Key Points to Remember:

1. Default behavior is eventually consistent - If a question doesn't specify consistency type, assume eventually consistent reads

2. Cost implications - Questions about reducing costs often have eventually consistent reads as the answer since they use half the RCUs

3. GSI limitations - Global Secondary Indexes ONLY support eventually consistent reads. If a question mentions GSI with strongly consistent reads, that option is incorrect

4. LSI flexibility - Local Secondary Indexes support BOTH consistency models

5. Use case scenarios:
- Banking/financial transactions requiring accuracy = Strongly Consistent
- Social media feeds, product catalogs = Eventually Consistent
- Real-time inventory systems = Strongly Consistent

6. Latency considerations - Strongly consistent reads may have higher latency; questions about minimizing latency often favor eventually consistent reads

7. The ConsistentRead parameter - Remember this is how you enable strongly consistent reads in API calls

8. Transactional reads - DynamoDB transactions provide ACID compliance and consume 2 RCUs per 4 KB (double the strongly consistent read cost)

Common Exam Scenarios:

- Scenario asking for most cost-effective solution: Choose eventually consistent reads
- Scenario requiring latest data after a write: Choose strongly consistent reads
- Scenario involving GSI queries needing strong consistency: This is a trick question - it's not possible
- Scenario calculating RCU requirements: Pay attention to which consistency model is specified
Test mode:
More DynamoDB consistency models questions
27 questions (total)

Other Development with AWS Services concepts

Amazon ElastiCacheAmazon EventBridge for event-driven patternsAmazon Kinesis Data StreamsAmazon OpenSearch ServiceAmazon Q DeveloperAPI response and request transformationsAPI validation rulesAWS SDKs and APIsCaching strategiesChoosing data stores by access patternsChoreography vs orchestration patternsCircuit breaker patternCreating and maintaining APIsData lifecycle managementData persistence patternsData serialization and deserializationDAX (DynamoDB Accelerator)DynamoDB global secondary indexesDynamoDB local secondary indexesDynamoDB partition keysDynamoDB primary keysDynamoDB query operationsDynamoDB scan operationsError handling patternsEventBridge rules and targetsEvent-driven architectural patternsEventually consistent readsFanout patternFault-tolerant application developmentHandling streaming dataHigh-cardinality partition key designLambda concurrency settingsLambda data processing and transformationLambda dead-letter queuesLambda destinationsLambda environment variablesLambda event lifecycleLambda extensionsLambda integration with AWS servicesLambda layersLambda memory and timeout configurationLambda performance tuningLambda Provisioned ConcurrencyLambda real-time stream processingLambda runtime and handler configurationLambda testing strategiesLambda triggers and event sourcesLambda VPC access configurationManaging data storesMicroservices architectureMonolithic architectureOverriding API status codesResilient application patternsRetry logic implementationStateful vs stateless applicationsStrongly consistent readsSynchronous vs asynchronous patternsTightly coupled vs loosely coupled componentsUnit testing with AWS SAMWriting code for messaging services