A developer is designing a subscription management system using Amazon DynamoDB. The table stores customer subscriptions with `SubscriptionID` as the partition key. Over of the subscriptions are in the "Active" status, while less than are in the "PendingCancellation" status. The developer needs to build a dashboard that lists all subscriptions in the "PendingCancellation" status. Which two actions should the developer take to retrieve this data in the most cost-effective and performant manner?
- Create a Global Secondary Index (GSI) with a sparse attribute (such as `CancellationDate`, which is only populated on subscriptions with a pending cancellation status) as the partition key.Answer
- Use the `Query` API operation to retrieve the subscriptions from the global secondary index.Answer
- CUse the `Scan` API operation with a FilterExpression to retrieve all subscriptions from the main table where `SubscriptionStatus` equals `PendingCancellation`.
- DInitialize the AWS SDK client inside the application code by hardcoding the access keys of an IAM user with read access to the DynamoDB table.
- EIncrease the provisioned Read Capacity Units (RCUs) of the main table to resolve read throttling, assuming the performance issues are due to overall table capacity limits rather than the scanning of inactive partitions.
Answer
Create a Global Secondary Index (GSI) with a sparse attribute as the partition key and use the Query API operation to retrieve the subscriptions from the index.
The correct approach is to create a Global Secondary Index (GSI) with a sparse attribute (like a cancellation date that is only present for subscriptions pending cancellation) as the partition key, and then use the Query API on this index. In DynamoDB, an item is only written to a GSI if the GSI's partition key attribute is present in that item. Since only of subscriptions have this attribute, the index size remains very small, saving storage and write costs. Querying this GSI directly retrieves only the matching items, making the operation extremely efficient.
Step-by-Step Solution
Key Concept
Optimizing DynamoDB data retrieval for sparse attributes using Global Secondary Indexes (GSIs) and the Query API rather than Table Scans.