DynamoDb table design Single table or multiple tables
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance. One of the most debated design choices when working with DynamoDB is whether to use a single table for all entities or multiple tables for different types of data. Understanding the trade-offs between these approaches helps in choosing the right design pattern for specific application needs.
Single Table Design
Overview
In a single table design, all entities are stored in one table. Each item in the table must have a primary key which can be partition key alone or a combination of partition key and sort key. Secondary indexes can be used for additional access patterns.
Benefits
- Reduced Network Calls: By organizing data in a single table, queries can retrieve multiple types of entities with fewer network calls.
- Simplified Query Logic: Complex joins and filtering logic that would otherwise require multiple queries are simplified.
- Maintenance: Schema changes and indexes management become easier as they're applied universally across the table.
- Cost Efficiency: Paying for operations on fewer tables reduces operational costs.
Considerations
- Access Patterns: Must consider all access patterns in advance and design the primary key schema to support them.
- Scalability: The design needs to handle bursts of traffic evenly distributed across keys to avoid throttling.
- Complexity: A rich understanding of the access patterns is vital to avoid mistakes that require redesign.
Example
Imagine a blogging application with entities like Users, Posts, and Comments. In a single table design, these entities can be stored in a table named "BlogApp":
| Partition Key | Sort Key | Attribute |
| `USER#``<ID>``` | METADATA | Name, Email |
| `USER#``<ID>``` | `POST#``<POSTID>``` | Title, Body |
| `POST#``<POSTID>``` | `COMMENT#``<COMMENTID>``` | CommentText |
Query Example
- Increased Complexity: Aggregating data across tables is more complex, potentially requiring additional logic.
- Increased Cost: More tables might incur higher costs due to operations, read/write capacity, etc.
- Higher Latency: Queries may require aggregating data over multiple tables, leading to increased latency.
Related reading
- DynamoDB The provided key element does not match the schema
- Dynamodb TTL 24hours
- DynamoDB update fails when nested path not exists
- DynamoDB updateItem only if it already exists
- DynamoDB updateItem only if it already exists
- DynamoDB Update/Put throttled despite high provisioned capacity
- DynamoDB/Redis activity stream help needed
- e-commerce Algorithm for calculating discounts

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.