DynamoDB interview question · Most asked
Design the DynamoDB table for an e-commerce order system. What are your access patterns and keys?
Summary
List the patterns first, for example: get an order by ID; list a customer's orders newest first; get an order with its line items; list orders by status for the warehouse; look up a customer by email. Then map each to a key or index.
Longer Answer
One reasonable design:
| Pattern | Key |
|---|---|
| Customer profile | PK = CUSTOMER#<id>, SK = PROFILE |
| Customer's orders, newest first | PK = CUSTOMER#<id>, SK = ORDER#<iso-date>#<orderId> (Query, descending) |
| Order with items | PK = ORDER#<orderId>, SK = META and SK = ITEM#<n> |
| Orders by status | GSI1: GSI1PK = STATUS#<status>#<yyyy-mm-dd>, GSI1SK = <createdAt> (date bucket avoids a hot partition) |
| Customer by email | Uniqueness item PK = EMAIL#<email> written transactionally |
Write the order summary under the customer and the full order under its own key in one transaction, update status with a conditional write (state machine), and use a stream to notify fulfilment. Mention trade-offs: duplicated order summaries, status-bucket sharding, and where reporting will happen (export to S3).
Key Points
- Start with access patterns.
- Map each to a key or GSI.
- Avoid hot status partitions.
- Transactions, conditions and streams for consistency.
Explanation
What interviewers look for
A design driven by explicit access patterns, with hot-partition and consistency trade-offs called out.
More most-asked DynamoDB questions
- What are DynamoDB Streams, and how are they used?
- Query or Scan: what is the difference, and why avoid Scan?
- What is DynamoDB, and what workloads is it a good fit for?
- What is a hot partition, and how do you avoid one?
- How do you approach data modelling in DynamoDB, and how is it different from relational design?
- Explain partition keys, sort keys and the two kinds of primary key.
- Global secondary index or local secondary index: what is the difference?
- How do conditional writes work, and how do you implement optimistic locking in DynamoDB?