DynamoDB interview question · Most asked
Global secondary index or local secondary index: what is the difference?
Summary
A GSI has its own partition key and optional sort key, can be added any time, has its own capacity, and is eventually consistent. An LSI keeps the table's partition key with a different sort key, must be created with the table, shares the table's capacity, supports strongly consistent reads, and limits each item collection to 10 GB.
Longer Answer
GSIs are the default choice: they serve new access patterns on different keys (orders by status, users by email). Writes to the table are propagated asynchronously, and each GSI write consumes write capacity, so many GSIs multiply write cost. If a GSI is under-provisioned it can throttle writes to the base table.
LSIs suit "same partition, different order" patterns that need strong consistency, but the 10 GB item-collection limit and the creation-time-only rule make them rare in new designs. Tables allow up to 20 GSIs by default and 5 LSIs.
Key Points
- GSI: any keys, add any time, eventually consistent.
- LSI: same PK, created with the table, strong reads, 10 GB collections.
- Each index costs writes.
- Prefer GSIs.
More most-asked DynamoDB questions
- What are DynamoDB Streams, and how are they used?
- How do conditional writes work, and how do you implement optimistic locking in DynamoDB?
- 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.
- Query or Scan: what is the difference, and why avoid Scan?
- DynamoDB or a relational database such as Aurora PostgreSQL: how do you decide?