Bird
Raised Fist0
DynamoDBquery~10 mins

Consistent vs eventually consistent reads in DynamoDB - Visual Side-by-Side Comparison

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Consistent vs eventually consistent reads
Write data to DynamoDB
Data stored in primary storage
Eventually Consistent Read
After replication delay
Replicas updated with latest data
Strongly Consistent Read
Data is written once, then reads can be either eventually consistent (may be stale) or strongly consistent (always latest).
Execution Sample
DynamoDB
PutItem: {id:1, name:'Alice'}
ReadItem (eventually consistent)
ReadItem (strongly consistent)
Write a record, then read it with two different consistency settings.
Execution Table
StepActionData SourceData ReturnedNotes
1PutItem {id:1, name:'Alice'}Primary StorageData storedData written to main storage
2ReadItem (eventually consistent)ReplicaOld or empty dataReplica might not be updated yet
3Replication delayReplicas updatingData propagatingReplicas get latest data
4ReadItem (eventually consistent)Replica{id:1, name:'Alice'}Replica now has latest data
5ReadItem (strongly consistent)Primary Storage{id:1, name:'Alice'}Always returns latest data
💡 Reads stop after returning data; eventually consistent reads may return stale data until replicas update.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
Primary Storage Dataempty{id:1, name:'Alice'}{id:1, name:'Alice'}{id:1, name:'Alice'}{id:1, name:'Alice'}{id:1, name:'Alice'}
Replica Dataemptyemptyempty or old dataupdating{id:1, name:'Alice'}{id:1, name:'Alice'}
Key Moments - 2 Insights
Why does an eventually consistent read sometimes return old data?
Because replicas may not have received the latest update yet, as shown in step 2 of the execution_table where the replica data is still empty or old.
How does a strongly consistent read ensure the latest data?
It reads directly from the primary storage which always has the latest data, as shown in step 5 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what data does the eventually consistent read return at step 2?
AThe latest data {id:1, name:'Alice'}
BAn error
COld or empty data
DData from primary storage
💡 Hint
Check the 'Data Returned' column at step 2 in the execution_table.
At which step does the replica get updated with the latest data?
AStep 3
BStep 1
CStep 5
DStep 2
💡 Hint
Look at the 'Notes' column describing replication delay in the execution_table.
If you want to always read the latest data, which read type should you use?
AEventually consistent read
BStrongly consistent read
CAny read type works the same
DRead from replicas only
💡 Hint
Refer to the 'Data Source' and 'Data Returned' columns at step 5 in the execution_table.
Concept Snapshot
Consistent vs Eventually Consistent Reads in DynamoDB:
- Write data once to primary storage.
- Eventually consistent reads read from replicas and may return stale data.
- Strongly consistent reads read from primary storage and always return latest data.
- Eventually consistent reads are faster but may lag behind.
- Use strongly consistent reads when you need the freshest data.
Full Transcript
When you write data to DynamoDB, it is stored in the primary storage. Reads can be done in two ways: eventually consistent or strongly consistent. Eventually consistent reads get data from replicas which might not be updated immediately, so they can return old data right after a write. Strongly consistent reads always get data from the primary storage, ensuring the latest data is returned. This means eventually consistent reads are faster but may be stale, while strongly consistent reads are slower but always fresh. The execution table shows the write, the first read returning possibly old data, the replication delay, and then reads returning the latest data.

Practice

(1/5)
1. What is the main difference between consistent reads and eventually consistent reads in DynamoDB?
easy
A. Eventually consistent reads always return the latest data, consistent reads may return older data.
B. Consistent reads are faster than eventually consistent reads.
C. Consistent reads always return the latest data, eventually consistent reads may return older data.
D. There is no difference; both return the same data at the same speed.

Solution

  1. Step 1: Understand consistent reads

    Consistent reads always return the most up-to-date data from DynamoDB.
  2. Step 2: Understand eventually consistent reads

    Eventually consistent reads may return data that is slightly out of date but are faster and cheaper.
  3. Final Answer:

    Consistent reads always return the latest data, eventually consistent reads may return older data. -> Option C
  4. Quick Check:

    Consistent = latest data, Eventually consistent = possibly older data [OK]
Hint: Remember: consistent = latest, eventually consistent = maybe older [OK]
Common Mistakes:
  • Thinking eventually consistent reads are always faster but not cheaper
  • Confusing which read type returns the latest data
  • Assuming both read types always return the same data
2. Which of the following is the correct way to specify a consistent read in a DynamoDB GetItem request using AWS SDK?
easy
A. "ConsistentRead": true
B. "ConsistentRead": false
C. "Consistent": true
D. "ReadConsistency": "strong"

Solution

  1. Step 1: Recall DynamoDB GetItem syntax

    The parameter to request a consistent read is exactly "ConsistentRead" set to true or false.
  2. Step 2: Identify correct syntax

    Only "ConsistentRead": true is valid; other options are incorrect parameter names or values.
  3. Final Answer:

    "ConsistentRead": true -> Option A
  4. Quick Check:

    Use "ConsistentRead": true for consistent reads [OK]
Hint: Look for exact parameter name "ConsistentRead" set to true [OK]
Common Mistakes:
  • Using incorrect parameter names like "Consistent" or "ReadConsistency"
  • Setting ConsistentRead to false to get consistent reads
  • Confusing boolean values with strings
3. Consider this DynamoDB query snippet using AWS SDK for JavaScript:
const params = {
  TableName: "Users",
  Key: { "UserId": "123" },
  ConsistentRead: false
};
const data = await dynamoDb.get(params).promise();
console.log(data.Item.lastLogin);
What can you say about the freshness of the lastLogin value printed?
medium
A. It is guaranteed to be the most recent lastLogin value.
B. It will cause a runtime error because ConsistentRead must be true.
C. It will always be null because ConsistentRead is false.
D. It may be an older lastLogin value due to eventual consistency.

Solution

  1. Step 1: Check ConsistentRead parameter

    ConsistentRead is set to false, meaning the read is eventually consistent.
  2. Step 2: Understand impact on data freshness

    Eventually consistent reads may return stale data, so lastLogin might be older than the latest update.
  3. Final Answer:

    It may be an older lastLogin value due to eventual consistency. -> Option D
  4. Quick Check:

    ConsistentRead false = possibly stale data [OK]
Hint: ConsistentRead false means data may be stale [OK]
Common Mistakes:
  • Assuming ConsistentRead false always returns latest data
  • Thinking ConsistentRead false causes errors
  • Believing data will be null if not consistent
4. You wrote this DynamoDB query but always get stale data even after updates:
const params = {
  TableName: "Orders",
  Key: { "OrderId": "789" },
  ConsistentRead: "true"
};
const result = await dynamoDb.get(params).promise();
What is the problem in this code?
medium
A. ConsistentRead must be omitted to get consistent reads.
B. ConsistentRead should be a boolean, not a string.
C. The Key attribute name is incorrect.
D. TableName should be lowercase.

Solution

  1. Step 1: Check ConsistentRead data type

    ConsistentRead must be a boolean (true/false), not a string "true".
  2. Step 2: Understand impact of wrong type

    Passing a string may cause DynamoDB to ignore the parameter, resulting in eventually consistent reads and stale data.
  3. Final Answer:

    ConsistentRead should be a boolean, not a string. -> Option B
  4. Quick Check:

    ConsistentRead must be boolean true/false [OK]
Hint: Use boolean true, not string "true" for ConsistentRead [OK]
Common Mistakes:
  • Passing ConsistentRead as string instead of boolean
  • Changing Key attribute name unnecessarily
  • Thinking TableName case matters for consistency
5. You have a high-traffic app that shows user profiles updated frequently. You want to minimize latency but ensure users see their latest profile changes immediately after saving. Which read strategy should you use in DynamoDB?
hard
A. Use consistent reads only for profile fetches immediately after updates, eventually consistent otherwise.
B. Use eventually consistent reads everywhere to reduce cost and latency.
C. Use consistent reads for all profile fetches regardless of timing.
D. Use eventually consistent reads only for profile fetches immediately after updates.

Solution

  1. Step 1: Understand trade-offs

    Consistent reads ensure latest data but cost more and have higher latency; eventually consistent reads are cheaper and faster but may be stale.
  2. Step 2: Apply strategy for user experience

    Use consistent reads right after updates to show fresh data, then eventually consistent reads for normal fetches to save cost and improve speed.
  3. Final Answer:

    Use consistent reads only for profile fetches immediately after updates, eventually consistent otherwise. -> Option A
  4. Quick Check:

    Consistent reads after update, eventually consistent otherwise [OK]
Hint: Use consistent reads only when fresh data is critical [OK]
Common Mistakes:
  • Using consistent reads everywhere causing high cost and latency
  • Using eventually consistent reads immediately after updates causing stale data
  • Ignoring cost and latency trade-offs