DynamoDB GSI vs LSI Interview Questions
Master DynamoDB Secondary Indexes with interview-focused questions covering Global Secondary Index (GSI), Local Secondary Index (LSI), sparse indexes, projections, query optimization, performance, costs, and production best practices.
Introduction
One of the biggest limitations of DynamoDB is that you cannot efficiently query by arbitrary attributes.
Unlike relational databases where indexes can be added on almost any column, DynamoDB requires you to carefully design indexes around your application's access patterns.
DynamoDB provides two types of secondary indexes:
- Global Secondary Index (GSI)
- Local Secondary Index (LSI)
Choosing the correct index type significantly impacts:
- Query Performance
- Cost
- Scalability
- Storage
- Throughput
This chapter covers the most frequently asked DynamoDB indexing interview questions.
Secondary Index Architecture
flowchart LR
Application --> PrimaryTable
PrimaryTable --> GSI
PrimaryTable --> LSI
GSI --> Query
LSI --> Query
1. What is a Secondary Index?
Answer
A Secondary Index provides an alternative way to query data without scanning the entire table.
Benefits
- Faster Queries
- Multiple Access Patterns
- Better Performance
- Lower Cost than Scan
2. Why are Secondary Indexes required?
Suppose a table uses
Partition Key
CustomerId
Now the application needs
Find Customer By Email
Without an index
↓
Full Table Scan
With an index
↓
Efficient Query
3. What are the two types of Secondary Indexes?
DynamoDB supports
- Global Secondary Index (GSI)
- Local Secondary Index (LSI)
4. What is a Global Secondary Index (GSI)?
A GSI is an index with its own Partition Key and optional Sort Key.
It enables completely different query patterns from the base table.
GSI Architecture
flowchart LR
PrimaryTable --> GSI
GSI --> DifferentPartitionKey --> Query
5. What is a Local Secondary Index (LSI)?
An LSI uses
- Same Partition Key
- Different Sort Key
It allows additional sorting within the same partition.
LSI Architecture
flowchart LR
PrimaryTable --> SamePartitionKey --> DifferentSortKey --> Query
6. Difference between GSI and LSI?
| GSI | LSI |
|---|---|
| Different Partition Key | Same Partition Key |
| Optional Sort Key | Different Sort Key |
| Created Anytime | Must be Created with Table |
| Separate Throughput | Shares Table Throughput |
| Eventually Consistent | Strong or Eventual Reads |
7. When should GSI be used?
Use GSI when
- New Query Pattern
- Different Partition Key
- Search by Email
- Search by Status
- Search by Category
Example
Table
PK = CustomerId
Need
Find by Email
Create
GSI
PK = Email
8. When should LSI be used?
Use LSI when
Partition Key remains the same
but sorting changes.
Example
PK = CustomerId
Sort by
OrderDate
or
OrderAmount
9. Can GSI have a different Partition Key?
Yes.
Example
Primary Table
CustomerId
GSI
Email
10. Can LSI have a different Partition Key?
No.
LSI always shares the same Partition Key.
11. Can GSI have a different Sort Key?
Yes.
Example
PK
Email
SK
CreatedDate
12. Can LSI have a different Sort Key?
Yes.
Changing the Sort Key is the main purpose of LSI.
13. Can GSI be created after table creation?
Yes.
GSI can be
- Added
- Modified
- Deleted
without recreating the table.
14. Can LSI be created later?
No.
LSI must be created when the table is created.
15. Which index has separate throughput?
GSI
has its own
- RCU
- WCU
16. Which index shares throughput?
LSI shares throughput with the base table.
17. Which index supports Strongly Consistent Reads?
LSI
supports
- Strong Consistency
- Eventual Consistency
18. Which index only supports Eventually Consistent Reads?
Global Secondary Index
19. What is an Index Projection?
Projection determines
which attributes are copied into an index.
Types
- KEYS_ONLY
- INCLUDE
- ALL
Projection Types
flowchart LR
Table --> Projection
Projection --> KEYS_ONLY
Projection --> INCLUDE
Projection --> ALL
20. Explain KEYS_ONLY Projection.
Stores only
- Primary Keys
- Index Keys
Lowest storage cost.
21. Explain INCLUDE Projection.
Stores
Keys
Selected Attributes
Balances cost and performance.
22. Explain ALL Projection.
Copies every attribute.
Advantages
- Fast Queries
Disadvantages
- Higher Storage Cost
23. What is a Sparse Index?
A Sparse Index contains only items that include the indexed attribute.
Example
Only active orders have
Status
Only those items appear in the index.
Sparse Index Example
Orders
↓
Status Exists
↓
Indexed
↓
Query
24. Does every item appear in a GSI?
No.
Only items containing the indexed attribute.
25. How does GSI improve performance?
Instead of
Scan
↓
Entire Table
Use
Query
↓
Index
26. Does GSI increase write cost?
Yes.
Every write updates
- Base Table
- Index
Multiple GSIs increase write cost.
27. Does LSI increase storage?
Yes.
Additional index storage is required.
28. Banking Example
Primary Table
PK
AccountId
SK
TransactionTime
Need
Find Transactions
By Status
Create
GSI
PK
Status
SK
TransactionTime
29. E-Commerce Example
Primary
CustomerId
OrderDate
Need
Orders By Product
Create
GSI
ProductId
OrderDate
30. HR Example
Primary
EmployeeId
Need
Employees By Department
Create
GSI
Department
EmployeeName
31. Time-Series Example
Primary
DeviceId
Timestamp
Need
Events By Severity
Create
GSI
Severity
Timestamp
32. When should Scan be replaced with GSI?
Whenever the application repeatedly searches by a non-key attribute.
Example
Instead of
Scan
WHERE Status='OPEN'
Use
GSI
PK
Status
33. Common mistakes
- Too many GSIs
- Wrong Partition Key
- Using Scan instead of Query
- Large ALL projections
- Creating unnecessary indexes
- Ignoring write costs
Enterprise Best Practices
- Design indexes around access patterns.
- Prefer Query over Scan.
- Keep GSI count minimal.
- Choose projection type carefully.
- Use KEYS_ONLY whenever possible.
- Use INCLUDE for frequently accessed attributes.
- Monitor GSI throttling.
- Review index usage regularly.
- Avoid indexing low-value attributes.
- Consider write amplification before adding indexes.
Quick Revision
| Feature | GSI | LSI |
|---|---|---|
| Partition Key | Different | Same |
| Sort Key | Optional | Different |
| Create Later | Yes | No |
| Delete Later | Yes | No |
| Strong Reads | No | Yes |
| Separate Throughput | Yes | No |
| Storage | Additional | Additional |
| Best For | New Access Patterns | Alternate Sorting |
Interview Tips
Interviewers commonly ask
- Difference between GSI and LSI.
- Why use GSI instead of Scan?
- Can GSI be added later?
- Why can't LSI be added later?
- Explain Sparse Index.
- Explain Projection Types.
- Difference between KEYS_ONLY and ALL.
- Which index supports Strongly Consistent Reads?
- Does GSI increase write cost?
- Give a banking use case for GSI.
Always explain the trade-off between query flexibility, write cost, storage cost, and consistency when discussing secondary indexes.
Summary
Global Secondary Indexes (GSIs) and Local Secondary Indexes (LSIs) enable DynamoDB to support multiple access patterns without performing expensive table scans. GSIs provide new partition keys and independent scalability, while LSIs reuse the existing partition key with alternative sorting.
Understanding secondary indexes, projection types, sparse indexes, throughput implications, and consistency behavior is essential for designing scalable DynamoDB applications and succeeding in AWS, backend engineering, cloud architecture, and solution architect interviews.