Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 37 ยท Data Model and Keys

Choosing Good Partition Keys

Learn how to pick a DynamoDB partition key: high cardinality, even access, composite values, write sharding for hot keys, and a checklist of keys to avoid.

A good partition key does two things: it has many distinct values, and requests spread evenly across them. If either fails, a few partitions do all the work and you hit throttling long before the table is "full". The principle is simple, but real access patterns push back, so this lesson gives you a checklist, some patterns and a way to measure a key before you commit to it.

Two properties of a good key

  1. High cardinality. The number of different partition key values should be large compared with the number of partitions. User ids, order ids, device ids and session ids qualify. A status flag, a country code or a boolean does not.
  2. Uniform access. Even with many values, traffic must not be dominated by a few of them. One user in ten million is fine; one user producing half of all requests is not.

Good keys are also known at request time. A GetItem needs the key, so if you only know the email address, then email (or a lookup item keyed by email) must be part of the design.

Keys that usually fail

Key Why it goes wrong
Status (active, closed) Two or three values, almost all traffic on one
Date (2026-03-14) Today's writes all hit one partition
Country A few countries carry most users
TenantId in a multi-tenant table One huge tenant becomes a hot key
A monotonically increasing number Fine for hashing, but the items also have to be read somehow, and sequences don't help lookups
Type (order, invoice) A handful of values by definition

Composite and prefixed keys

Combining values makes a key both unique and more even. A date can be useful if it is combined with something broad:

TEXT
PK = SENSOR#device-77#2026-03-14     (one partition per device per day)
SK = 09:30:15.123Z

Here device-77 spreads the load, and the date keeps any one partition from growing without bound. The price: to read "the last week of readings for a device", you run seven queries (one per day) and merge them. That is a typical trade-off and one you can make deliberately.

Write sharding for hot keys

When one key is inherently hot (a global counter, a live leaderboard, the "all orders" collection), split it into N shards by appending a suffix, and fan out on read.

TEXT
PK = SCORES#0 ... SCORES#9     (writes pick a random shard)

There are two common ways to choose the suffix:

  • Random: random 0..N-1. Best write spread, but a reader must query all N shards.
  • Calculated: a hash of an item attribute (hash(orderId) % N). The same item always maps to the same shard, so a single known item can still be fetched with one read.
Python Stand-in: random shards spread a hot key

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each shard takes about a tenth of the traffic. The single key takes all of it. With a per-partition write limit near 1,000 per second, this is the difference between throttling and smooth sailing for 5,000 writes per second.

Reading back from shards

The cost of sharding appears on the read side: you query every shard and combine the results.

Python Stand-in: fan out a read across shards

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice each shard only returns its own top n; merging those gives the true top n. That is the pattern in real code too, usually with the queries issued in parallel.

A lookup item for a different key

If you need to find a user by email but the table is keyed by UserId, do not scan. Add a second item (or a global secondary index, covered later) keyed by email that points back to the user id.

TEXT
PK = USER#u-1001        SK = PROFILE        Email = [email protected]
PK = EMAIL#amina@...    SK = LOOKUP         UserId = u-1001

A conditional write on the lookup item (attribute_not_exists(PK)) also gives you uniqueness of email addresses, because DynamoDB has no unique constraint other than the primary key.

Checklist before you commit

  • Can I name each access pattern and the exact key values it supplies?
  • How many distinct partition key values will exist? Millions is comfortable, dozens is not.
  • What is the busiest single key's share of traffic, now and at 10 times the load?
  • Does any key's item collection grow without limit? If yes, bucket it by time or number.
  • Can the key values be built from information I always have at request time?
  • If a key is inherently hot, have I planned shards or a cache in front of it?

Measure with real or realistic data before committing. Count how many requests each partition key would get in a day of production traffic. The busiest handful tell you whether the design survives.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which of these is the best partition key for a user-profile table?

    UserId has high cardinality and traffic spreads across many users. The other options have few values or concentrate traffic.
  2. 2. What is write sharding?

    Appending a shard number such as #0 to #9 divides one hot key into several, spreading the writes.
  3. 3. What is the cost of sharding a key?

    Because items are spread over several keys, a complete read fans out to all shards.
  4. 4. How can you enforce unique email addresses in DynamoDB?

    Only the primary key is unique. A separate item keyed by the email, written with attribute_not_exists, gives you uniqueness.

Summary

  • A good partition key has high cardinality and spreads traffic evenly.
  • Low-cardinality keys (status, country, type) and date-only keys create hot partitions.
  • Composite keys and prefixes make keys unique and better distributed.
  • Write sharding splits a hot key into shards; reads fan out and merge.
  • Test the key against realistic traffic before locking it in.

Next: Item Size and Limits