Local Secondary Indexes (LSIs)
A local secondary index keeps the base table's partition key but sorts the item collection by a different attribute.
Introduction
A local secondary index keeps the base table's partition key but sorts the item collection by a different attribute. Use it when you need another sort order within the same partition and you need strongly consistent reads — for example, listing a customer's orders by total instead of by date.
Purpose of this lesson
You will learn when an LSI is the right choice, its creation-time restriction, and the 10 GB item-collection limit it introduces.
Understanding the topic
LSI rules:
- Same partition key as the table, different sort key.
- Must be defined when the table is created; it cannot be added or removed later.
- Up to 5 LSIs per table.
- Supports strongly consistent reads (
ConsistentRead: true). - Shares the base table's read and write capacity.
- With any LSI, each item collection (base table + LSIs for one partition key) is limited to 10 GB.
Syntax reference
Create a table with an LSI:
aws dynamodb create-table \--table-name CustomerOrders \--attribute-definitions \AttributeName=customerId,AttributeType=S \AttributeName=placedAt,AttributeType=S \AttributeName=total,AttributeType=N \--key-schema AttributeName=customerId,KeyType=HASH AttributeName=placedAt,KeyType=RANGE \--local-secondary-indexes '[{"IndexName": "ByTotal","KeySchema": [{"AttributeName": "customerId", "KeyType": "HASH"},{"AttributeName": "total", "KeyType": "RANGE"}],"Projection": {"ProjectionType": "ALL"}}]' \--billing-mode PAY_PER_REQUEST
Informative example
Largest orders for a customer, strongly consistent:
import { QueryCommand } from "@aws-sdk/lib-dynamodb";import { ddb } from "./ddb";const { Items = [] } = await ddb.send(new QueryCommand({TableName: "CustomerOrders",IndexName: "ByTotal",KeyConditionExpression: "customerId = :c AND #total >= :min",ExpressionAttributeNames: { "#total": "total" },ExpressionAttributeValues: { ":c": "c1", ":min": 50 },ScanIndexForward: false,ConsistentRead: true,}));console.log(Items.map((o) => o.total));
DynamoDB reserves several hundred words in expressions (including status, name, and date). Aliasing attribute names with ExpressionAttributeNames avoids reserved-word errors entirely.
Decision framework
- Need a different sort order inside the same partition and strong consistency → LSI.
- Need a different partition key, or might add the index later → GSI.
- Item collections could exceed 10 GB → avoid LSIs.
Best practices
- Decide on LSIs during initial design — they cannot be added later.
- Monitor item-collection size (
ReturnItemCollectionMetrics) for large customers. - Prefer GSIs unless you specifically need strong consistency on the alternate sort order.
Common mistakes
- Planning to 'add an LSI later'.
- Hitting the 10 GB item-collection limit, which makes writes to that partition key fail.
- Forgetting that LSI writes consume the base table's capacity.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1IntermediateQuestionWhy must LSIs be created with the table?+
Answer
2AdvancedQuestionWhat is the 10 GB item collection limit?+
Answer
Hands-on exercise
Hands-on: create CustomerOrders with the ByTotal LSI, insert eight orders for one customer with different totals, and query the three largest orders using Limit: 3 and ScanIndexForward: false.
Summary
LSIs add alternate sort orders within a partition with strong consistency, but must be created up front and cap item collections at 10 GB.