DynamoDB
Table Design
Single Table
Multiple Tables
Database Optimization

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.

Practice system design

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

  1. Reduced Network Calls: By organizing data in a single table, queries can retrieve multiple types of entities with fewer network calls.
  2. Simplified Query Logic: Complex joins and filtering logic that would otherwise require multiple queries are simplified.
  3. Maintenance: Schema changes and indexes management become easier as they're applied universally across the table.
  4. 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 KeySort KeyAttribute
`USER#``<ID>```METADATAName, 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.