Designing DynamoDB Tables, Keys and Indexes
This lesson explains how a DynamoDB table's key design determines how data is spread, which queries are efficient and how well the fully managed NoSQL database scales.
Learning outcomes
After completing this lesson, you will be able to:
- Choose a primary key that spreads data evenly.
- Distinguish global from local secondary indexes.
- Distinguish GetItem, Query and Scan.
Items and primary keys
DynamoDB stores data as items of up to 400 KB, each identified by a primary key, which is either a partition key alone or a partition key plus a sort key. DynamoDB uses the partition key to spread items across partitions, so the choice of key determines how well a table scales.
A high-cardinality partition key, such as a user ID or order ID, spreads requests evenly and avoids hot partitions, where one key receives most of the traffic and is throttled. A sort key groups related items under one partition key and allows range queries, such as all of a customer's orders between two dates. Objects larger than the item limit are stored in Amazon S3, with the item holding the object's S3 key.
The following diagram compares two choices of partition key.

A hot partition
Secondary indexes
A DynamoDB table can be queried efficiently only by its primary key, and secondary indexes add other access patterns. A global secondary index (GSI) uses any attributes as its partition and sort key, so it supports entirely new queries, such as finding orders by status across all customers. It can be added at any time and has its own capacity, but it is updated asynchronously, so its reads are eventually consistent only.
A local secondary index (LSI) keeps the table's partition key with a different sort key, giving another ordering within each partition, such as a customer's orders by total instead of by date. It must be created with the table, cannot be added later, and supports strongly consistent reads. The following table summarises the differences.
| Index | Key | When created | Consistency |
|---|---|---|---|
| Global secondary index (GSI) | Any alternative partition and sort key | Any time | Eventual only |
| Local secondary index (LSI) | Same partition key, different sort key | Only with the table | Strong or eventual |
The following diagram shows one table with an index of each kind.

Reading data
DynamoDB's read operations differ greatly in efficiency. GetItem reads one item by its full primary key. Query reads the items that share one partition key value, optionally narrowed by a condition on the sort key, and reads only those items, so it stays fast and cheap as the table grows. Scan reads every item in the table, so it becomes slow and expensive at scale; a frequent need to scan usually means a GSI should be added for that access pattern.
Reads are eventually consistent by default and cost half as much; a strongly consistent read, available on tables and LSIs, returns the latest committed value. Transactions read or write up to 100 items, across tables, as a single all-or-nothing operation. The following table summarises the operations.
| Operation or option | Behaviour |
|---|---|
GetItem | One item by primary key |
Query | Items for one partition key value, optionally filtered by sort key: efficient |
Scan | The whole table: slow and expensive at scale; add a GSI instead |
| Eventually consistent read | The default, at half the cost |
| Strongly consistent read | On request, on tables and LSIs |
| Transactions | ACID across up to 100 items and tables |
Reading DynamoDB
A key that caused throttling
An IoT table uses the device type ("sensor", "camera", "lock") as its partition key. Writes are throttled even though total capacity is far from used.
| Design | Effect |
|---|---|
| Partition key: device type | Three values: almost all writes hit one or two partitions |
| Partition key: device ID; sort key: timestamp | Writes spread across millions of partitions; each device's readings are stored in time order |
| GSI on device type | Queries by type remain possible without a hot base table |
No amount of extra capacity fixes a hot partition; the key design must spread the load.
Which index can be added to an existing table, with a different partition key?
Show answer
A global secondary index. A local secondary index uses the same partition key and must be created with the table.
Exam Tips
Remember for the exam
- To fix throttling from hot partitions, design a better partition key with high cardinality.
- To query on a non-key attribute, add a GSI.
- To store large binary data, put it in S3 and keep its key in a DynamoDB item.
- Avoid scans by designing keys and GSIs for each access pattern.
- A local secondary index must be created with the table.