Understanding DynamoDB, Global Secondary Index, and Local Secondary Index

Ayushman GargAyushman Garg
4 min read

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_idgame_idhighest_scoregames_played
U1G12505
U1G21802
U2G13008
U3G22206
U4G31003

Case 1: Sorting User Scores with LSI

Table's primary key:

  • user_id is the Partition Key

  • game_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_idgame_idhighest_score
U1G2180
U1G1250

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 Key

  • games_played as the Sort Key

Now, the GSI provides you with the following when you query for game_id = G1:

game_iduser_idgames_played
G1U15
G1U28

When sorted by games_played, this is:

game_iduser_idgames_played
G1U15
G1U28

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.

0
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