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

← All DynamoDB questions