Question

Difficulty: MediumData Store Operations with Amazon DynamoDB

A developer is designing a smart utility monitoring application that stores hourly meter readings in an Amazon DynamoDB table. The base table uses `DeviceID` as the partition key and `ReadingTimestamp` as the sort key. The application must support two new requirements:

1. Retrieve all readings across all devices for a specific day, sorted by energy consumption in descending order.
2. Retrieve all error events for a specific `DeviceID`, sorted by the event's timestamp.

Which two configurations should the developer implement to meet these requirements with optimal query performance and security? (Select TWO.)

  1. Create a Global Secondary Index (GSI) with `ReadingDate` as the partition key and `EnergyConsumption` as the sort key.Answer
  2. Create a Local Secondary Index (LSI) with `DeviceID` as the partition key and `ErrorTimestamp` as the sort key.Answer
  3. C
    Use a Scan operation with a FilterExpression on the base table to retrieve all readings for a specific day, then sort the results in the application.
  4. D
    Perform a Scan operation on the base table to locate error events for a specific device based on the ErrorTimestamp attribute.
  5. E
    Hardcode AWS access keys directly in the AWS SDK client initialization code to authenticate requests for scanning the base table.

Answer

Create a Global Secondary Index (GSI) with ReadingDate as the partition key and EnergyConsumption as the sort key, and create a Local Secondary Index (LSI) with DeviceID as the partition key and ErrorTimestamp as the sort key.
To retrieve readings across all devices for a specific day sorted by energy consumption, a Global Secondary Index (GSI) is required because the query spans multiple base table partitions. The GSI uses the daily date as the partition key and the energy consumption as the sort key. To retrieve error events for a specific device sorted by the event timestamp, a Local Secondary Index (LSI) is appropriate because it shares the same partition key as the base table (DeviceID) but uses a different sort key (ErrorTimestamp), allowing fast and efficient queries within a single device partition.

Step-by-Step Solution

1
Analyze the first requirement: Querying readings across all devices for a specific day, sorted by energy consumption.
Identify that because the query spans multiple devices (multiple partition keys), a Global Secondary Index (GSI) must be defined with ReadingDate as the partition key and EnergyConsumption as the sort key.
GSIs allow queries to cross partition boundaries of the base table, and setting EnergyConsumption as the sort key allows DynamoDB to return the results pre-sorted.
2
Analyze the second requirement: Querying error events for a specific DeviceID, sorted by the event's timestamp.
Identify that since the query is restricted to a single device (the same partition key as the base table), a Local Secondary Index (LSI) can be defined with DeviceID as the partition key and ErrorTimestamp as the sort key.
LSIs must use the same partition key as the base table but allow a different sort key to enable alternative sorting on that partition's data.
3
Evaluate and discard inefficient operations (Scan) and insecure authentication practices.
Discard options suggesting Scan operations or hardcoded credentials.
Scan operations read all items in the database and are extremely inefficient for lookup queries, while hardcoded credentials violate basic security practices.

Key Concept

Choosing between Global Secondary Indexes (GSI) and Local Secondary Indexes (LSI) for query optimization in DynamoDB, and avoiding full table scans.
Rate this question