Amazon DynamoDB (50 Qs)•Part 2: Intermediate Data Modeling & API Operations (Mid-Level)
What is the difference between Global Secondary Indexes (GSI) and Local Secondary Indexes (LSI)?
junior LevelMost AskedCode ExampleHTTP & REST APIs
In Simple Words:
- LSI (Local Secondary Index):
- Must share the exact same Partition Key as the base table, but provides an alternative Sort Key.
- Must be declared at table creation time; cannot be added or deleted later.
- Enforces a 10 GB partition size limit for the collection. Supports strong consistency.
- GSI (Global Secondary Index):
- Can define an entirely different Partition Key and Sort Key.
- Can be added, modified, or removed at any time on a live production table.
- Scales throughput independently; strictly eventually consistent.
json
// GSI Example in CloudFormation/CDK:
{
"IndexName": "EmailLookupIndex",
"KeySchema": [
{ "AttributeName": "email", "KeyType": "HASH" },
{ "AttributeName": "createdAt", "KeyType": "RANGE" }
],
"Projection": { "ProjectionType": "INCLUDE", "NonKeyAttributes": ["userId", "status"] }
}