Skip to content

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

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)
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” ✅
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 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

Multi-region, multi-active replication:

  • Write to any region, automatically replicated globally
  • < 1 second replication lag
  • Ideal for globally distributed applications

In-memory cache for DynamoDB:

  • Microsecond read latency (vs millisecond)
  • API-compatible — no application code changes
  • Best for read-heavy workloads

Automatically delete items after a specified timestamp:

{
"userId": "user-123",
"sessionToken": "abc...",
"expiry": 1735689600 ← Unix timestamp, auto-deleted after this
}
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)

DynamoDB design is access pattern first — unlike SQL where you design the schema and query as needed.

Design steps:

  1. List all your application’s queries (access patterns)
  2. Design the primary key to satisfy the most common queries
  3. Use GSIs and LSIs for secondary access patterns
  4. Avoid table scans (very expensive)
Terminal window
# Create a table
aws 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 item
aws dynamodb put-item \
--table-name Users \
--item '{"userId":{"S":"u-001"},"createdAt":{"S":"2024-01-01"},"name":{"S":"Alice"}}'
# Get an item
aws dynamodb get-item \
--table-name Users \
--key '{"userId":{"S":"u-001"},"createdAt":{"S":"2024-01-01"}}'
# Query items by partition key
aws dynamodb query \
--table-name Users \
--key-condition-expression "userId = :uid" \
--expression-attribute-values '{":uid":{"S":"u-001"}}'