Understanding DynamoDB, Global Secondary Index, and Local Secondary Index


DynamoDB: What is it?
Amazon Web Services (AWS) offers a NoSQL database service called DynamoDB.
It adheres to the ACID (Atomicity, Consistency, Isolation, and Durability) properties.
It can process millions of requests per second and is very scalable.
Important characteristics:
Ready for production from the first day
Pay-as-you-go pricing structure
Infrastructure that is automatically scalable
Smooth interaction with Amazon S3
It is renowned for being easy to use and integrate with other services
The official AWS documentation is the best source for in-depth DynamoDB knowledge
DynamoDB's Primary Key
The Primary Key for each DynamoDB table can be:
A basic Partition Key, or
A composite key: Partition Key + Sort Key
Each item must have a distinct partition key and sort key combination.
DynamoDB Secondary Indexes
To improve data retrieval, DynamoDB offers two kinds of secondary indexes:
1. The Global Secondary Index (GSI)
May differ from the primary table in terms of the Sort Key and Partition Key
Can be added or removed at any time, even once the table is operational
Up to 20 GSIs are supported by each table
Beneficial for alternative access routes and adaptable query patterns
2. The LSI, or Local Secondary Index
Must have a different Sort Key but the same Partition Key as the main table
Cannot be removed after the table has been created
Up to five LSIs are supported by each table
Helpful for sorting the same partition key according to various criteria
Why Make Use of LSIs and GSIs?
For certain query patterns, the default key structure may not be the best option
Secondary indexes facilitate more effective data querying
They enhance performance and steer clear of full-table scans
But it's important to pick the right keys and indexes early on
Example Table: Scores for Games
Assume that we are constructing a table for a gaming platform.
This is the structure of our base table:
user_id | game_id | highest_score | games_played |
U1 | G1 | 250 | 5 |
U1 | G2 | 180 | 2 |
U2 | G1 | 300 | 8 |
U3 | G2 | 220 | 6 |
U4 | G3 | 100 | 3 |
Case 1: Sorting User Scores with LSI
Table's primary key:
user_id
is the Partition Keygame_id
is the Sort Key
Issue:
All of the games that user U1 has played should be arranged according to their highest score.
After fetching, you would need to manually sort using the base table.
Answer:
Make an LSI using:
user_id
as the Partition Key (same as table)Sort Key:
highest_score
You will now receive this sorted result after retrieving the U1 scores:
user_id | game_id | highest_score |
U1 | G2 | 180 |
U1 | G1 | 250 |
Because the LSI does the sorting internally, this query is now quick and effective.
Case 2: Querying Users by Game with GSI
All users who have played game G1 should be retrieved, arranged according to the number of times they have played.
Issue:
Since game_id
is not a Partition Key in the base table, the query would need to scan every record.
Answer:
Make a GSI using:
game_id
as the Partition Keygames_played
as the Sort Key
Now, the GSI provides you with the following when you query for game_id = G1
:
game_id | user_id | games_played |
G1 | U1 | 5 |
G1 | U2 | 8 |
When sorted by games_played
, this is:
game_id | user_id | games_played |
G1 | U1 | 5 |
G1 | U2 | 8 |
A lot quicker than going through the entire table.
Important Takeaways
DynamoDB is a robust NoSQL service that has excellent performance and scalability built in
Optimized query patterns are made possible by secondary indexes (GSI and LSI)
While LSIs must be created initially, GSIs are adaptable and can be added at any time
If you want different sorting logic for the same Partition Key, use LSI
When you need to query using a completely different attribute, use GSI
Performance depends on selecting the appropriate keys and indexes according to anticipated access patterns
Last Remark
After reading the official AWS documentation for the entire day, these were my last thoughts regarding GSI, LSI, and DynamoDB. If you want to learn more, I suggest reading the official AWS documents.
Subscribe to my newsletter
Read articles from Ayushman Garg directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ayushman Garg
Ayushman Garg
π¨βπ» MERN Stack Developer | π» Python | π Open Source | π Blogs/ βοΈThreads |πMPS'21 | π¨βπ MAIT'25