Global Secondary Indexes (GSIs)
A global secondary index is a second copy of selected table data, organised under a different partition key and sort key.
Introduction
A global secondary index is a second copy of selected table data, organised under a different partition key and sort key. DynamoDB keeps it updated asynchronously whenever base-table items change. GSIs are how you serve access patterns that the base table's keys cannot — such as “list orders by status” for the warehouse in ShopTable.
Purpose of this lesson
You will create a GSI, choose a projection, query it, and understand its consistency and capacity behaviour.
Understanding the topic
How GSIs behave:
- Any attributes can be the GSI's partition and sort key; key values do not have to be unique in the index.
- Items without the GSI key attributes are not written to the index (this enables sparse indexes).
- Reads from a GSI are eventually consistent only.
- Projections:
KEYS_ONLY,INCLUDE(selected attributes), orALL. Smaller projections cost less storage and write capacity. - GSIs can be added or deleted at any time; tables support up to 20 GSIs by default.
- On provisioned tables each GSI has its own capacity; if a GSI is throttled, writes to the base table can be throttled too.
Syntax reference
Add GSI1 for orders by status (on-demand table, so no throughput settings are needed):
aws dynamodb update-table \--table-name ShopTable \--attribute-definitions AttributeName=GSI1PK,AttributeType=S AttributeName=GSI1SK,AttributeType=S \--global-secondary-index-updates '[{"Create": {"IndexName": "GSI1","KeySchema": [{"AttributeName": "GSI1PK", "KeyType": "HASH"},{"AttributeName": "GSI1SK", "KeyType": "RANGE"}],"Projection": {"ProjectionType": "INCLUDE", "NonKeyAttributes": ["customerId", "total"]}}}]'
Informative example
Write orders with GSI attributes, then query by status:
import { PutCommand, QueryCommand } from "@aws-sdk/lib-dynamodb";import { ddb, TABLE } from "./ddb";async function putOrder(id: string, status: string, placedAt: string, total: number) {await ddb.send(new PutCommand({TableName: TABLE,Item: {PK: `ORDER#${id}`, SK: "HEADER", entityType: "Order",customerId: "c1", status, total,GSI1PK: `STATUS#${status}`, GSI1SK: `${placedAt}#${id}`,},}));}await putOrder("o1", "PLACED", "2026-11-19T09:00:00Z", 59);await putOrder("o2", "SHIPPED", "2026-11-19T09:30:00Z", 20);await putOrder("o3", "PLACED", "2026-11-19T10:00:00Z", 75);const { Items = [] } = await ddb.send(new QueryCommand({TableName: TABLE,IndexName: "GSI1",KeyConditionExpression: "GSI1PK = :s",ExpressionAttributeValues: { ":s": "STATUS#PLACED" },}));console.log(Items.map((i) => `${i.PK} ${i.total}`));
Output
[ 'ORDER#o1 59', 'ORDER#o3 75' ]
Because GSI updates are asynchronous, a Query immediately after a write can briefly miss the newest item. Design flows that need read-after-write to read from the base table.
Scalability analysis
STATUS#PLACED is a low-cardinality GSI partition key: during a sale every new order writes to it. For high volume, shard the GSI key (STATUS#PLACED#0..9) or bucket it by time (STATUS#PLACED#2026-11-19).
Best practices
- Use generic GSI key names (
GSI1PK,GSI1SK) so one index can serve several entity types. - Project only the attributes the access pattern displays.
- Monitor GSI throttling separately from the base table.
Common mistakes
- Expecting strongly consistent reads from a GSI.
- Low-cardinality GSI partition keys that create hot index partitions.
- Projecting ALL attributes on large items 'just in case', doubling storage and write cost.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1IntermediateQuestionWhat is the difference between a GSI and an LSI?+
Answer
2AdvancedQuestionCan a throttled GSI affect the base table?+
Answer
Hands-on exercise
Hands-on: add GSI2 keyed on GSI2PK = EMAIL#<email> to the customer profile item and implement findCustomerByEmail. Then explain why this GSI alone does not guarantee email uniqueness (Module 5 fixes that with a transaction).
Summary
GSIs re-key your data to serve extra access patterns. They are eventually consistent, can be added anytime, and need the same partition-key care as the base table.