Question

Difficulty: HardData Store Operations with Amazon DynamoDB

A developer is optimizing a multiplayer gaming application backend. The application tracks player performance in real time and stores match results in an Amazon DynamoDB table. The table has `MatchId` as the partition key and `PlayerId` as the sort key. Each item contains additional attributes such as `Score`, `Duration`, and `Region`. For a post-match leaderboard display, the application needs to retrieve only the players who scored more than 10,00010,000 points in a specific match. The target match contains approximately 5,0005,000 player records, but typically fewer than 5050 players achieve a score above 10,00010,000. Which approach will retrieve this data with the lowest latency and minimum Read Capacity Unit (RCU) consumption?

  1. Create a Global Secondary Index (GSI) with `MatchId` as the partition key and `Score` as the sort key, and then perform a `Query` operation on the GSI using a `KeyConditionExpression` for both keys.Answer
  2. B
    Perform a `Query` operation on the base table using the partition key `MatchId`, and apply a `FilterExpression` of `Score > 10000` to filter the results.
  3. C
    Perform a `Scan` operation on the base table with a `FilterExpression` checking if `MatchId` matches the target match and `Score` is greater than 10,000.
  4. D
    Initialize the DynamoDB client in the application code using static IAM Access Keys, and run a parallel `Scan` operation with segment filters to retrieve the data.

Answer

Create a Global Secondary Index (GSI) with `MatchId` as the partition key and `Score` as the sort key, and then perform a `Query` operation on the GSI using a `KeyConditionExpression` for both keys.
The correct approach is to create a Global Secondary Index (GSI) with the match identifier as the partition key and the score as the sort key. By doing so, the query operation can utilize both attributes in its key condition expression. Since DynamoDB only bills RCUs for items returned by a key condition expression, this consumes minimal capacity (only for the matching records) and minimizes retrieval latency.

Step-by-Step Solution

1
Identify the data access pattern and constraints.
The application needs to retrieve a small subset of records (fewer than 5050 out of 5,0005,000) within a specific partition (`MatchId`) based on a non-key attribute (`Score`).
Understanding the data volume and distribution helps determine the most cost-effective and low-latency retrieval method.
2
Evaluate the behavior of FilterExpression vs KeyConditionExpression.
A FilterExpression on the base table Query would read all 5,0005,000 records before filtering, consuming unnecessary RCUs. A KeyConditionExpression requires the attribute to be part of the primary key or index key.
Filtering must occur at the storage layer before RCU calculation to optimize costs.
3
Design a secondary index to support the query requirements.
Create a GSI with `MatchId` as the partition key and `Score` as the sort key. This allows both attributes to be used in the KeyConditionExpression.
GSIs allow sorting and querying on attributes other than the base table's primary keys, enabling highly targeted reads that only consume RCUs for matching records.

Key Concept

Optimizing DynamoDB retrieval using Global Secondary Indexes (GSI) and KeyConditionExpressions instead of FilterExpressions or Scans.
Estimated Time:2m 0s
Rate this question