Amazon DynamoDB Overview
AWS Amplify Gen 2 uses Amazon DynamoDB as its data store. Let's understand what DynamoDB is and why it's a great choice for modern applications.
What is DynamoDB?
Amazon DynamoDB is a fully managed NoSQL database service provided by AWS. Unlike traditional relational databases (like MySQL or PostgreSQL), DynamoDB stores data in a flexible, document-like format.
🔑 Key Characteristics
- Serverless: No servers to manage, scales automatically
- Fast: Single-digit millisecond response times
- Scalable: Handles trillions of requests per day
- Flexible: Schema-less design, items can have different attributes
- Integrated: Works seamlessly with other AWS services
NoSQL vs SQL
If you're coming from a SQL background, here's how DynamoDB concepts map to what you know:
| SQL Concept | DynamoDB Concept | Description |
|---|---|---|
| Database | Account/Region | All tables exist within your AWS account |
| Table | Table | Collection of items (same concept) |
| Row | Item | A single record in the table |
| Column | Attribute | A piece of data within an item |
| Primary Key | Partition Key (+ Sort Key) | Unique identifier for items |
| Index | Secondary Index (GSI/LSI) | Alternative ways to query data |
Key Concepts
Partition Key
Every DynamoDB table requires a partition key (also called hash key). This is used to distribute data across partitions for scalability.
// Simple primary key: just partition key
{
"userId": "user123", // Partition key
"email": "user@example.com",
"name": "John Doe"
}
Sort Key
Optionally, you can add a sort key (also called range key) to create a composite primary key. This allows multiple items with the same partition key, sorted by the sort key.
// Composite primary key: partition key + sort key
{
"userId": "user123", // Partition key
"todoId": "todo-001", // Sort key
"content": "Buy groceries",
"completed": false
}
Secondary Indexes
DynamoDB only allows efficient queries on the primary key by default. Secondary indexes let you query on other attributes:
- Global Secondary Index (GSI): Query on any attribute, can have different partition/sort key than table
- Local Secondary Index (LSI): Same partition key as table, different sort key (must be defined at table creation)
Good news! Amplify Data handles most of these details for you. You define your models in TypeScript, and Amplify creates the appropriate DynamoDB tables and indexes automatically.
How Amplify Uses DynamoDB
When you define a data model in Amplify Gen 2, here's what happens behind the scenes:
- Amplify reads your model definition from
amplify/data/resource.ts - AWS CDK generates CloudFormation templates
- DynamoDB tables are created with appropriate keys and indexes
- AWS AppSync GraphQL API is generated for CRUD operations
- TypeScript types are generated for type-safe client operations
âš¡ The Magic of Amplify Data
You write: a.model({ ... })
Amplify creates: DynamoDB table + GraphQL API + TypeScript types
DynamoDB Pricing
DynamoDB has two pricing modes:
| Mode | Best For | How It Works |
|---|---|---|
| On-Demand | Variable/unpredictable traffic | Pay per request, no capacity planning |
| Provisioned | Predictable traffic | Specify read/write capacity, lower cost at scale |
By default, Amplify uses On-Demand mode, which is perfect for development and applications with variable traffic.
AWS offers a generous free tier for DynamoDB: 25 GB of storage and enough read/write capacity for ~200 million requests per month. This is usually more than enough for development and small applications.
Data Access Patterns
With DynamoDB, it's important to think about how you'll access your data upfront. Common patterns include:
- Get by ID: Fetch a single item by its primary key (fastest)
- Query by partition key: Get all items with the same partition key
- Query with filter: Query plus filter on non-key attributes
- Scan: Read all items (expensive, avoid if possible)
Summary
- DynamoDB is a fully managed, serverless NoSQL database
- It uses partition keys and optional sort keys for organizing data
- Secondary indexes enable queries on non-key attributes
- Amplify abstracts most DynamoDB complexity through code-first models
- On-demand pricing with a generous free tier makes it cost-effective