Difficulty: Advanced
What is a Global Secondary Index (GSI) in DynamoDB, and how does it differ from a Local Secondary Index (LSI)?
A Global Secondary Index is a separately maintained copy of selected attributes from a DynamoDB table, organized under a different partition key and optional sort key. It lets you run efficient Query operations on access patterns the base table's key cannot serve, such as finding orders by restaurant when the table is keyed by customer. GSIs are updated asynchronously, so reads from them are always eventually consistent.
DynamoDB can only Query efficiently by partition key (plus an optional sort key condition). If your table is PK = customer_id, SK = order_time, you can fetch "orders for customer 42 this week" cheaply. But "all pending orders for restaurant 7" would require a full Scan, which reads every item and costs read capacity for all of it. A GSI keyed by restaurant_id and status#order_time turns that into a fast query.
KEYS_ONLY, INCLUDE (specific attributes), or ALL. Querying an attribute not projected requires a second read from the base table.is_flagged exists.| Aspect | Global Secondary Index | Local Secondary Index |
|---|---|---|
| Partition key | Any attribute | Same as base table |
| Sort key | Any attribute | Different from base table |
| When created | Any time | Only at table creation |
| Read consistency | Eventually consistent only | Eventual or strongly consistent |
| Capacity | Separate from the table | Shared with the table |
| Size limits | None per partition | 10 GB per partition key value (item collection) |
| Default limit per table | 20 (adjustable quota) | 5 |
GSI1PK and GSI1SK serves several entity types.status with only three values will concentrate traffic.aws dynamodb create-table \
--table-name Orders \
--billing-mode PAY_PER_REQUEST \
--attribute-definitions \
AttributeName=customer_id,AttributeType=S \
AttributeName=order_time,AttributeType=S \
AttributeName=restaurant_id,AttributeType=S \
--key-schema \
AttributeName=customer_id,KeyType=HASH \
AttributeName=order_time,KeyType=RANGE \
--global-secondary-indexes '[{
"IndexName": "ByRestaurant",
"KeySchema": [
{"AttributeName": "restaurant_id", "KeyType": "HASH"},
{"AttributeName": "order_time", "KeyType": "RANGE"}
],
"Projection": {"ProjectionType": "INCLUDE",
"NonKeyAttributes": ["status", "amount_paise"]}
}]'
The GSI re-keys the same orders by restaurant and projects only the attributes the restaurant dashboard needs.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";
const db = DynamoDBDocumentClient.from(new DynamoDBClient({}));
const res = await db.send(new QueryCommand({
TableName: "Orders",
IndexName: "ByRestaurant", // query the GSI, not the table
KeyConditionExpression: "restaurant_id = :r AND order_time >= :t",
ExpressionAttributeValues: { ":r": "R-7", ":t": "2026-10-01" },
// ConsistentRead: true is NOT allowed on a GSI
}));
console.log(res.Items);
Passing IndexName routes the query to the GSI; strongly consistent reads are rejected for global indexes.
DynamoDB, Global Secondary Index, Local Secondary Index, Partition Key, Projection