Community by FindUtils

DynamoDB Single-Table Design

DynamoDB with single-table design, GSIs, access patterns, and cost optimization.

Claude CodeCursorGitHub CopilotWindsurfClineCodex / OpenAIGemini CLI
Updated 2026-04-05
CLAUDE.md
# DynamoDB Single-Table Design

You are an expert in Amazon DynamoDB, NoSQL data modeling, and serverless databases.

Single-Table Design:
- Model all entities in one table with composite keys (PK, SK)
- Use prefixes for entity types: PK=USER#123, SK=PROFILE or SK=ORDER#456
- Design keys around access patterns, not entity relationships
- Use Generic GSIs (GSI1PK, GSI1SK) for alternate access patterns
- Document all access patterns before designing the key schema

Key Design:
- Partition key: high cardinality to distribute load evenly
- Sort key: enables range queries and hierarchical data
- Use begins_with(SK, 'ORDER#') for filtering entity types
- Use between(SK, '2026-01-01', '2026-12-31') for date ranges
- Composite sort keys: STATUS#CREATED_AT for compound filtering

Query Patterns:
- Use Query (not Scan) for all production reads
- Use FilterExpression for post-query filtering (still reads all items)
- Use ProjectionExpression to return only needed attributes
- Paginate with ExclusiveStartKey / LastEvaluatedKey
- Use BatchGetItem for fetching multiple known items (max 100)

Write Patterns:
- Use ConditionExpression for optimistic locking (attribute_not_exists, =)
- Use TransactWriteItems for cross-item atomic operations (max 100 items)
- Use BatchWriteItem for bulk inserts (max 25 items per batch)
- Implement idempotency with conditional writes

Cost Optimization:
- Use on-demand capacity for unpredictable workloads
- Use provisioned capacity with auto-scaling for steady workloads
- Use DynamoDB Streams + Lambda instead of polling
- Archive cold data to S3 with export-to-S3 feature
- Monitor ConsumedCapacity to right-size throughput

Add to your project root CLAUDE.md file, or append to an existing one.

Tags

dynamodbawsnosqlsingle-tableserverless