Jeff’s Note #
Unlike generic exam dumps, ADH analyzes this scenario through the lens of a Real-World Lead Developer.
For DVA-C02 candidates, the confusion often lies in choosing the optimal DynamoDB API call to minimize read capacity unit consumption. In production, this is about knowing exactly when to use Query vs Scan and how global secondary indexes impact efficiency. Let’s drill down.
The Certification Drill (Simulated Question) #
Scenario #
A fintech startup called “FinSolve” maintains a DynamoDB table named transactions to track user purchases. The table uses a primary partition key named transactionId and has a global secondary index (GSI) called userIndex, which has userId as a partition key and transactionTimestamp as a sort key. The development team needs to create an AWS Lambda function to efficiently retrieve all transactions for a particular userId (value: 100) using the least amount of read capacity.
The Requirement: #
Implement a DynamoDB data retrieval method in Lambda that fetches all items with userId = 100, using minimal read capacity.
The Options #
- A) Use the
GetItemAPI operation with parameters:"TableName": "transactions", "Key": { "userId": {"S": "100"} } - B) Use the
BatchGetItemAPI operation with parameters:"RequestItems": { "transactions": { "Keys": [{ "userId": {"S": "100"} }] } } - C) Use the
ScanAPI operation with parameters:{ "TableName": "transactions", "FilterExpression": "userId = :userId", "IndexName": "userIndex", "ExpressionAttributeValues": { ":userId": {"S": "100"} } } - D) Use the
QueryAPI operation with parameters:"TableName": "transactions", "IndexName": "userIndex", "KeyConditionExpression": "userId = :userId", "ExpressionAttributeValues": { ":userId": {"S": "100"} }
Google adsense #
leave a comment:
Correct Answer #
D
Quick Insight: The Developer Imperative #
DynamoDB
Queryoperations on a GSI are designed to efficiently retrieve all items matching a partition key condition with optimized read capacity consumption, unlikeScanwhich reads the entire index and filters afterward.GetItemandBatchGetItemrequire full primary key values, which are not suitable here since the query is on the GSI.
Content Locked: The Expert Analysis #
You’ve identified the answer. But do you know the implementation details that separate a Junior from a Senior?
The Expert’s Analysis #
Correct Answer #
Option D
The Winning Logic #
A DynamoDB Query operation on the userIndex GSI specifically retrieves all items with a partition key of userId = 100 in a highly efficient manner. This avoids scanning the entire table or index and thus consumes the least read capacity units (RCUs). Query uses the KeyConditionExpression to directly fetch matching partitions, which is optimal for this use case.
The Trap (Distractor Analysis): #
- Why not A?
GetItemrequires a full primary key (partition and sort key if any). Here,userIdis only a GSI partition key, so it cannot be used alone withGetItem. This request would fail or return unexpected results. - Why not B?
BatchGetItemworks with full primary keys and is designed for fetching multiple specific items by their exact keys. It cannot fetch items by GSI partition key alone. - Why not C?
Scanwith a filter reads every item (or every index entry if specifyingIndexName) and then filters client-side. This has the highest read capacity cost and latency among the options and should be avoided if possible.
The Technical Blueprint #
// Example Lambda function snippet to perform the Query operation
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB();
const params = {
TableName: 'transactions',
IndexName: 'userIndex',
KeyConditionExpression: 'userId = :userId',
ExpressionAttributeValues: {
':userId': { S: '100' }
}
};
exports.handler = async () => {
try {
const result = await dynamodb.query(params).promise();
return result.Items;
} catch (error) {
console.error('Error querying DynamoDB:', error);
throw error;
}
};
The Comparative Analysis #
| Option | API Complexity | Performance | Use Case |
|---|---|---|---|
| A | Simple but incorrect usage | Invalid request (wrong key) | Requires full PK; can’t query by GSI key |
| B | Moderate | Inefficient & inappropriate | Batch gets require full primary keys |
| C | Simple | Highest latency and RCU cost | Scans entire index; least efficient |
| D | Moderate | Most efficient; lowest RCU cost | Correct usage of Query on GSI to fetch by partition key |
Real-World Application (Practitioner Insight) #
Exam Rule #
“For the exam, always pick Query when you need to fetch all items by a known partition key, especially using a GSI.”
Real World #
“In real production systems, leveraging Query over Scan drastically reduces costs and improves latency, and is a foundational pattern for efficient DynamoDB data access.”
(CTA) Stop Guessing, Start Mastering #
Disclaimer
This is a study note based on simulated scenarios for the AWS DVA-C02 exam.