A video streaming platform uses an Amazon DynamoDB table to track user watch progress. The table uses as the partition key and as the sort key. As users watch videos, progress updates create a high volume of writes. The platform's homepage must display the most recently watched videos that are currently in progress (where the attribute is ), sorted by the timestamp in descending order.
To minimize both Read Capacity Unit (RCU) consumption and query latency, which strategy should the developer implement?
- APerform 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.
- 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
- CQuery 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.
- DInitialize 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
Key Concept
Sparse Global Secondary Indexes (GSIs) for optimized querying and cost management in DynamoDB.