Lesson 6 of 30Core services
Databases on AWS
Choose between RDS, Aurora, DynamoDB and ElastiCache, and design keys and replicas that fit your access patterns.
Picking the database is the decision that is hardest to undo. Compute you can swap in an afternoon; moving 400 GB of production data between engines is a quarter-long project. AWS offers a dozen database services, but four of them cover almost every web app, and interviewers expect you to explain why you would pick one over another.
RDS: managed Postgres and MySQL
Amazon RDS (Relational Database Service) runs a normal database engine (PostgreSQL, MySQL, MariaDB, SQL Server, Oracle) on instances AWS manages. You still pick an instance class (like db.t4g.medium or db.r7g.large) and storage size, but AWS handles:
- installing and patching the engine (in a maintenance window you choose),
- automated daily backups with point-in-time restore (to any second within your retention, up to 35 days),
- failover, if you turn on Multi-AZ.
Your app connects with the same driver as always (psycopg, asyncpg, SQLAlchemy, Prisma). It is just Postgres with a hostname like app-db.c9xyz.us-east-1.rds.amazonaws.com.
aws rds create-db-instance \ --db-instance-identifier app-db \ --engine postgres --engine-version 17 \ --db-instance-class db.t4g.micro \ --allocated-storage 20 \ --master-username app_admin --manage-master-user-password \ --db-subnet-group-name app-db-subnets \ --vpc-security-group-ids sg-0db \ --no-publicly-accessible--manage-master-user-password stores the password in Secrets Manager and rotates it, so it never appears in your shell history. --no-publicly-accessible plus private DB subnets (from VPC Networking) means the database has no path from the internet.
Multi-AZ vs read replicas
These two features sound similar and do different jobs.
The API sends all writes (and normal reads) to the primary.
- Multi-AZ is for availability. Turn it on for production.
- Read replicas are for read scaling. Add them when reads outgrow one instance. Because they lag, do not read your own just-written data from a replica.
Aurora: AWS's cloud-native relational engine
Amazon Aurora is MySQL- and PostgreSQL-compatible, but AWS rebuilt the storage layer. Data is stored 6 times across 3 AZs on a shared distributed volume, so replicas do not need their own copy of the data.
What that buys you:
- up to 15 low-lag read replicas sharing one storage volume, and failover often under 30 seconds,
- storage that grows automatically (no picking a disk size),
- Aurora Serverless v2, which scales capacity up and down in small steps (ACUs) with load, and can scale to zero when idle for dev databases,
- Global Database for cross-Region replicas.
The trade-off: it costs more than plain RDS for small, steady workloads, and I/O-heavy workloads should look at the I/O-Optimized configuration. Start on RDS Postgres; move to Aurora when you need its scaling or failover speed.
DynamoDB: key-value at any scale
Amazon DynamoDB is a fully managed NoSQL database. No instances, no connections to pool, single-digit millisecond reads at any size. In exchange, you must design around your access patterns up front, because you can only query efficiently by key.
Every item has a primary key:
- Partition key (
pk): DynamoDB hashes it to decide which partition stores the item. Every query must name one partition key. - Sort key (
sk, optional): items with the same partition key are stored together, sorted by sort key. You can query ranges and prefixes on it.
Single-table design puts several entity types in one table, shaping keys so one query returns everything a page needs:
| pk | sk | other attributes |
|---|---|---|
USER#42 | PROFILE | name: Ada, plan: pro |
USER#42 | ORDER#2026-09-30#A17 | total: 49.00 |
USER#42 | ORDER#2026-10-12#B02 | total: 12.50 |
USER#7 | PROFILE | name: Sam |
"Get user 42's orders, newest first" is one Query: pk = USER#42 AND begins_with(sk, "ORDER#"), scanning backwards. Putting ISO dates in the sort key is what makes them come back in order.
Press Run (⌘ Enter) to see what happens.And the same call with boto3:
import boto3
from boto3.dynamodb.conditions import Key
table = boto3.resource("dynamodb").Table("app")
resp = table.query(
KeyConditionExpression=Key("pk").eq("USER#42") & Key("sk").begins_with("ORDER#"),
ScanIndexForward=False, # newest first
Limit=20,
)For questions your primary key cannot answer ("all orders over $100 this week"), you add a global secondary index (GSI) with different keys, or you accept that DynamoDB is the wrong tool for ad-hoc analytics. A Scan reads the whole table and is almost never what you want in an API path.
Capacity modes:
- On-demand: pay per read and write request. No planning. The right default for new or spiky apps.
- Provisioned: you set read and write capacity units (optionally with auto scaling). Cheaper for steady, predictable traffic.
date (like 2026-10-20). Writes are getting throttled at peak. Why?ElastiCache: Redis and Valkey
Amazon ElastiCache runs managed in-memory caches: Valkey (the open-source fork of Redis that AWS now leads with, and prices lower), Redis OSS, and Memcached. There is also a serverless option that scales without picking nodes.
Typical uses: caching expensive query results, session storage, rate-limit counters, leaderboards with sorted sets. The usual pattern is cache-aside: check the cache, on a miss read the database and write the result to the cache with a TTL.
Like RDS, it lives in private subnets, and your app's security group must be allowed on port 6379.
Choosing between them
| RDS (Postgres/MySQL) | Aurora | DynamoDB | ElastiCache | |
|---|---|---|---|---|
| Model | Relational, SQL | Relational, SQL | Key-value / document | In-memory key-value |
| Queries | Any SQL, joins, ad hoc | Any SQL, joins, ad hoc | By key and sort key (plus GSIs) | By key |
| Scaling | Bigger instance, read replicas | Up to 15 replicas, Serverless v2 | Practically unlimited, automatic | Add shards/nodes, or serverless |
| Ops | Low | Lower | Almost none | Low |
| Pick it when | Default for most apps | Need fast failover or big read scale | Known access patterns, huge or spiky scale | Speed up reads, sessions, counters |
Match the workload to the service.
A DynamoDB Query
In DynamoDB, a Query picks one partition key (pk) and optionally a sort key condition. Items inside a partition come back sorted by sort key (sk).
Write query(items, pk, sk_prefix) that returns the sk values of every item whose pk equals the given one and whose sk begins with sk_prefix, sorted ascending (plain string order). An empty prefix matches every item in the partition.
items = [{"pk": "USER#1", "sk": "PROFILE"},
{"pk": "USER#1", "sk": "ORDER#2026-03-02"},
{"pk": "USER#2", "sk": "ORDER#2026-01-15"},
{"pk": "USER#1", "sk": "ORDER#2026-01-09"}]
query(items, "USER#1", "ORDER#") → ["ORDER#2026-01-09", "ORDER#2026-03-02"]
query(items, "USER#1", "") → ["ORDER#2026-01-09", "ORDER#2026-03-02", "PROFILE"]- Multi-AZ
- A synchronous standby in another AZ that RDS promotes on failure. For availability, not read scaling.
- read replica
- An asynchronous, read-only copy for scaling reads. Can lag behind the primary.
- Aurora
- AWS's MySQL/Postgres-compatible engine with storage replicated 6 ways across 3 AZs.
- partition key
- The DynamoDB key that is hashed to choose a partition. Every Query names exactly one.
- sort key
- Orders items within a partition, enabling range and
begins_withqueries. - cache-aside
- Read from cache; on a miss, read the database and store the result with a TTL.