A SaaS billing application stores invoice records in an Amazon DynamoDB table. The table has `CustomerId` as the partition key and `InvoiceId` as the sort key. An `InvoiceStatus` attribute indicates whether the invoice is `PAID` or `UNPAID`. Approximately of all invoices are `PAID`. A developer needs to build a dashboard feature that retrieves only the `UNPAID` invoices for a specific customer. Which of the following strategies is the most performant and cost-effective way to retrieve these records?
- Create a Global Secondary Index (GSI) with `CustomerId` as the partition key and a new attribute `UnpaidTimestamp` as the sort key, which is only populated when `InvoiceStatus` is `UNPAID`. Query this GSI using the `CustomerId`.Answer
- BPerform a Scan operation on the base table using a `FilterExpression` to filter items where `CustomerId` matches the target customer and `InvoiceStatus` is `UNPAID`.
- CCreate a Global Secondary Index (GSI) using `InvoiceStatus` as the partition key and `CustomerId` as the sort key. Query this GSI using `InvoiceStatus = UNPAID` and `CustomerId`.
- DQuery the base table using `CustomerId` to retrieve all invoices, filter for `UNPAID` status in the application logic, and initialize the AWS SDK client using hardcoded AWS IAM access keys.
Answer
Creating a sparse Global Secondary Index (GSI) with CustomerId as the partition key and a conditional attribute like UnpaidTimestamp as the sort key, then querying that GSI.
The correct strategy uses a sparse Global Secondary Index (GSI). By defining the GSI with CustomerId as the partition key and a custom attribute (such as UnpaidTimestamp) as the sort key that is only written when the invoice is UNPAID, DynamoDB will only index the unpaid invoices. Since 98% of the invoices are PAID, they will not have the UnpaidTimestamp attribute and will be excluded from the GSI. This minimizes the storage size of the GSI and allows highly efficient, low-cost Query operations restricted to the target customer's unpaid invoices.
Step-by-Step Solution
Key Concept
Sparse Global Secondary Indexes (GSIs) for filtering low-cardinality subsets of data.