Question

Difficulty: MediumData Store Operations with Amazon DynamoDB

A developer is building a recipe sharing platform. The recipes are stored in an Amazon DynamoDB table with `RecipeID` as the partition key. Each recipe item contains attributes such as `Title`, `PrepTime`, and `Category` (e.g., 'Dessert', 'Main Course'). The application homepage needs to display all recipes in the 'Dessert' category. The developer wants to retrieve these items while minimizing the latency and the consumption of Read Capacity Units (RCUs).

Which approach should the developer take to retrieve these recipes?

  1. A
    Perform a `Scan` operation on the base table with a `FilterExpression` to retrieve only the items where `Category` equals 'Dessert'.
  2. Create a Global Secondary Index (GSI) with `Category` as the partition key, and perform a `Query` operation on the GSI using the category value.Answer
  3. C
    Increase the provisioned Read Capacity Units (RCUs) of the base table to prevent throttling, and execute a `Scan` operation on the table to filter for the 'Dessert' category in the application memory.
  4. D
    Initialize the AWS SDK client inside the application using hardcoded AWS credentials to minimize IAM authorization latency, and perform a `Scan` operation on the base table.

Answer

Create a Global Secondary Index (GSI) with `Category` as the partition key, and perform a `Query` operation on the GSI using the category value.
The correct answer is to create a Global Secondary Index (GSI) with `Category` as the partition key and query the GSI. Because the base table's partition key is `RecipeID`, querying by `Category` is not directly supported on the base table. By creating a GSI, you can perform a `Query` operation that target-retrieves only the items matching the 'Dessert' category. This consumes RCUs only for the matched items and projected attributes, offering low latency and maximum cost-efficiency.

Step-by-Step Solution

1
Analyze the table structure and the retrieval requirements.
The base table uses `RecipeID` as the partition key. To find all items in a specific category, a query on the base table is not possible because it requires a specific `RecipeID` in the KeyConditionExpression.
This establishes that querying the base table directly for a category is not viable.
2
Compare the impact of Scan vs Indexing.
A Scan operation reads all items in the base table, consuming RCUs proportional to the total size of the table. Creating a GSI with `Category` as the partition key allows the application to query the GSI directly.
This shows how a GSI changes the access pattern from a scan to a targeted query.
3
Select the optimal strategy that minimizes RCUs and latency.
Querying the GSI using the category value as the key condition retrieves only the items that match the category, minimizing RCUs consumed and reducing latency.
This identifies the most efficient and cost-effective approach.

Key Concept

Using Global Secondary Indexes (GSIs) to enable query operations on non-key attributes in Amazon DynamoDB.
Rate this question