Curious Minds

Clear answers to science and history questions

DynamoDB Partition Key: How It Works

· 6 min read · Science

A DynamoDB partition key is the single attribute value that determines which physical partition stores an item and how that item is distributed across the table. DynamoDB hashes the partition key value to assign each item to a partition, and all items sharing the same partition key value are stored together on the same partition.

Background: Why DynamoDB Uses Partition Keys

DynamoDB is a managed NoSQL database designed for predictable performance at scale. To achieve that, it spreads data across many storage nodes, called partitions, and routes requests based on the partition key. Each partition has a fixed capacity: roughly 3,000 read capacity units and 1,000 write capacity units, along with about 10 GB of data. When a table grows beyond these limits, DynamoDB splits partitions automatically. The partition key is the mechanism that makes this horizontal scaling possible, because it gives the system a deterministic way to decide where any given item lives.

Unlike a relational database, where you can query on any column, DynamoDB requires you to plan access patterns around the partition key. The key is not just an index; it is the primary routing mechanism for reads and writes. This design trade-off is central to understanding how DynamoDB behaves.

How the Partition Key Works Internally

When you write an item, DynamoDB takes the partition key value and runs it through an internal hash function. The hash output maps to a partition. Because the hash function is deterministic, the same partition key value always maps to the same partition, which is why reads and writes for that key go to the same place. The hash also spreads different key values across partitions, aiming for an even distribution.

DynamoDB supports two kinds of primary keys:

  • Partition key only (simple primary key): The partition key uniquely identifies each item. No two items can have the same partition key value.
  • Partition key and sort key (composite primary key): The partition key groups items, and the sort key orders them within that group. Multiple items can share a partition key value as long as their sort key values differ.

With a composite key, items with the same partition key are stored contiguously and sorted by the sort key. This enables efficient range queries, such as retrieving all orders for a customer within a date range, because the items are physically adjacent on the same partition.

DynamoDB also maintains indexes. A global secondary index (GSI) has its own partition key and sort key, which can differ from the base table. A local secondary index (LSI) shares the partition key of the base table but uses a different sort key. These indexes are separate data structures with their own partitioning, so the choice of partition key affects index performance too.

How to Choose a Good Partition Key

The goal is high cardinality and even access distribution. A good partition key has many distinct values, and requests are spread roughly evenly across those values. Here are practical factors to consider:

  • Cardinality: The number of distinct key values. Low-cardinality keys, such as a status field with a handful of values, concentrate traffic on a few partitions.
  • Access uniformity: Even a high-cardinality key can be problematic if one value is extremely popular. A celebrity user ID, for example, can create a hot partition.
  • Query patterns: The partition key should support your most common access patterns. If you frequently fetch items by customer, customer ID is a natural candidate.
  • Write distribution: Time-based keys like a date string can cause hot partitions because recent dates receive most writes. Consider adding a random suffix or using a composite key to spread writes.
  • Item size and volume: Large items and high request rates on the same key accelerate partition capacity consumption.

A common misconception is that a partition key must be a single scalar attribute. It is always a single attribute, but that attribute can be a string, number, or binary value. You cannot use a composite of multiple attributes as the partition key itself; that is what the sort key is for.

Common Problems: Hot Partitions and Throttling

When one partition key value receives a disproportionate share of traffic, it creates a hot partition. DynamoDB throttles requests that exceed a partition's capacity, even if the table as a whole has unused capacity. Symptoms include ProvisionedThroughputExceededException errors or, in on-demand mode, throttling during sudden spikes.

Hot partitions often arise from:

  • Low-cardinality partition keys, such as country or status.
  • Monotonically increasing keys, such as timestamps or sequential IDs, which concentrate new writes on the latest partition.
  • Popular items, such as a viral post or a heavily accessed system record.

Mitigations include adding a random suffix to the partition key (write sharding), using a composite key to distribute related items, or redesigning the key to match access patterns more evenly. Adaptive capacity can help absorb brief spikes, but it does not fix a fundamentally skewed key design.

Another issue is item collection size. With a composite key, all items sharing a partition key form an item collection, and the total size of an item collection is limited to 10 GB. This matters for local secondary indexes and for designs that pack many items under one partition key.

Partition Key vs. Sort Key

The partition key determines location; the sort key determines order within that location. You can query by partition key alone, but you cannot query by sort key alone without scanning the table. A composite key enables efficient range queries and hierarchical data modeling. For example, a table with partition key CustomerID and sort key OrderDate lets you fetch all orders for a customer sorted by date, or a specific date range, without scanning unrelated data.

Choosing between a simple and composite key depends on whether you need to group multiple items under one partition key and query them by range. If each item is independent and uniquely identified by one value, a simple key is sufficient.

Cost and Capacity Implications

Partition keys affect cost indirectly through capacity consumption. Reads and writes are metered by capacity units, and a hot partition can cause throttling even when the table has provisioned capacity available. In on-demand mode, you pay per request, and throttling can still occur during extreme spikes. A well-distributed partition key helps you use capacity efficiently and avoid over-provisioning to compensate for skew.

Storage cost is based on total data size, not partition count, but partition splits can affect performance rather than direct storage billing. Indexes add their own storage and capacity costs, and their partition keys should be chosen with the same care as the base table's.

Frequently Asked Questions

Can a partition key be changed after table creation?

No. The primary key schema, including the partition key and sort key, is fixed at table creation. To change it, you must create a new table and migrate the data.

Does the partition key have to be unique?

In a table with only a partition key, yes, each item must have a unique partition key value. In a table with a composite key, partition key values can repeat as long as the combination of partition key and sort key is unique.

What happens if two items have the same partition key and sort key?

DynamoDB overwrites the existing item with the new one. The write is an upsert, not an error, unless you use a conditional expression to prevent it.

Is the partition key the same as the primary key?

The primary key is the combination of the partition key and, optionally, the sort key. The partition key is one component of the primary key.

Can I query by partition key only?

Yes. You can use the Query operation with just the partition key value to retrieve all items with that value, up to the query limits. You cannot query by sort key alone without specifying the partition key.

How many partitions does a table have?

DynamoDB manages partitions automatically. The number depends on data size and throughput. You do not create or configure partitions directly; the service splits and merges them as needed based on the partition key distribution.

In this article
  1. Background: Why DynamoDB Uses Partition Keys
  2. How the Partition Key Works Internally
  3. How to Choose a Good Partition Key
  4. Common Problems: Hot Partitions and Throttling
  5. Partition Key vs. Sort Key
  6. Cost and Capacity Implications
  7. Frequently Asked Questions