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

ConceptDescription
TableA collection of items (like a table in SQL, but schema-less)
ItemA single record in a table (like a row) — up to 400 KB
AttributeA field in an item (like a column, but each item can have different attributes)
Partition KeyThe primary key — DynamoDB hashes this to determine which partition stores the item
Sort KeyOptional second part of the primary key — enables range queries within a partition
LSILocal Secondary Index — alternate sort key, same partition key
GSIGlobal Secondary Index — alternate partition key + sort key (most flexible)
TypeStructureUse When
Simple (Hash only)Partition keyItems are accessed by a single unique identifier
Composite (Hash + Range)Partition key + sort keyItems 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” ✅
ModeDescriptionBest For
On-DemandPay per request (read/write units)Unpredictable traffic
ProvisionedSet read/write capacity units (RCU/WCU)Predictable, steady traffic
Auto ScalingAutomatically adjusts provisioned capacityVariable 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
}
FeatureDynamoDBCosmos DB
ModelKey-value + documentMulti-model (document, graph, key-value, table, Cassandra)
APIsDynamoDB API, PartiQLSQL, MongoDB, Gremlin, Cassandra, Table
ConsistencyEventual or strong (per-read)5 levels (strong to eventual)
Global distributionGlobal Tables (multi-active)Turnkey global distribution
In-memory cacheDAXBuilt-in (cache tier)
PartitioningPartition key (hash)Partition key
Max item size400 KB2 MB (document)
Free tier25 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"}}'