DynamoDB GSI: How Global Secondary Indexes Work in Single-Table Design
A table’s primary key answers one question well: “give me the items under this partition key, in sort key order.” Every other question needs either a scan, a reshaped key, or a global secondary index.
A GSI is a second copy of some of your data, keyed differently, that DynamoDB keeps in sync for you. That’s the useful part. The cost is that every write to the table may become a write to the index too, and the copy is always slightly behind.
rasika.life started with 6 GSIs on the Event entity. That was one of the pain points that motivated SingleTable.
What a GSI is
You define a GSI with its own partition key and, optionally, its own sort key. Those keys can be any top-level String, Number, or Binary attribute on your items. When you write an item to the table, DynamoDB copies it into the index, organised by the index keys instead of the table keys. You query the index the same way you query the table.
// Table key: pk = ORG#org_acme, sk = USER#u_alice
// GSI1 key: gsi1pk = EMAIL#alice@acme.com, gsi1sk = USER
const { data } = await UserEntity.query.byEmail({ email: 'alice@acme.com' }).go()
Without the GSI, “find a user by email” is a scan. With it, the lookup is a single Query against a partition that holds one item.
The rules
These come from the AWS docs.
| Rule | What it means |
|---|---|
| 20 GSIs per table (default quota) | Plenty, if you overload. Running out means one GSI per access pattern, which is the expensive way to design. |
| Eventually consistent reads only | A write shows up in the index within a fraction of a second, usually. Not always. |
| Query and Scan only | No GetItem or BatchGetItem on an index. |
| Keys don’t have to be unique | Ten items can share the same GSI key. A GSI can’t enforce uniqueness. |
| Items without the key attributes aren’t indexed | This is what makes sparse indexes work. |
| Key attributes must be String, Number, or Binary | Top-level only. No maps, lists, sets, or booleans. Writing the wrong type to an indexed attribute fails the whole table write. |
Projections: KEYS_ONLY, INCLUDE, or ALL | A GSI query can’t fetch attributes that weren’t projected. You need a second read against the table. |
100 INCLUDE attributes per table | Summed across all secondary indexes. |
| Add or delete any time | DynamoDB backfills a new GSI from existing items. |
GSI vs LSI
A local secondary index (LSI) shares the table’s partition key and gives you a different sort key. It sounds like a cheaper GSI. In practice I don’t use them.
| GSI | LSI | |
|---|---|---|
| Partition key | Any attribute | Same as the table |
| When you can create it | Any time | Only at table creation |
| Consistency | Eventually consistent | Strongly consistent available |
| Limit | 20 per table (default) | 5 per table |
| Side effect | None on the table | Caps each item collection at 10 GB |
The 10 GB cap is the reason. Once a table has an LSI, every partition key value is limited to 10 GB across the table and its LSIs. A GSI has no such limit, and you can add one later when a new access pattern shows up. Create an LSI only when you need a strongly consistent alternate sort order and you know the partition will stay small.
What a GSI costs
Reads from a GSI cost the same as reads from a table. Writes are where the bill grows. For each GSI, a table write costs:
- One index write when the item gains the index key attributes (new item, or an update that sets them).
- Two index writes when an index key value changes. DynamoDB deletes the old index entry and puts the new one.
- One index write when the update removes the index key attribute.
- One index write when only projected, non-key attributes change.
- Nothing when the change touches neither key nor projected attributes, or the item isn’t in the index before or after.
So an entity with three GSIs, all projecting ALL, turns one 1 KB update into up to seven writes. That’s the math behind too many GSIs.
Storage costs extra too. Each index entry is the table key, the index key, the projected attributes, and 100 bytes of overhead.
On provisioned tables there’s one more trap. If a GSI runs out of write capacity, DynamoDB throttles writes to the table. Give each GSI at least the table’s write capacity, or use on-demand.
GSIs in a single-table design
In a single-table design you don’t name GSIs after what they do. You name them GSI1, GSI2, GSI3, with key attributes gsi1pk, gsi1sk, and so on. Each entity type decides what it writes into those attributes. Several entity types share one index, each using it for a different access pattern. That’s GSI overloading.
Here is a small project tracker in one table. Three entity types, one GSI.
| pk | sk | entity | gsi1pk | gsi1sk |
|---|---|---|---|---|
ORG#org_acme | USER#u_alice | user | EMAIL#alice@acme.com | USER |
ORG#org_acme | USER#u_bob | user | EMAIL#bob@acme.com | USER |
ORG#org_acme | PROJECT#01JA2B7Q | project | OWNER#u_alice | PROJECT#01JA2B7Q |
ORG#org_acme | PROJECT#01JA3C9R | project | OWNER#u_bob | PROJECT#01JA3C9R |
ORG#org_acme | TASK#01JA4D2S | task | u_alice#TASKS | DUE#2026-10-20 |
ORG#org_acme | TASK#01JA5E8T | task | DUE#2026-10-22 |
The table key answers “everything in this org.” GSI1 answers three more questions:
| Access pattern | GSI1 query |
|---|---|
| Find a user by email (login) | gsi1pk = "EMAIL#alice@acme.com" |
| List projects a user owns | gsi1pk = "OWNER#u_alice" |
| List a user’s tasks by due date | gsi1pk = "u_alice#TASKS", sorted by gsi1sk |
The last task has no assignee, so it has no gsi1pk and never enters GSI1. The index stays small, and unassigned tasks cost no index writes.
Overloading works because the prefixes never collide. EMAIL#, OWNER#, and #TASKS keys can share an index without one query returning another entity’s items. That’s three access patterns for the write cost of one GSI per entity, instead of three GSIs that every entity pays into.
Before you add a GSI at all, check whether a reshaped sort key on the table answers the question for free. GSI vs sort key walks through that decision.
Defining a GSI in ElectroDB
In ElectroDB a GSI is an index entry with an index name that matches the physical index in your infrastructure. Here’s the task entity from the table above, on ElectroDB 2.15:
import { Entity } from 'electrodb'
import { ulid } from 'ulid'
export const TaskEntity = new Entity(
{
model: { entity: 'task', version: '1', service: 'app' },
attributes: {
orgId: { type: 'string', required: true },
taskId: { type: 'string', required: true, readOnly: true, default: () => ulid() },
title: { type: 'string', required: true },
assigneeId: { type: 'string' },
dueAt: { type: 'string', required: true }, // ISO date, sorts as a string
},
indexes: {
byOrg: {
pk: { field: 'pk', composite: ['orgId'], template: 'ORG#${orgId}', casing: 'none' },
sk: { field: 'sk', composite: ['taskId'], template: 'TASK#${taskId}', casing: 'none' },
},
byAssignee: {
index: 'GSI1',
pk: { field: 'gsi1pk', composite: ['assigneeId'], template: '${assigneeId}#TASKS', casing: 'none' },
sk: { field: 'gsi1sk', composite: ['dueAt'], template: 'DUE#${dueAt}', casing: 'none' },
},
},
},
{ client, table },
)
const { data } = await TaskEntity.query.byAssignee({ assigneeId: 'u_alice' }).go()
Two details in that definition are deliberate.
casing: 'none'. ElectroDB lowercases keys by default, templates included. Without it, ORG#${orgId} is written as org#org_acme. That’s fine if you never look at the table, but it won’t match sample data, scripts, or anyone’s mental model.
The template starts with ${assigneeId}. This is the sparse-index trap. I checked it against ElectroDB 2.15.1 by printing .params(): when assigneeId is missing and the template is ASSIGNEE#${assigneeId}, ElectroDB still writes gsi1pk = "ASSIGNEE#". Every unassigned task lands in one ASSIGNEE# partition in the index. You pay index writes for items you meant to exclude, and you build a hot partition as a bonus. When the template begins with the missing attribute, ElectroDB leaves gsi1pk off the item and the index stays sparse.
Whatever library you use, run .params() (or log the request) for an item that should be excluded, and look at what gets written. It takes a minute and it’s the only proof that counts.
Adding a GSI after launch
You can create a GSI on a live table. DynamoDB builds it in the background and indexes every item that already has the key attributes.
With synthetic keys there’s a catch: existing items don’t have gsi2pk yet, because nothing wrote it. The new index starts empty. You have to backfill: read each affected item and write the new key attributes. On a large table that’s a migration with its own cost and risk. Adding an access pattern after launch covers how to run one.
GSIs now also support multi-attribute keys: up to four attributes for the partition key and four for the sort key, with DynamoDB doing the composition. That lets a new index use attributes your items already have, with no backfill. ElectroDB 2.x, which is what I run in production, still builds synthetic string keys, so the examples here use those.
Common mistakes
Reading the index right after writing. Create a user, then immediately look them up by email through GSI1, and once in a while you get nothing. For read-your-own-write, use the table key.
Using a GSI to enforce uniqueness. It can’t. Two users with the same email both land in the index. Write a separate item with pk = "EMAIL#alice@acme.com" and a conditional put, in the same transaction as the user.
Low-cardinality GSI partition keys. gsi1pk = "STATUS#open" puts every open item in one index partition. Reads and writes to that partition hit per-partition throughput limits. Add the tenant or a shard to the key. See hot partitions and write sharding.
Projecting ALL by default. It’s the easiest choice and the most expensive one. If the query only feeds a list view, project the attributes the list shows.
One GSI per access pattern. This is how you reach 20. Overload first, use sparse indexes second, and add a new GSI only when neither works.
What I’d recommend
Start every single-table schema with one overloaded GSI. Add a second only when you find an access pattern whose key shape conflicts with what’s already in GSI1. Before each new one, ask whether a sort key reshape or a sparse attribute on an existing index would do. Most teams skip that question, and that’s how an entity ends up with six GSIs.
GSIs are where single-table schemas get expensive, slowly, one access pattern at a time. ElectroDB GSI overloading shows the full overloading code, and sparse indexes covers the patterns that keep an index small. I’m building singletable.dev to show what each GSI costs before you deploy it.