SkillSprout
Chapter 7 · Lesson 3
Advanced
8 min read

DynamoDB

Overview

Amazon DynamoDB is a fully managed, serverless NoSQL key-value and document database that delivers single-digit-millisecond performance at any scale. There are no servers, patches or capacity to size up front: you create a table, define a primary key, and DynamoDB spreads data over as many partitions as needed, replicating it across three AZs automatically.

Items (rows) in a table can have different attributes and are accessed by their primary key, either a single partition key or a partition key + sort key pair. Because DynamoDB is optimised for access by key, you design tables around your access patterns instead of normalising data as in SQL, and you avoid expensive full-table scans.

DynamoDB offers two capacity modes (on-demand and provisioned), secondary indexes, Streams for change data capture, Global Tables for multi-Region active-active replication, DAX for microsecond caching and point-in-time recovery. Expect many SAA-C03 questions about capacity calculations, index choice and when to pick DynamoDB over RDS.

Key Concepts

  • Serverless NoSQL, replicated across 3 AZs, single-digit-ms latency; max item size 400 KB; tables scale to virtually unlimited size.
  • Primary key: partition key alone, or partition key + sort key. A good key has high cardinality to spread load and avoid hot partitions.
  • Capacity modes: On-demand (pay per request, no planning) and Provisioned (RCU/WCU, optional auto scaling, cheaper for steady load).
  • 1 RCU = one strongly consistent read of up to 4 KB/s (or two eventually consistent); 1 WCU = one write up to 1 KB/s; transactional operations cost 2x.
  • Indexes: GSI (different keys, own capacity, eventual consistency, add anytime, default 20 per table) and LSI (same partition key, created only at table creation, max 5, shares table capacity).
  • DAX is an in-memory cache for DynamoDB giving microsecond reads with no application logic change; Streams capture item changes for 24 hours to trigger Lambda.
  • Global Tables provide multi-Region, multi-active replication (last-writer-wins); PITR restores to any second in the last 35 days; TTL deletes expired items for free.
  • ACID transactions (up to 100 items, 4 MB), encryption at rest always on (KMS), IAM fine-grained access, and Standard-IA table class for cold data.

Detailed Notes

Data model

A table holds items, each a collection of attributes (string, number, binary, boolean, list, map, set). Only the key attributes are required; everything else is schema-less.

Key typeDescriptionExample
Partition key (hash)Determines the partition; must be unique if used aloneUserId
Partition + sort key (composite)Items share a partition key, ordered by sort key; enables range queriesCustomerId + OrderDate
python
import boto3
table = boto3.resource("dynamodb").Table("Orders")

table.put_item(Item={"CustomerId": "C123", "OrderDate": "2025-03-01", "Total": 42})
# Query - efficient, uses the key
resp = table.query(
    KeyConditionExpression="CustomerId = :c AND OrderDate BETWEEN :a AND :b",
    ExpressionAttributeValues={":c": "C123", ":a": "2025-01-01", ":b": "2025-03-31"},
)
# Scan - reads the whole table, avoid in production

Capacity modes and calculations

On-demandProvisioned
PricingPer read/write requestPer RCU/WCU-hour
PlanningNone; instantly accommodates trafficYou set capacity (+ auto scaling)
Best forUnknown or spiky traffic, new appsPredictable load, reserved capacity discounts

Worked examples

  • Read 10 items/s of 6 KB strongly consistent: 6 KB rounds up to 8 KB = 2 RCU per item, so 20 RCU. Eventually consistent: 10 RCU. Transactional: 40 RCU.
  • Write 5 items/s of 2.5 KB: rounds up to 3 KB = 3 WCU each, so 15 WCU.
  • Capacity is spread across partitions; a partition supports about 3,000 RCU and 1,000 WCU, and excess leads to ProvisionedThroughputExceededException (throttling) - use exponential backoff.

Secondary indexes

GSILSI
KeysDifferent partition and sort keySame partition key, different sort key
CreatedAny timeOnly at table creation
CapacityIts own RCU/WCUShares the table's
ConsistencyEventual onlyEventual or strong
Limit20 per table5 per table, 10 GB per partition key

Performance and caching

  • DAX (DynamoDB Accelerator) is a write-through, cluster-based cache giving microsecond latency for read-heavy and repeat-read workloads; it is API-compatible with DynamoDB. It does not help write-heavy workloads or strongly consistent reads that bypass the cache.
  • Use BatchGetItem (up to 100 items) and BatchWriteItem (25 items) to cut round trips; use Query rather than Scan, and parallel scan only for exports.

Durability, backup and replication

  • On-demand backups and PITR (continuous, 35 days) do not use table capacity; AWS Backup supports cross-Region and cross-account copies. Restores create a new table.
  • Global Tables replicate to chosen Regions with typically under a second lag and require Streams; conflict resolution is last writer wins.
  • DynamoDB Streams records item-level changes for 24 hours and triggers Lambda; Kinesis Data Streams integration retains longer.
  • Export to S3 and import from S3 work without using table capacity.
  • TTL marks expiry with an epoch timestamp attribute and deletes items in the background at no cost (typically within 48 hours).

Security

Encryption at rest is always on (AWS owned, AWS managed or customer managed KMS key). Use IAM conditions such as dynamodb:LeadingKeys for item-level, per-user access, VPC gateway endpoints for private access and CloudTrail for auditing. Table class Standard-IA lowers storage cost by roughly 60% for rarely read data.

DynamoDB vs RDS

Choose DynamoDB for key-based access, massive scale, serverless operation and flexible schema (shopping carts, sessions, IoT, leaderboards, gaming). Choose RDS/Aurora for complex joins, ad-hoc SQL and strict relational integrity.

Real World Example

Scenario — a mobile gaming company stores player profiles and session state for 40 million users.

Traffic is highly spiky: it can jump tenfold during live events and falls overnight. Latency must stay in single-digit milliseconds worldwide.

The architects choose DynamoDB in on-demand mode with PlayerId as the partition key (high cardinality, even distribution) and ItemType#Timestamp as the sort key. A GSI on GuildId supports guild leaderboards. DAX in front of the table serves the hot leaderboard reads in microseconds. Global Tables replicate to three Regions so players read and write near home. A TTL attribute expires temporary session items automatically, and Streams feed a Lambda function that updates analytics. Backups use PITR with a 35-day window.

The platform absorbed a 12x traffic spike with no capacity changes and no operations staff paged.

Architecture Diagram

DynamoDB with DAX, Streams and Global Tables
Architecture
AWS Cloud
Players
API Gateway
Lambda
Region 1
DAX clustermicrosecond reads
DynamoDB tableon-demand, PK + SK
Stream processor
Region 2
Replica tablemulti-active
PITR / Backup

Reads are accelerated by DAX, changes flow to Lambda via Streams, and Global Tables replicate across Regions.

  1. 1Clients call an API; Lambda reads and writes DynamoDB by primary key.
  2. 2Hot reads are served from DAX in microseconds; cache misses fall through to the table.
  3. 3DynamoDB Streams emits item changes that trigger a Lambda function for analytics and notifications.
  4. 4Global Tables replicate writes to other Regions for local latency and Regional resilience.

Best Practices

  • ✓Design for access patterns first: choose a high-cardinality partition key and use sort keys and GSIs to answer queries without scans.
  • ✓Use on-demand for new or unpredictable workloads and provisioned with auto scaling (plus reserved capacity) for steady traffic.
  • ✓Avoid hot partitions: add suffixes or sharding for keys that receive disproportionate traffic.
  • ✓Prefer Query over Scan, use projection expressions, and use batch operations to cut cost and latency.
  • ✓Add DAX or ElastiCache for read-heavy hot items, and use TTL to delete short-lived data for free.
  • ✓Enable PITR and AWS Backup, and use Global Tables for multi-Region availability requirements.
  • ✓Use IAM least privilege with leading-key conditions, VPC gateway endpoints and CMKs where policy demands them.
  • ✓Implement exponential backoff and jitter for throttled requests; keep items small (400 KB limit) and store large blobs in S3 with a pointer.

Common Interview Questions

Exam Tips

SAA-C03 focus

  • 1 RCU = 4 KB strongly consistent (2 eventually consistent); 1 WCU = 1 KB. Always round up to the next 4 KB / 1 KB. Transactions cost double.
  • "Microsecond latency for DynamoDB reads" -> DAX. "Relational cache for any DB" -> ElastiCache.
  • "Multi-Region, multi-active, local reads and writes" -> Global Tables. "Restore to any second in the last 35 days" -> PITR.
  • LSI only at table creation (5 max); GSI anytime (20 default). Scan is inefficient; Query uses the key.
  • "Unpredictable traffic, no capacity planning" -> on-demand. "Throttling errors" -> increase capacity, fix hot keys, or use backoff/DAX.
  • "React to table changes" -> DynamoDB Streams + Lambda; "auto-delete expired items" -> TTL.
  • Max item size 400 KB; larger objects go to S3 with a reference stored in DynamoDB.
  • DynamoDB is always encrypted at rest, replicated across 3 AZs and accessed privately with a gateway VPC endpoint.

Finished this lesson?

0 of 64 lessons done · 0% of the course