Question

Difficulty: MediumData Store Operations with Amazon DynamoDB

A developer is building a workflow management application that tracks task history in an Amazon DynamoDB table. The table has TaskId\text{TaskId} as the partition key and UpdateTimestamp\text{UpdateTimestamp} as the sort key. The application needs to retrieve the single most recent update for a specific task to display on a dashboard. Which DynamoDB API configuration should the developer use to retrieve this item with the lowest latency and minimal Read Capacity Unit (RCU) consumption?

  1. A
    Invoke the Scan API with a filter expression for the TaskId\text{TaskId} attribute, sort the returned array by UpdateTimestamp\text{UpdateTimestamp} in the application code, and select the first item.
  2. B
    Initialize the AWS SDK DynamoDB client by passing hardcoded IAM access keys directly into the client constructor, and invoke the GetItem API using only the TaskId\text{TaskId} attribute.
  3. Invoke the Query API with a key condition expression for the TaskId\text{TaskId}, set the ScanIndexForward\text{ScanIndexForward} parameter to false, and set the Limit\text{Limit} parameter to 11.Answer
  4. D
    Invoke the Query API to retrieve all updates for a given TaskId\text{TaskId}, and if a ProvisionedThroughputExceededException is thrown, resolve it by increasing the overall provisioned Read Capacity Units (RCUs) for the table.

Answer

Invoke the Query API with a key condition expression for the partition key, set ScanIndexForward to false, and set the Limit parameter to 1.
To retrieve the latest item under a specific partition key in a table with a composite primary key, the most efficient method is to perform a `Query` operation. By specifying the partition key in the key condition expression, setting `ScanIndexForward` to `false` (which reverses the sort key order to descending), and setting `Limit` to 11, DynamoDB only reads and returns the single most recent item. This minimizes the read capacity units (RCUs) consumed.

Step-by-Step Solution

1
Analyze the table schema and data retrieval requirements.
The table has a composite primary key consisting of a partition key (TaskId) and a sort key (UpdateTimestamp). We need to find the latest record for a specific TaskId.
Since the sort key is a timestamp, DynamoDB naturally stores items for the same partition key sorted by this timestamp.
2
Select the correct operation API.
Choose the Query API instead of GetItem or Scan.
GetItem requires both partition and sort keys, which is impossible since the exact timestamp is unknown. Scan reads the entire table, which is highly inefficient.
3
Optimize the Query operation parameters.
Apply a key condition expression for the TaskId, set ScanIndexForward to false to sort in descending order (latest first), and set Limit to 1.
This instructs DynamoDB to scan only the single latest item under that partition key, minimizing latency and RCU consumption to the absolute minimum.

Key Concept

Optimizing read operations on composite keys using the DynamoDB Query API with ScanIndexForward and Limit
Estimated Time:1m 30s
Rate this question