AWS Builder Center

Demystifying DynamoDB: A Beginner's Guide to Single-Table Design.

Stop trying to write SQL in NoSQL! Learn the core concepts of DynamoDB Single-Table Design and how to model your data for single-digit millisecond latency at any scale.

When developers migrate from traditional relational databases (like PostgreSQL or MySQL) to Amazon DynamoDB, they often bring their SQL habits with them. They create a Users table, an Orders table, and a Products table, and then try to figure out how to join them.
The problem? DynamoDB doesn't do joins.
To achieve the single-digit millisecond latency that DynamoDB is famous for, you have to adopt a completely different mindset: Single-Table Design.
In this article, we’ll break down what Single-Table Design is, why it's the gold standard for NoSQL on AWS, and how to model your data effectively.

Why Use One Table?

In a relational database, you normalize your data to reduce redundancy and use SQL JOIN operations to stitch it back together at query time. However, joining tables requires CPU overhead and disk seeks, which slow down your application as your dataset grows to terabytes.
DynamoDB is designed to scale infinitely without performance degradation. To guarantee fast reads, it requires you to pre-join your data. By putting all related items into a single table, you can retrieve a user, their profile details, and their last five orders in a single, lightning-fast database query.

The Secret Sauce: Generic Keys

In a multi-table setup, your partition key (PK) is usually obvious: userId for the Users table, orderId for the Orders table.
In a Single-Table Design, your table holds different types of entities. Therefore, your keys must be generic. We typically name the partition key PK and the sort key SK.
Let's look at an e-commerce example where we need to store Users and their Orders.

Storing a User

To store a user, we use a prefix to identify the entity type:
  • PK: USER#123
  • SK: PROFILE#123
  • Attributes: Name, Email, Address

Storing an Order

To store an order that belongs to that user, we use the same partition key but a different sort key:
  • PK: USER#123
  • SK: ORDER#987
  • Attributes: OrderDate, TotalAmount, Status

The Magic of the Query

Because both the User Profile and the Order share the exact same Partition Key (USER#123), they are stored physically next to each other on the same underlying SSD in the AWS data center.
This enables powerful access patterns:
  1. Get the User Profile:
    Query: PK = "USER#123" AND SK = "PROFILE#123"
  2. Get a Specific Order:
    Query: PK = "USER#123" AND SK = "ORDER#987"
  3. Get the User AND All Their Orders in One Request (The "Pre-Join"):
    Query: PK = "USER#123" AND SK begins_with("ORDER#")
By simply asking for the partition key and utilizing the begins_with operator on the sort key, we have effectively executed a JOIN without the performance penalty!

When NOT to use Single-Table Design

While Single-Table Design is incredibly performant, it is not a silver bullet.
It heavily optimizes for read performance at the expense of flexibility. You must know your application's exact access patterns before you design your table. If your startup is constantly pivoting and your data access patterns change weekly, the rigid nature of Single-Table Design can become a bottleneck.
In those cases, using multiple tables or sticking to Amazon Aurora (Relational) might be a better choice until your access patterns stabilize.

Conclusion

Mastering Amazon DynamoDB requires unlearning some relational database concepts. By embracing generic keys (PK and SK) and grouping related entities under the same partition key, you unlock the true power of NoSQL.
Single-Table Design allows your application to fetch complex, relational data in a single network request, ensuring your backend remains blisteringly fast whether you have ten users or ten million.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article