Question

Difficulty: HardData Store Operations with Amazon DynamoDB

A video streaming platform uses an Amazon DynamoDB table to track user watch progress. The table uses UserIdUserId as the partition key and VideoIdVideoId as the sort key. As users watch videos, progress updates create a high volume of writes. The platform's homepage must display the 1010 most recently watched videos that are currently in progress (where the CompletionStatusCompletionStatus attribute is IN_PROGRESSIN\_PROGRESS), sorted by the LastUpdatedLastUpdated timestamp in descending order.

To minimize both Read Capacity Unit (RCU) consumption and query latency, which strategy should the developer implement?

  1. A
    Perform a Scan operation on the base table using a FilterExpression of UserId = :uid AND CompletionStatus = :status, then sort and limit the results to the top 10 items in the application memory.
  2. Create a Global Secondary Index (GSI) with UserId as the partition key and InProgressTimestamp as the sort key. Populate InProgressTimestamp with the update timestamp only when CompletionStatus is IN_PROGRESS, otherwise leave it blank. Query this GSI with ScanIndexForward set to false.Answer
  3. C
    Query the base table using only UserId as the partition key to retrieve all watch history records, and if the client encounters a ProvisionedThroughputExceededException due to high read volume, scale up the provisioned Read Capacity Units (RCUs) for the base table.
  4. D
    Initialize the AWS SDK client inside the application by hardcoding temporary IAM Access Key ID and Secret Access Key credentials directly in the constructor, then run a Scan query on the base table to filter the records.

Answer

Create a Global Secondary Index (GSI) with UserId as the partition key and InProgressTimestamp as the sort key. Only populate InProgressTimestamp when CompletionStatus is IN_PROGRESS, and query the GSI with ScanIndexForward set to false.
The correct strategy is to create a sparse Global Secondary Index (GSI). By defining a sort key (such as InProgressTimestamp) that is only populated when the status is 'IN_PROGRESS', DynamoDB will automatically exclude all 'COMPLETED' records from the index. Querying this GSI by UserId with ScanIndexForward set to false retrieves only the relevant, in-progress items in descending order of the timestamp, minimizing RCU usage and latency.

Step-by-Step Solution

1
Identify the performance and cost bottleneck in the access pattern.
Querying the base table or scanning it for 'IN_PROGRESS' items scans unnecessary data, wasting Read Capacity Units (RCUs) and increasing latency.
DynamoDB charges RCUs based on the size of the data read before filtering. To optimize cost and speed, only the relevant items should be read.
2
Design a sparse secondary index to isolate the required dataset.
A Global Secondary Index (GSI) is created with UserId as the partition key and InProgressTimestamp as the sort key. InProgressTimestamp is only populated for in-progress items.
DynamoDB sparse indexes only include items that possess the index key attributes. This excludes completed videos from the GSI, making it small and efficient to query.
3
Execute the query using appropriate sort configuration.
Query the GSI with ScanIndexForward set to false and a limit of 10.
Setting ScanIndexForward to false returns the results sorted by the sort key (InProgressTimestamp) in descending order, fulfilling the chronological requirement.

Key Concept

Sparse Global Secondary Indexes (GSIs) for optimized querying and cost management in DynamoDB.
Rate this question