Question: Can I use a UUID as a DynamoDB partition key?

Answered by Rafal Wilinski
Last updated: October 23rd, 2026
Answer
Yes. A UUID makes a good partition key. Random UUIDs provide many distinct partition key values, helping distribute items. They do not prevent a hot item if many requests target the same UUID. Store it as a String, or as 16 bytes of Binary if you want smaller items and keys.
What to plan for:
- You can only look items up by exact ID. With a UUID as the whole primary key,
GetItemby ID is the only efficient read. You can't list, range-query or sort by a random value, so add a global secondary index for each other access pattern, such as "orders by customer". - The partition key doesn't have to be unique. It must be unique only when it's the entire primary key. With a composite key, many items can share one UUID partition key and differ by sort key, for example all line items of one order.
- Time-ordered IDs sort; random ones don't. UUID v4 is fully random. UUID v7, ULID and KSUID start with a timestamp, so they sort by creation time, which makes them useful as sort keys. As partition keys they still distribute well, because DynamoDB hashes the partition key value.
DynamoDB doesn't generate IDs, so create them in your code:
import { randomUUID } from "node:crypto"; import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb"; const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({})); await ddb.send( new PutCommand({ TableName: "Orders", Item: { orderId: randomUUID(), total: 42 }, ConditionExpression: "attribute_not_exists(orderId)", }) );
The condition makes the write fail, instead of silently replacing an item, if the ID somehow already exists. The DynamoDB UUID generator creates v4, v7, ULID and KSUID values and shows which of them sort by time.
Source: AWS DynamoDB documentation.
Other Common DynamoDB FAQ (with Answers)
- How do you access DynamoDB from outside AWS?
- How do you store location coordinates in DynamoDB?
- What are the differences between DynamoDB and Snowflake?
- Is connection pooling needed for DynamoDB?
- Can Django use DynamoDB?
- How do you store JSON on DynamoDB?
- Is a DynamoDB table Region-specific?
- How to track changes to items in DynamoDB?
- What are the naming conventions in DynamoDB?
- How do I create an ID in DynamoDB?
- How do you mock DynamoDB in a test?
- Is DynamoDB open source?
- Does DynamoDB support load balancing?
- How do you handle empty strings in DynamoDB?
- What is an access pattern in DynamoDB?
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today
Product Features
DynamoDB Tools
DynamoDB Info
© 2026 Dynobase