Skip to main content
PreparationZone Logo
PrepZone

DynamoDB GSI and LSI

Global and local secondary indexes — alternate access paths without full table scans.

Pass checkpoint below

Read these first

Why this matters

  • VaultCommerce needs orders by user (primary key) and orders by status for ops dashboards (GSI) — without scanning the table nightly.
  • GSIs replicate data asynchronously — expect eventual consistency and plan for write amplification on hot items.
  • LSIs must be defined at create time and share the 10 GB per-partition limit with the base table — rarely added later.
Partition key: USER#42Routes to shard
Items in partition
ORDER#2026-001Sort key
ORDER#2026-002Sort key
PROFILESort key
Partition key routes to a shard. Sort key orders items within that partition.

Global Secondary Index

A GSI has its own partition and sort key drawn from table attributes:

Java
aws dynamodb update-table \
  --table-name VaultCommerceOrders \
  --attribute-definitions \
    AttributeName=GSI1PK,AttributeType=S \
    AttributeName=GSI1SK,AttributeType=S \
  --global-secondary-index-updates '[{
    "Create": {
      "IndexName": "GSI1-StatusDate",
      "KeySchema": [
        { "AttributeName": "GSI1PK", "KeyType": "HASH" },
        { "AttributeName": "GSI1SK", "KeyType": "RANGE" }
      ],
      "Projection": { "ProjectionType": "INCLUDE", "NonKeyAttributes": ["totalAmount","customerName"] },
      "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }
    }
  }]'

VaultCommerce writes GSI1PK=STATUS#SHIPPED, GSI1SK=2026-04-01T10:00:00Z on each order update so ops queries pending shipments by status.

GSI vs LSI

  • GSI — new partition key; sparse indexes OK; add after creation; eventually consistent reads only.
  • LSI — same partition key, alternate sort key; must exist at table create; supports strong reads on same partition.
  • Projection — ALL, KEYS_ONLY, or INCLUDE specific attributes — trade storage vs query efficiency.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 2 sections

  • Querying a GSI
  • Local Secondary Index example

Test yourself

Answer these before moving on — recall is what makes it stick.