AWS DynamoDB — NoSQL Database
AWS DynamoDB — NoSQL Database
Section titled “AWS DynamoDB — NoSQL Database”Amazon DynamoDB is a fully managed, serverless, key-value and document NoSQL database designed for single-digit millisecond performance at any scale. No servers to manage, no schema to define upfront.
In Azure terms: AWS DynamoDB = Azure Cosmos DB
Core Concepts
Section titled “Core Concepts”| Concept | Description |
|---|---|
| Table | A collection of items (like a table in SQL, but schema-less) |
| Item | A single record in a table (like a row) — up to 400 KB |
| Attribute | A field in an item (like a column, but each item can have different attributes) |
| Partition Key | The primary key — DynamoDB hashes this to determine which partition stores the item |
| Sort Key | Optional second part of the primary key — enables range queries within a partition |
| LSI | Local Secondary Index — alternate sort key, same partition key |
| GSI | Global Secondary Index — alternate partition key + sort key (most flexible) |
Primary Key Types
Section titled “Primary Key Types”| Type | Structure | Use When |
|---|---|---|
| Simple (Hash only) | Partition key | Items are accessed by a single unique identifier |
| Composite (Hash + Range) | Partition key + sort key | Items share a partition key but need ordering/range queries |
Example: A social media posts table:
- Partition key:
userId - Sort key:
timestamp - Query: “Get all posts by user X from the last 30 days” ✅
Capacity Modes
Section titled “Capacity Modes”| Mode | Description | Best For |
|---|---|---|
| On-Demand | Pay per request (read/write units) | Unpredictable traffic |
| Provisioned | Set read/write capacity units (RCU/WCU) | Predictable, steady traffic |
| Auto Scaling | Automatically adjusts provisioned capacity | Variable but predictable |
- RCU (Read Capacity Unit): 1 strongly consistent read/sec for up to 4 KB
- WCU (Write Capacity Unit): 1 write/sec for up to 1 KB
Free tier: 25 GB storage, 25 WCU, 25 RCU permanently free (always, not just 12 months).
DynamoDB Features
Section titled “DynamoDB Features”Streams
Section titled “Streams”DynamoDB Streams capture a time-ordered log of item changes:
- Trigger Lambda functions on inserts, updates, deletes
- Use for real-time data processing, event sourcing, replication
Global Tables
Section titled “Global Tables”Multi-region, multi-active replication:
- Write to any region, automatically replicated globally
- < 1 second replication lag
- Ideal for globally distributed applications
DAX (DynamoDB Accelerator)
Section titled “DAX (DynamoDB Accelerator)”In-memory cache for DynamoDB:
- Microsecond read latency (vs millisecond)
- API-compatible — no application code changes
- Best for read-heavy workloads
TTL (Time to Live)
Section titled “TTL (Time to Live)”Automatically delete items after a specified timestamp:
{ "userId": "user-123", "sessionToken": "abc...", "expiry": 1735689600 ← Unix timestamp, auto-deleted after this}DynamoDB vs Azure Cosmos DB
Section titled “DynamoDB vs Azure Cosmos DB”| Feature | DynamoDB | Cosmos DB |
|---|---|---|
| Model | Key-value + document | Multi-model (document, graph, key-value, table, Cassandra) |
| APIs | DynamoDB API, PartiQL | SQL, MongoDB, Gremlin, Cassandra, Table |
| Consistency | Eventual or strong (per-read) | 5 levels (strong to eventual) |
| Global distribution | Global Tables (multi-active) | Turnkey global distribution |
| In-memory cache | DAX | Built-in (cache tier) |
| Partitioning | Partition key (hash) | Partition key |
| Max item size | 400 KB | 2 MB (document) |
| Free tier | 25 GB + 25 WCU/RCU (permanent) | 1000 RU/s + 25 GB (permanent) |
Access Patterns First
Section titled “Access Patterns First”DynamoDB design is access pattern first — unlike SQL where you design the schema and query as needed.
Design steps:
- List all your application’s queries (access patterns)
- Design the primary key to satisfy the most common queries
- Use GSIs and LSIs for secondary access patterns
- Avoid table scans (very expensive)
CLI Examples
Section titled “CLI Examples”# Create a tableaws dynamodb create-table \ --table-name Users \ --attribute-definitions \ AttributeName=userId,AttributeType=S \ AttributeName=createdAt,AttributeType=S \ --key-schema \ AttributeName=userId,KeyType=HASH \ AttributeName=createdAt,KeyType=RANGE \ --billing-mode PAY_PER_REQUEST
# Put an itemaws dynamodb put-item \ --table-name Users \ --item '{"userId":{"S":"u-001"},"createdAt":{"S":"2024-01-01"},"name":{"S":"Alice"}}'
# Get an itemaws dynamodb get-item \ --table-name Users \ --key '{"userId":{"S":"u-001"},"createdAt":{"S":"2024-01-01"}}'
# Query items by partition keyaws dynamodb query \ --table-name Users \ --key-condition-expression "userId = :uid" \ --expression-attribute-values '{":uid":{"S":"u-001"}}'