Question

Difficulty: MediumData Store Operations with Amazon DynamoDB

A developer is designing a corporate training portal that tracks student progress using an Amazon DynamoDB table. The base table key schema consists of UserIDUserID as the partition key and CourseIDCourseID as the sort key. The application must support two primary query patterns: retrieving all courses completed by a specific user within a specified date range sorted by completion date, and retrieving all users who have completed a specific course. Which two strategies should the developer implement to meet these requirements with the most efficient database operations?

  1. Create a Local Secondary Index (LSI) with UserIDUserID as the partition key and CompletionDateCompletionDate as the sort key.Answer
  2. Create a Global Secondary Index (GSI) with CourseIDCourseID as the partition key and UserIDUserID as the sort key.Answer
  3. C
    Perform a Scan operation on the base table using a FilterExpression on CourseIDCourseID to identify all users who completed the course.
  4. D
    Perform a Scan operation on the base table using a FilterExpression on CompletionDateCompletionDate to filter the user's courses.
  5. E
    Initialize the DynamoDB client in the application code by hardcoding static AWS Access Keys to authenticate the query operations.

Answer

Create a Local Secondary Index (LSI) with UserID as the partition key and CompletionDate as the sort key, and create a Global Secondary Index (GSI) with CourseID as the partition key and UserID as the sort key.
To support retrieving all courses completed by a specific user within a date range, the developer should create a Local Secondary Index (LSI) with the user ID as the partition key and the completion date as the sort key. This enables key-based Query operations with sorting on the date. To find all users who completed a specific course, a Global Secondary Index (GSI) with the course ID as the partition key is required because queries must look up records using an attribute other than the base table's partition key.

Step-by-Step Solution

1
Analyze the first query pattern: retrieving courses completed by a specific user within a date range.
Since the partition key of the query is the same as the base table (UserIDUserID), but a different sort key (CompletionDateCompletionDate) is required to filter and sort the results, a Local Secondary Index (LSI) is the optimal configuration.
An LSI allows defining a new sort key for queries on the same partition key as the base table.
2
Analyze the second query pattern: retrieving all users who completed a specific course.
Because the query must filter by CourseIDCourseID across all users (which is not the partition key of the base table), a Global Secondary Index (GSI) with CourseIDCourseID as the partition key is needed.
GSIs allow queries across all partitions by defining a different partition key.
3
Verify and eliminate inefficient and insecure options.
Scan operations are ruled out due to high resource usage and latency, and hardcoded credentials in the SDK client creation are eliminated as a severe security anti-pattern.
Query operations on indexes should always be preferred over Scan operations for specific lookups, and authentication must use IAM roles via the default credential provider chain.

Key Concept

DynamoDB Secondary Indexes (LSI vs GSI) and Query Optimization
Rate this question