0
0
DynamodbConceptBeginner · 4 min read

Single Table Design in DynamoDB: What It Is and How It Works

Single table design in DynamoDB means storing multiple types of related data in one table using composite keys and attributes to distinguish them. This approach reduces the need for multiple tables and complex joins, making queries faster and more efficient.
⚙️

How It Works

Imagine a large filing cabinet where instead of having separate drawers for different document types, you keep all documents in one drawer but use labels and folders to organize them. Single table design in DynamoDB works similarly by storing different kinds of data items in one table. Each item has a partition key and a sort key that act like labels to organize and quickly find the data.

By cleverly designing these keys and adding extra attributes, you can store user profiles, orders, messages, or any related data together. This avoids the need to look through multiple tables and join data, which DynamoDB does not support natively. Instead, you use queries that fetch related data efficiently from the single table.

💻

Example

This example shows a DynamoDB table storing users and their orders in one table. The PK (partition key) identifies the user, and the SK (sort key) distinguishes between user info and orders.
plaintext
Table: SingleTable

Items:
{
  "PK": "USER#123",
  "SK": "PROFILE",
  "Name": "Alice",
  "Email": "alice@example.com"
}
{
  "PK": "USER#123",
  "SK": "ORDER#001",
  "OrderDate": "2024-06-01",
  "Amount": 50
}
{
  "PK": "USER#123",
  "SK": "ORDER#002",
  "OrderDate": "2024-06-15",
  "Amount": 75
}
Output
Query for PK = 'USER#123' returns: - PROFILE item with user details - ORDER#001 and ORDER#002 items with order details
🎯

When to Use

Use single table design when you want to optimize for fast, simple queries in DynamoDB and avoid multiple round trips to the database. It is ideal for applications where related data is accessed together, like user profiles with their orders, messages, or activity logs.

This design is great for high-scale apps such as e-commerce, social media, or gaming, where performance and cost efficiency matter. However, it requires careful planning of keys and access patterns upfront.

Key Points

  • Stores multiple entity types in one DynamoDB table.
  • Uses composite keys to organize and query data efficiently.
  • Reduces complexity by avoiding joins and multiple tables.
  • Improves performance for related data access patterns.
  • Needs thoughtful design of keys and attributes.

Key Takeaways

Single table design stores different data types in one DynamoDB table using composite keys.
It improves query speed by grouping related data together.
This design reduces the need for multiple tables and complex joins.
Best for applications with predictable access patterns and related data.
Requires upfront planning of keys and data organization.