Question

Difficulty: MediumData Store Operations with Amazon DynamoDB

A developer is designing an Amazon DynamoDB table named `UserActivities` to track user actions in a web application. The table's primary key is configured with `UserID` as the partition key and `ActivityTimestamp` as the sort key.

The application needs to support the following operations:
1. Retrieve all activities for a specific `UserID` that occurred within a particular date range, sorted by timestamp.
2. Retrieve all activities of a specific `ActivityType` (such as 'login' or 'purchase') across all users, sorted by the timestamp of the activity.

The developer wants to implement this with optimal performance and minimal read capacity consumption.

Which two actions should the developer take to meet these requirements? (Select TWO.)

  1. Query the base table directly using the UserID partition key and a key condition expression on the ActivityTimestamp sort key.Answer
  2. Create a Global Secondary Index (GSI) with ActivityType as the partition key and ActivityTimestamp as the sort key.Answer
  3. C
    Create a Local Secondary Index (LSI) with ActivityType as the partition key and ActivityTimestamp as the sort key.
  4. D
    Perform a Scan operation on the base table using a FilterExpression on the ActivityType attribute to retrieve activities across all users.
  5. E
    Initialize the DynamoDB client in the application by hardcoding the AWS Access Key ID and Secret Access Key of an IAM user with full permissions to the table to ensure fast authentication.

Answer

Query the base table using the partition key and sort key for the first pattern, and create a Global Secondary Index (GSI) with the activity type as the partition key and timestamp as the sort key for the second pattern.
Querying the base table directly with a key condition expression is the most efficient way to retrieve sorted data within a single partition (UserID). Creating a Global Secondary Index (GSI) with ActivityType as the partition key and ActivityTimestamp as the sort key enables querying across all partitions while maintaining the required timestamp sort order.

Step-by-Step Solution

1
Analyze the first access pattern: Retrieve all activities for a specific UserID within a date range, sorted by timestamp.
Since UserID is the base table's partition key and ActivityTimestamp is the sort key, we can query the base table directly. A Query operation retrieves items with a specific partition key and can filter/sort using key conditions on the sort key.
Using Query on the base table avoids the cost of creating an additional index and is the most efficient way to access this data.
2
Analyze the second access pattern: Retrieve all activities of a specific ActivityType across all users, sorted by timestamp.
Because this query needs to span across all partitions (all UserIDs), we must change the partition key. This requires a Global Secondary Index (GSI) with ActivityType as the partition key and ActivityTimestamp as the sort key.
GSIs allow querying across all partitions of the base table using a new partition key, and the sort key provides the required ordering.

Key Concept

Selecting between base table queries, Local Secondary Indexes (LSIs), and Global Secondary Indexes (GSIs) based on access patterns.
Estimated Time:2m 0s
Rate this question