Getting Started
Amazon DynamoDB Introduction
Learn DynamoDB — Amazon's fully managed, serverless NoSQL database that scales to any workload with single-digit millisecond performance.
What is Amazon DynamoDB?
Amazon DynamoDB is a fully managed, serverless NoSQL database service provided by AWS. You don't provision servers, manage OS patches, or worry about scaling — AWS handles all of it. You pay for the reads and writes your application performs, not for idle capacity.
DynamoDB was built at Amazon to solve an internal problem: during peak shopping events like Prime Day, they needed a database that could scale to millions of requests per second with predictable, single-digit millisecond latency — regardless of scale. That system became DynamoDB, launched publicly in 2012.
DynamoDB's Core Properties
Fully serverless — No servers to manage. Capacity scales automatically.
Predictable performance at any scale — Whether your table has 1 row or 1 trillion rows, DynamoDB delivers consistent single-digit millisecond reads and writes.
Global replication — DynamoDB Global Tables replicate your data across multiple AWS regions automatically, with active-active writes.
Built-in high availability — DynamoDB automatically replicates data across three availability zones within a region.
Two capacity modes:
- On-Demand — Pay per request. Scales instantly. No capacity planning.
- Provisioned — Pre-specify read/write capacity. Cheaper for predictable workloads.
DynamoDB vs. Other Databases
| Feature | DynamoDB | MongoDB Atlas | PostgreSQL |
|---|---|---|---|
| Hosting | AWS-only managed | Multi-cloud | Self-hosted or managed |
| Scaling | Serverless, automatic | Manual or auto-scaling | Vertical / read replicas |
| Query model | Key-based, limited | Flexible queries | Full SQL |
| Consistency | Eventual or strong | Configurable | ACID |
| Best for | AWS-native, unpredictable scale | Flexible document queries | Relational data, complex queries |
Core Concepts
Tables
DynamoDB stores data in tables. Each table stores a collection of items (rows). Unlike SQL, tables don't have a fixed schema — items can have different attributes.
Items and Attributes
An item is a collection of attributes (like a JSON object). Every item must have the table's primary key, but other attributes are flexible.
Primary Key
Every table must have a primary key. Two options:
- Simple primary key — Just a Partition Key (hash key)
- Composite primary key — A Partition Key + Sort Key (range key)
GSI and LSI (Secondary Indexes)
DynamoDB can only efficiently query by primary key. For other access patterns, you create:
- Global Secondary Index (GSI) — A new partition key + optional sort key, on any attributes
- Local Secondary Index (LSI) — Same partition key, different sort key
Setting Up
AWS SDK v3 (JavaScript):
npm install @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodbLocal Development with DynamoDB Local:
docker run -p 8000:8000 amazon/dynamodb-localimport { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
const client = new DynamoDBClient({
region: 'us-east-1',
// For local development:
// endpoint: 'http://localhost:8000',
// credentials: { accessKeyId: 'local', secretAccessKey: 'local' },
});
const docClient = DynamoDBDocumentClient.from(client);Example
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, PutCommand, GetCommand } from '@aws-sdk/lib-dynamodb';
const client = new DynamoDBClient({ region: 'us-east-1' });
const docClient = DynamoDBDocumentClient.from(client);
// Write an item
await docClient.send(new PutCommand({
TableName: 'Users',
Item: {
PK: 'USER#alice@example.com',
SK: 'PROFILE',
name: 'Alice Smith',
role: 'admin',
createdAt: new Date().toISOString(),
},
}));
// Read an item
const result = await docClient.send(new GetCommand({
TableName: 'Users',
Key: { PK: 'USER#alice@example.com', SK: 'PROFILE' },
}));
console.log(result.Item);Want to run this code interactively?
Try in Compiler