We want to know if the data we read is the latest or if it might be a little old. This helps us decide how fresh our information is.
Consistent vs eventually consistent reads in DynamoDB
Start learning this pattern below
Jump into concepts and practice - no test required
aws dynamodb get-item \
--table-name YourTable \
--key '{"YourKey": {"S": "YourValue"}}' \
--consistent-read true|falseSet --consistent-read to true for a consistent read (latest data).
If you omit or set it to false, you get an eventually consistent read (may be slightly old).
aws dynamodb get-item --table-name Users --key '{"UserId": {"S": "123"}}' --consistent-read trueaws dynamodb get-item --table-name Users --key '{"UserId": {"S": "123"}}'This command fetches the latest data for product A1 using a consistent read.
aws dynamodb get-item --table-name Products --key '{"ProductId": {"S": "A1"}}' --consistent-read trueConsistent reads cost more and can be slower because they always get the latest data.
Eventually consistent reads are faster and cheaper but might return data that is a little out of date.
Use consistent reads when accuracy is very important, like financial or inventory data.
Consistent reads always return the latest data.
Eventually consistent reads may return slightly older data but are faster and cheaper.
Choose the read type based on how fresh your data needs to be and your app's needs.
Practice
consistent reads and eventually consistent reads in DynamoDB?Solution
Step 1: Understand consistent reads
Consistent reads always return the most up-to-date data from DynamoDB.Step 2: Understand eventually consistent reads
Eventually consistent reads may return data that is slightly out of date but are faster and cheaper.Final Answer:
Consistent reads always return the latest data, eventually consistent reads may return older data. -> Option CQuick Check:
Consistent = latest data, Eventually consistent = possibly older data [OK]
- 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
GetItem request using AWS SDK?Solution
Step 1: Recall DynamoDB GetItem syntax
The parameter to request a consistent read is exactly "ConsistentRead" set to true or false.Step 2: Identify correct syntax
Only "ConsistentRead": true is valid; other options are incorrect parameter names or values.Final Answer:
"ConsistentRead": true -> Option AQuick Check:
Use "ConsistentRead": true for consistent reads [OK]
- Using incorrect parameter names like "Consistent" or "ReadConsistency"
- Setting ConsistentRead to false to get consistent reads
- Confusing boolean values with strings
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?Solution
Step 1: Check ConsistentRead parameter
ConsistentRead is set to false, meaning the read is eventually consistent.Step 2: Understand impact on data freshness
Eventually consistent reads may return stale data, so lastLogin might be older than the latest update.Final Answer:
It may be an older lastLogin value due to eventual consistency. -> Option DQuick Check:
ConsistentRead false = possibly stale data [OK]
- Assuming ConsistentRead false always returns latest data
- Thinking ConsistentRead false causes errors
- Believing data will be null if not consistent
const params = {
TableName: "Orders",
Key: { "OrderId": "789" },
ConsistentRead: "true"
};
const result = await dynamoDb.get(params).promise();
What is the problem in this code?Solution
Step 1: Check ConsistentRead data type
ConsistentRead must be a boolean (true/false), not a string "true".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.Final Answer:
ConsistentRead should be a boolean, not a string. -> Option BQuick Check:
ConsistentRead must be boolean true/false [OK]
- Passing ConsistentRead as string instead of boolean
- Changing Key attribute name unnecessarily
- Thinking TableName case matters for consistency
Solution
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.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.Final Answer:
Use consistent reads only for profile fetches immediately after updates, eventually consistent otherwise. -> Option AQuick Check:
Consistent reads after update, eventually consistent otherwise [OK]
- 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
