Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. DynamoDB lets you offload the administrative burdens of operating and scaling a distributed database so that you don't have to worry about hardware provisioning, setup and configuration, replication, software patching, or cluster scaling.
With DynamoDB, you can create database tables that can store and retrieve any amount of data and serve any level of request traffic. You can scale up or scale down your tables' throughput capacity without downtime or performance degradation.
If you're familiar with relational databases like MySQL or PostgreSQL, you'll need to adjust your thinking when working with DynamoDB:
The basic DynamoDB components are:
When you create a table, you must specify its primary key. The primary key uniquely identifies each item in the table, so that no two items can have the same key. DynamoDB supports two types of primary keys:
DynamoDB stores data in partitions. A partition is an allocation of storage for a table, backed by solid-state drives (SSDs) and automatically replicated across multiple Availability Zones within an AWS Region. Partition management is handled entirely by DynamoDB—you never have to manage partitions yourself.
When you create a table, DynamoDB allocates enough partitions to handle your provisioned throughput requirements. As your application data and throughput requirements change, DynamoDB automatically adjusts by splitting busy partitions and distributing the data over a larger number of partitions.
// Example CloudFormation template for a DynamoDB table
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"UsersTable": {
"Type": "AWS::DynamoDB::Table",
"Properties": {
"TableName": "Users",
"BillingMode": "PAY_PER_REQUEST",
"AttributeDefinitions": [
{
"AttributeName": "userId",
"AttributeType": "S"
},
{
"AttributeName": "email",
"AttributeType": "S"
}
],
"KeySchema": [
{
"AttributeName": "userId",
"KeyType": "HASH"
}
],
"GlobalSecondaryIndexes": [
{
"IndexName": "EmailIndex",
"KeySchema": [
{
"AttributeName": "email",
"KeyType": "HASH"
}
],
"Projection": {
"ProjectionType": "ALL"
}
}
]
}
}
}
}
The partition key is used to determine the physical partition where the item will be stored. A well-designed partition key will:
Good candidates for partition keys include:
Poor candidates for partition keys include:
A composite key (partition key + sort key) allows you to:
Common patterns include:
Unlike relational databases where you can add indexes later, with DynamoDB you need to identify your access patterns upfront and design your tables accordingly:
// Example of DynamoDB table design for a social media app
// Primary table: UserId (partition) and ItemType#Timestamp (sort)
// Example items in the table:
// User profile:
{
"UserId": "U123",
"ItemType": "PROFILE",
"Name": "John Doe",
"Email": "john@example.com"
}
// User post:
{
"UserId": "U123",
"ItemType": "POST#2023-06-15T14:30:00Z",
"Content": "Hello world!",
"Likes": 42
}
// User follower:
{
"UserId": "U123",
"ItemType": "FOLLOWER#U456",
"FollowerName": "Jane Smith"
}
Learn about how DynamoDB stores and manages data across partitions.
Learn best practices for designing DynamoDB tables to optimize for your access patterns.
Learn how to create and configure DynamoDB tables using AWS CloudFormation.
In the guided project for this module, you'll apply what you've learned about DynamoDB table design to create the database tables for your Learn and Be Curious project.
This project will guide you through designing and creating DynamoDB tables based on the API you designed in Module 2.
Work on the tables section of your design document, defining the DynamoDB tables needed for your project.
Official AWS documentation for Amazon DynamoDB.
AWS guide for DynamoDB best practices.
Examples of creating DynamoDB tables with CloudFormation.