Question

Difficulty: MediumData Store Operations with Amazon DynamoDB

A developer is designing a backend service for a ride-sharing application that stores ride details in an Amazon DynamoDB table. The base table uses `RideId` as the partition key. Each item contains attributes such as `RiderId`, `DriverId`, `Fare`, `RideDate`, and `Status` (which can be 'Requested', 'Ongoing', or 'Completed').

The application needs to retrieve the 5050 most recent completed rides for a specific driver to display on a dashboard.

Which approach is the most efficient and cost-effective to meet these requirements?

  1. A
    Perform a `Scan` operation on the base table with a filter expression matching the specific `DriverId` and `Status` of 'Completed', then sort the retrieved items by `RideDate` in the application code.
  2. Create a Global Secondary Index (GSI) with `DriverId` as the partition key and `RideDate` as the sort key. Perform a `Query` operation on the GSI for the specific driver, setting `ScanIndexForward` to `false`, using a filter expression for the completed status, and setting the `Limit` to 5050.Answer
  3. C
    Perform a `Scan` operation on the base table using a filter expression. If the operation throttles and returns a `ProvisionedThroughputExceededException`, increase the provisioned Read Capacity Units (RCUs) of the base table to handle the high volume of scanned items.
  4. D
    Initialize the AWS SDK client in the backend code by hardcoding a dedicated IAM user's credentials with read-only access to the DynamoDB table, then perform a parallel `Scan` operation to retrieve the rides.

Answer

Create a Global Secondary Index (GSI) with `DriverId` as the partition key and `RideDate` as the sort key. Perform a `Query` operation on the GSI for the specific driver, setting `ScanIndexForward` to `false`, using a filter expression for the completed status, and setting the `Limit` to 5050.
The correct approach involves creating a Global Secondary Index (GSI) with the driver identifier as the partition key and the ride date as the sort key. This allows the application to query directly for the specific driver. Setting the sort order parameter to false returns items in descending order, and specifying a limit of 50 restricts the read operation to only the required dataset, keeping costs low and performance high.

Step-by-Step Solution

1
Determine the partition and sort key requirements based on the query patterns.
Identify that the search attribute (`DriverId`) is not the base table's partition key, requiring an index to avoid scanning.
DynamoDB queries require matching the partition key of either the base table or an index to locate items efficiently.
2
Define a Global Secondary Index (GSI) with `DriverId` as the partition key and `RideDate` as the sort key.
Enables sorting records chronologically per driver directly on the index storage.
GSIs allow redefining partition and sort keys for flexible query patterns on non-key attributes.
3
Configure the Query operation with `ScanIndexForward` set to `false` and a `Limit` of 5050.
Retrieves only the latest 5050 matching elements in descending order, minimizing RCU consumption.
Setting `ScanIndexForward` to `false` reverses the sort order, and setting `Limit` prevents reading extra items beyond the required dashboard threshold.

Key Concept

Using Global Secondary Indexes (GSIs) and Query operations to perform optimized lookups on non-key attributes in Amazon DynamoDB.
Rate this question