Lesson 1 of 6 Amazon DynamoDB

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.

How the partition key decides scaling. With a high-cardinality key such as a user ID, requests spread evenly across partitions. With a low-cardinality key such as a status value, most requests land on one partition, which becomes hot and is throttled even though the table has capacity elsewhere.
The traffic shares are illustrative. A key with many distinct values, each requested evenly, avoids hot partitions.
Troubleshooting scenario

A hot partition

Exam skills 3.3.2 · 4.3.3

Resolve problems caused by DynamoDB key design.

Log in to try this activity Interactive activities are free with an account.

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.

IndexKeyWhen createdConsistency
Global secondary index (GSI)Any alternative partition and sort keyAny timeEventual only
Local secondary index (LSI)Same partition key, different sort keyOnly with the tableStrong or eventual

The following diagram shows one table with an index of each kind.

Secondary indexes on an Orders table keyed by customerId and orderDate. A local secondary index keeps customerId as the partition key with a different sort key, such as total, must be created with the table and supports strongly consistent reads. A global secondary index uses any key, such as status and orderDate, can be added at any time and is updated asynchronously, so its reads are eventually consistent.
The attribute names are illustrative. A new access pattern on an existing table needs a GSI, because an LSI cannot be added later.

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 optionBehaviour
GetItemOne item by primary key
QueryItems for one partition key value, optionally filtered by sort key: efficient
ScanThe whole table: slow and expensive at scale; add a GSI instead
Eventually consistent readThe default, at half the cost
Strongly consistent readOn request, on tables and LSIs
TransactionsACID across up to 100 items and tables
Match the pairs

Reading DynamoDB

Exam skills 3.3.2

Match each requirement with the DynamoDB read operation or option.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

DesignEffect
Partition key: device typeThree values: almost all writes hit one or two partitions
Partition key: device ID; sort key: timestampWrites spread across millions of partitions; each device's readings are stored in time order
GSI on device typeQueries 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.

Check yourself

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.