Quick revision
The short answer
The partition key is hashed to choose which partition stores an item, and the optional sort key orders the items that share a partition key, so Query can read ranges of them. A global secondary index has its own partition and sort key, can be added at any time, has its own capacity and is only eventually consistent; a local secondary index keeps the table's partition key with a different sort key, must be created with the table, shares the table's capacity and can be read with strong consistency. Strongly consistent reads are available for GetItem, Query and Scan on the base table and on an LSI, never on a GSI.
What to remember
In this answer, we saw that the partition key is hashed to place an item on a partition and the sort key orders the items in that partition so Query can read ranges. A GSI re-keys the data freely, can be added later, has its own capacity and is eventually consistent, while an LSI keeps the partition key, must exist from the start, shares capacity, allows strongly consistent reads and caps an item collection at 10 GB. Strong reads work on the base table and LSIs only, and projections decide how much each index costs to store and write.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The partition key is hashed to choose which partition stores an item, and the optional sort key orders the items that share a partition key, so Query can read ranges of them. A global secondary index has its own partition and sort key, can be added at any time, has its own capacity and is only eventually consistent; a local secondary index keeps the table's partition key with a different sort key, must be created with the table, shares the table's capacity and can be read with strong consistency. Strongly consistent reads are available for GetItem, Query and Scan on the base table and on an LSI, never on a GSI.
Why this question is asked
In DynamoDB the key design is the data model. A relational database lets us add an index and write any query later, but DynamoDB only answers questions that its keys and indexes were built for. Interviewers ask about partition keys, sort keys and the two index types because nearly every real DynamoDB problem, from slow queries to throttling to stale reads, traces back to a choice made here. In this answer, we will recap how DynamoDB stores items, see what the sort key adds, compare global and local secondary indexes, work out which reads can be strongly consistent, and finish with projections and what they cost.
1A Quick Recap of How DynamoDB Stores Items
A DynamoDB table holds items, which are like rows, and each item is a set of attributes. Every item must have a primary key. The primary key is either a single partition key or a pair made of a partition key and a sort key.
Behind the scenes DynamoDB splits a table into partitions, which are storage units of limited size and throughput, each kept on several servers for durability. When we write an item, DynamoDB runs the partition key through a hash function and the hash decides which partition the item lives on. This is why a lookup by key is fast whatever the table size: DynamoDB computes the hash, goes to one partition and reads the item. It is also why we cannot ask for 'all items where the partition key is greater than X': hashing destroys the order of partition keys.
2What the Sort Key Adds
A sort key turns one partition key into a small ordered collection. All items with the same partition key are stored together, sorted by the sort key. DynamoDB calls this group an item collection. With a sort key we can use Query, which takes exactly one partition key value and an optional condition on the sort key, such as equals, less than, between or begins_with.
Notice that both operations name a single partition key value. The sort key condition only narrows the range inside that collection. So the question we ask when designing keys is: which value will every query know, and what order do we want the results in?
3Global and Local Secondary Indexes Side by Side
A secondary index is a second copy of some of the table's data, arranged under a different key, so we can Query by something other than the primary key. DynamoDB keeps the copy up to date for us on every write.
A global secondary index (GSI) can use any attribute as its partition key and any attribute as its sort key. It is called global because its items can live on any partition, completely independent of the base table. We can add or delete a GSI at any time; DynamoDB backfills an index created on an existing table. In provisioned mode a GSI has its own read and write capacity. The catch is consistency: DynamoDB copies writes into a GSI asynchronously, usually within a fraction of a second, so a GSI read is always eventually consistent.
A local secondary index (LSI) keeps the base table's partition key and only changes the sort key. It is local because its entries sit on the same partition as the base items they point to. That locality explains its other properties: an LSI can be updated together with the item, so it supports strongly consistent reads; it uses the table's capacity; and it must be defined when the table is created. It also brings a limit: for a table with any LSI, one item collection, meaning all base items and LSI entries for one partition key value, may not exceed 10 GB.
| Property | Global secondary index | Local secondary index |
|---|---|---|
| Partition key | any attribute | same as the table |
| Sort key | any attribute, optional | a different attribute, required |
| When created | any time, backfilled | only with the table |
| Capacity | its own | shares the table's |
| Strongly consistent read | no | yes |
| Size limit | none per key | 10 GB per item collection |
| How many | up to 20 by default (a quota) | up to 5 |
4Which Reads Can Be Strongly Consistent?
By default every DynamoDB read is eventually consistent, which means it may read a replica that has not received the latest write yet, and it costs half a read unit per 4 KB. A strongly consistent read returns the result of every write that succeeded before it, and costs a full read unit per 4 KB. We ask for it per request.
So GetItem, BatchGetItem, Query and Scan accept ConsistentRead=true on the base table and on an LSI, and a GSI rejects it. Scan technically supports strong consistency too, but Scan reads every item in the table or index and charges for all of it, so it does not belong in a request path that users wait on. When we need read-after-write on a lookup that only a GSI can serve, we usually redesign so the lookup is a GetItem on a base table item.
5Projections and Their Cost
Each index stores a projection, which is the set of attributes copied into it. KEYS_ONLY copies the table and index keys, INCLUDE adds named attributes, and ALL copies the whole item. A Query on a GSI can only return projected attributes; it cannot fetch the rest from the table. An LSI can fetch missing attributes from the table, but each fetch costs extra reads.
Projecting ALL is convenient but not free. Every write to the base table that touches the item is also written into each index that holds it, so an item of 3 KB in a table with three ALL-projected GSIs costs roughly four times the write capacity and four times the storage. A good habit is to project only what the query on that index actually returns.
6Follow-up questions
What happens to writes if a GSI is under-provisioned?
In provisioned mode, DynamoDB must copy each write into every affected GSI. If a GSI lacks the write capacity to keep up, DynamoDB throttles writes to the base table, even when the table itself has spare capacity. So a small, forgotten index can block the whole write path. We give GSIs at least the write capacity the base writes need, enable auto scaling on them, or use on-demand mode, and we watch the throttling metrics per index.
Can two items in a GSI have the same key?
Yes. Unlike the primary key, a GSI key does not have to be unique. Many items can share the same GSI partition and sort key values, and a Query returns all of them. This is also why a GSI cannot enforce uniqueness.
Why would we choose an LSI at all if it has so many restrictions?
When we need a second sort order inside the same partition key and the read must be strongly consistent, for example reading a customer's orders by total amount right after placing one. If the item collection for a key could grow past 10 GB, or the need may appear after launch, a GSI is the safer choice.
7Conclusion
In this answer, we saw that the partition key is hashed to place an item on a partition and the sort key orders the items in that partition so Query can read ranges. A GSI re-keys the data freely, can be added later, has its own capacity and is eventually consistent, while an LSI keeps the partition key, must exist from the start, shares capacity, allows strongly consistent reads and caps an item collection at 10 GB. Strong reads work on the base table and LSIs only, and projections decide how much each index costs to store and write.
Open with
DynamoDB hashes the partition key to decide which partition holds an item, so key lookups are fast but there is no ordering across partition keys. The optional sort key orders items that share a partition key, and that is what lets Query read a range, like a customer's orders between two dates, with begins_with, between or comparison conditions.
Then, if they want more
A global secondary index can have a completely different partition and sort key, I can add it to an existing table at any time and DynamoDB backfills it, and it has its own capacity. Writes are copied to it asynchronously, so GSI reads are eventually consistent only.
A local secondary index keeps the same partition key with another sort key, has to be defined when the table is created, uses the table's capacity, and supports strongly consistent reads because it lives on the same partition. The price is a 10 GB limit per item collection.
For consistency, GetItem, Query and Scan can pass ConsistentRead true on the base table or an LSI, never on a GSI, and I would not use Scan in a hot path anyway because it reads and charges for everything.
Finally, projections decide what each index copies: projecting ALL costs storage and write capacity on every write, so I project only what the query needs. And if a GSI is under-provisioned, it throttles writes on the base table.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Cassandra documentation, Amazon DynamoDB developer guide
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain DynamoDB partition keys and sort keys, and the difference between a global...": https://scrivio-interview-library.netlify.app/databases/cassandra-and-dynamodb/b3f18ebfce/