DynamoDB Data Modeling & Operations
Difficulty: hard
Overview
Amazon DynamoDB is a fully managed, serverless, key-value and document NoSQL database.
Primary Keys:
- Partition Key only: Must be unique per item.
- Composite Key (PK + Sort Key): PK+SK combination must be unique. Items with same PK are co-located.
Capacity Modes:
- Provisioned: Set RCU/WCU. 1 RCU = 1 strongly consistent read OR 2 eventually consistent reads of ≤ 4 KB. 1 WCU = 1 write of ≤ 1 KB.
- On-Demand: Pay per request. Good for unpredictable traffic.
Read Consistency:
- Eventually Consistent: May return stale data. 0.5 RCU.
- Strongly Consistent: Latest data. 1 RCU per 4 KB. Set ConsistentRead=true.
Secondary Indexes:
- GSI: Different PK and SK. Separate throughput. Eventually consistent reads only.
- LSI: Same PK, different SK. Created at table creation. Shares table throughput.
Operations:
| Operation | Description |
|---|---|
| GetItem | Single item by PK |
| Query | Items with same PK (sorted by SK) |
| Scan | Reads entire table (expensive) |
| TransactWriteItems | Up to 25 items, ACID |
| BatchGetItem | Up to 100 items across tables |
Condition Expressions: attribute_not_exists(pk) prevents overwrites.
DynamoDB Streams: Ordered log of item changes. 24-hour retention. Triggers Lambda via event source mapping.
DAX: In-memory cache. Microsecond reads. Write-through. Same API as DynamoDB. Use for read-heavy, eventually consistent workloads.
TTL: Define an epoch timestamp attribute for automatic item deletion. Free, asynchronous.
Hot Partition Strategies: Add random suffix to partition key (write sharding). Use high-cardinality keys.
Practice Linked Questions
Q1. What is the difference between a Partition Key and a Sort Key in DynamoDB?
Select one answer before revealing.
Q2. A developer runs a DynamoDB Scan on a table with 100 GB of data. What is the expected behavior and cost concern?
Select one answer before revealing.
Q3. A developer needs to write an item to DynamoDB only if an item with that partition key does not already exist. What is the correct approach?
Select one answer before revealing.
Q4. A DynamoDB table stores IoT sensor readings with sensorId as the partition key. The table is experiencing hot partitions because only 10 sensors generate 90% of writes. Which strategy best distributes the write load?
Select one answer before revealing.
Q5. Which TWO DynamoDB features are best suited for improving read performance on a read-heavy application? (Choose 2)
Select one answer before revealing.
Q6. A developer needs to update a DynamoDB item's "views" counter atomically, incrementing it by 1. What is the correct approach?
Select one answer before revealing.
Q7. A developer enables DynamoDB Streams on a table and creates a Lambda event source mapping. Which stream view type includes both the old and new item images in every stream record?
Select one answer before revealing.
Q8. A developer creates a DynamoDB GSI with the same attributes as the base table's key but in reverse (table: PK=userId, SK=orderId; GSI: PK=orderId, SK=userId). What behavior should the developer expect when querying the GSI?
Select one answer before revealing.
Q9. A developer performs a DynamoDB TransactWriteItems operation with 3 PutItem actions. One of the items fails a condition check. What happens to the other two items?
Select one answer before revealing.
Q10. A DynamoDB table has TTL enabled on the "expiresAt" attribute. An item has expiresAt set to a Unix timestamp that passed 2 hours ago. What is the state of this item?
Select one answer before revealing.