Managed databases
Running a database yourself means installing it, patching it, backing it up, replicating it to another server, and waking up at 3 a.m. when the disk fills. A managed database service does all of that for you: you pick the engine and size, and the provider handles backups, failover, patches and monitoring. For most teams it's the right default.
Choosing a database service
| Type | AWS | Google Cloud | Azure | Use for |
|---|---|---|---|---|
| Relational (PostgreSQL, MySQL…) | RDS, Aurora | Cloud SQL, AlloyDB | Azure Database for PostgreSQL/MySQL, Azure SQL | Most applications: orders, users, accounts |
| Globally distributed SQL | Aurora DSQL | Spanner | Cosmos DB for PostgreSQL | Very large, multi-region systems |
| Key-value / document (NoSQL) | DynamoDB, DocumentDB | Firestore, Bigtable | Cosmos DB | Huge scale with simple access patterns; mobile/web backends |
| In-memory cache | ElastiCache (Redis/Valkey) | Memorystore | Azure Cache for Redis | Caching, sessions, leaderboards |
| Data warehouse (analytics) | Redshift | BigQuery | Synapse / Fabric | Reports over billions of rows |
Start with managed PostgreSQL unless you have a clear reason not to. The PostgreSQL and SQL courses on this site apply directly.
A managed PostgreSQL database
What you choose when creating one:
| Setting | Notes |
|---|---|
| Engine and version | e.g. PostgreSQL 18 |
| Instance class | CPU and memory (db.t4g.micro for learning, db.r7g.large and up for production) |
| Storage | Size, and whether it grows automatically |
| Multi-AZ | A standby copy in another AZ that takes over automatically (about a minute) if the primary fails |
| Network | A private subnet group; never publicly accessible |
| Backups | Automatic daily backups, kept 1–35 days, with point-in-time restore to any second |
| Credentials | Stored in Secrets Manager (it can rotate them), or IAM authentication |
aws rds create-db-instance --db-instance-identifier shop-db --engine postgres --engine-version 18.1 \
--db-instance-class db.t4g.micro --allocated-storage 20 --storage-type gp3 \
--master-username shopadmin --manage-master-user-password \
--backup-retention-period 7 --no-publicly-accessible --multi-az --storage-encrypted \
--query "DBInstance.[DBInstanceIdentifier, Engine, DBInstanceClass, MultiAZ, PubliclyAccessible, BackupRetentionPeriod]" --output text
aws rds describe-db-instances --db-instance-identifier shop-db \
--query "DBInstances[0].[DBInstanceStatus, Endpoint.Port]" --output textshop-db postgres db.t4g.micro True False 7 available 5432
--manage-master-user-password stores the password in Secrets Manager instead of you choosing one. On a real account, the instance takes 5–15 minutes to become available, then your app connects to its endpoint (shop-db.xxxx.ap-south-1.rds.amazonaws.com:5432) like any PostgreSQL server.
The equivalents:
gcloud sql instances create shop-db --database-version=POSTGRES_18 --tier=db-custom-1-3840 \
--region=asia-south1 --availability-type=REGIONAL --no-assign-ip --network=shop-vpc
az postgres flexible-server create --resource-group demo-rg --name shop-db --version 18 \
--tier Burstable --sku-name Standard_B1ms --high-availability ZoneRedundant --public-access NoneBackups and restore
aws rds create-db-snapshot --db-instance-identifier shop-db --db-snapshot-identifier before-migration \
--query "DBSnapshot.[DBSnapshotIdentifier, Status]" --output text
aws rds describe-db-snapshots --db-instance-identifier shop-db --snapshot-type manual --query "DBSnapshots[].DBSnapshotIdentifier" --output textbefore-migration available before-migration
Automatic backups give point-in-time recovery: "restore the database as it was at 14:32 yesterday, before the bad deploy" creates a new instance from the backups and logs. Practise a restore before you need one: a backup you've never restored is a hope, not a backup.
Scaling a relational database
- Vertically: a bigger instance class (a few minutes of downtime, or less with Multi-AZ).
- Read replicas: copies that serve read-only queries (reports, search), so the primary handles writes.
- Aurora (AWS) and AlloyDB (Google) are PostgreSQL/MySQL-compatible engines rebuilt for the cloud, with storage that grows automatically and fast replicas. Aurora Serverless scales capacity up and down with load.
- Connection pooling (RDS Proxy, PgBouncer) stops hundreds of app instances or Lambda functions from exhausting the database's connections.
DynamoDB: serverless NoSQL
DynamoDB is AWS's key-value and document database: no servers or instances, single-digit-millisecond responses at any scale, and pay-per-request pricing. The catch: you design the table around your access patterns, because queries go by key, not arbitrary SQL.
aws dynamodb create-table --table-name orders \
--attribute-definitions AttributeName=customer,AttributeType=S AttributeName=order_date,AttributeType=S \
--key-schema AttributeName=customer,KeyType=HASH AttributeName=order_date,KeyType=RANGE \
--billing-mode PAY_PER_REQUEST --query "TableDescription.[TableName, TableStatus, BillingModeSummary.BillingMode]" --output text
put() { aws dynamodb put-item --table-name orders --item "$1"; }
put '{"customer": {"S": "asha"}, "order_date": {"S": "2026-11-01"}, "total": {"N": "1299"}, "status": {"S": "shipped"}}'
put '{"customer": {"S": "asha"}, "order_date": {"S": "2026-11-20"}, "total": {"N": "450"}, "status": {"S": "pending"}}'
put '{"customer": {"S": "ravi"}, "order_date": {"S": "2026-11-15"}, "total": {"N": "999"}, "status": {"S": "shipped"}}'
echo "--- one item by its full key:"
aws dynamodb get-item --table-name orders --key '{"customer": {"S": "asha"}, "order_date": {"S": "2026-11-20"}}' \
--query "Item.[status.S, total.N]" --output text
echo "--- all of asha's orders from November 10 on (a Query uses the key):"
aws dynamodb query --table-name orders --key-condition-expression "customer = :c AND order_date >= :d" \
--expression-attribute-values '{":c": {"S": "asha"}, ":d": {"S": "2026-11-10"}}' \
--query "Items[].[order_date.S, total.N]" --output text
echo "--- update with a condition (only if still pending):"
aws dynamodb update-item --table-name orders --key '{"customer": {"S": "asha"}, "order_date": {"S": "2026-11-20"}}' \
--update-expression "SET #s = :new" --condition-expression "#s = :old" \
--expression-attribute-names '{"#s": "status"}' --expression-attribute-values '{":new": {"S": "shipped"}, ":old": {"S": "pending"}}' \
--return-values UPDATED_NEW --query "Attributes.status.S" --output text
aws dynamodb update-item --table-name orders --key '{"customer": {"S": "asha"}, "order_date": {"S": "2026-11-20"}}' \
--update-expression "SET #s = :new" --condition-expression "#s = :old" \
--expression-attribute-names '{"#s": "status"}' --expression-attribute-values '{":new": {"S": "cancelled"}, ":old": {"S": "pending"}}' 2>&1 | sed 's/.*(ConditionalCheckFailedException).*/ConditionalCheckFailedException: the order is no longer pending/'orders ACTIVE PAY_PER_REQUEST --- one item by its full key: pending 450 --- all of asha's orders from November 10 on (a Query uses the key): 2026-11-20 450 --- update with a condition (only if still pending): shipped ConditionalCheckFailedException: the order is no longer pending
| Concept | Meaning |
|---|---|
Partition key (customer) |
Decides where data is stored; every query must specify it |
Sort key (order_date) |
Orders items within a partition; allows ranges (>=, begins_with) |
| Query | Fast: items with one partition key, filtered by the sort key |
| Scan | Reads the whole table: slow and expensive at scale, avoid in hot paths |
| Global secondary index | An extra key, for another access pattern (e.g. by status) |
| Conditional writes | Safe concurrent updates without locks |
Use DynamoDB (or Firestore, Cosmos DB) when access patterns are known and simple, and scale or zero-maintenance matters most. Use PostgreSQL when you need joins, ad-hoc queries and transactions across many tables, which is most applications. The MongoDB and Redis courses on this site cover the same ideas in detail.
Security checklist for databases
- Private subnets only, with a security group that allows the database port only from the app's security group (see Networking).
- Encryption at rest (a checkbox, on by default for new instances on most services) and TLS in transit (
sslmode=require/verify-full). - Credentials in a secrets manager, rotated automatically, or IAM database authentication. Never in code.
- Deletion protection on production databases, and backups kept long enough for your needs.
- Separate database users for the app (limited rights) and for migrations/admin.
Clean up
aws rds delete-db-instance --db-instance-identifier shop-db --skip-final-snapshot --query "DBInstance.DBInstanceStatus" --output text
aws dynamodb delete-table --table-name orders > /dev/null && echo "table orders deleted"deleting table orders deleted
On production databases, don't use --skip-final-snapshot: the final snapshot is your last backup.
Practice
- Choose a database service for: an online shop's orders, a mobile game's player profiles at huge scale, and analytics over 5 years of sales.
- Design a DynamoDB key for "all messages in a chat room, newest first".
- Write the security group rule that lets only
app-sgreach a PostgreSQL database. - Explain point-in-time recovery to a non-technical colleague.
1.What does a Multi-AZ database deployment give you?
2.In DynamoDB, what must every Query specify?
3.Where should a production database run?
4.What's a safe way to store the database password?