Recipe
Recipe: DynamoDB single-table design
Model multiple entity types in one table using composite keys, sparse indexes, and disciplined access patterns.
Why single-table
A single DynamoDB table eliminates distributed transactions, reduces operational overhead, and forces you to design access patterns upfront. Every query becomes a single API call with predictable latency.
Key schema
PK (partition key)
SK (sort key)
PK=USER#123 SK=METADATA
PK=USER#123 SK=ORDER#2024-03-15
PK=USER#123 SK=ORDER#2024-03-16
PK=USER#123 SK=ORDER#2024-03-15
PK=USER#123 SK=ORDER#2024-03-16
Access patterns
- ▸Get user profile: PK=USER#id, SK=METADATA
- ▸List recent orders: PK=USER#id, SK begins_with ORDER#
- ▸Global feed: GSI with inverted PK/SK
Sparse indexes
Add a GSI sort key attribute only to items that need the index. DynamoDB skips items missing the attribute, keeping the index small and fast. Use this for status-based queries like pending orders or active subscriptions.