Database Services
Use AWS managed database services — RDS for relational databases, DynamoDB for NoSQL, ElastiCache for caching, and backup strategies.
Amazon RDS
RDS manages relational databases: PostgreSQL, MySQL, MariaDB, Oracle, and SQL Server. AWS handles provisioning, patching, backups, and failover. Multi-AZ deployments provide automatic failover for high availability.
Read Replicas scale read traffic and serve as failover targets. Automated backups with point-in-time recovery protect against data loss.
- Multi-AZ synchronously replicates to standby in another AZ
- Read Replicas are asynchronous — eventual consistency
- Aurora is AWS cloud-native database with 5x PostgreSQL performance
# Create PostgreSQL RDS instance aws rds create-db-instance \ --db-instance-identifier mydb \ --db-instance-class db.t3.micro \ --engine postgres \ --engine-version 16.1 \ --master-username admin \ --master-user-password secret \ --allocated-storage 20 \ --multi-az \ --backup-retention-period 7
Amazon DynamoDB
DynamoDB is a fully managed NoSQL key-value and document database. Single-digit millisecond latency at any scale. Pay-per-request or provisioned capacity pricing.
Design around access patterns — define partition key and optional sort key. Use Global Secondary Indexes (GSI) for alternate query patterns. DynamoDB Streams capture changes for event-driven processing.
# Create DynamoDB table
aws dynamodb create-table \
--table-name Users \
--attribute-definitions \
AttributeName=userId,AttributeType=S \
--key-schema AttributeName=userId,KeyType=HASH \
--billing-mode PAY_PER_REQUEST
# Query
aws dynamodb get-item \
--table-name Users \
--key '{"userId": {"S": "user-123"}}'ElastiCache
ElastiCache provides managed Redis and Memcached for in-memory caching. Reduce database load and improve response times by caching frequently accessed data.
Redis supports persistence, replication, and complex data structures. Memcached is simpler with multi-threaded performance. Use Redis for session storage, leaderboards, and pub/sub.
# Redis connection from Node.js
import { createClient } from 'redis';
const client = createClient({
url: 'redis://my-cluster.cache.amazonaws.com:6379',
});
await client.connect();
await client.set('session:abc', JSON.stringify(data), { EX: 3600 });Backup and Recovery
RDS automated backups capture daily snapshots and transaction logs for point-in-time recovery. Retention period: 1–35 days. Manual snapshots persist until deleted.
DynamoDB Point-in-Time Recovery (PITR) enables restoration to any second within the last 35 days. Enable on-demand backups before major schema changes.
# RDS point-in-time restore aws rds restore-db-instance-to-point-in-time \ --source-db-instance-identifier mydb \ --target-db-instance-identifier mydb-restored \ --restore-time 2024-01-15T10:00:00Z
Database Selection Guide
RDS/Aurora for relational data with complex queries and transactions. DynamoDB for key-value access patterns at massive scale. ElastiCache for caching layers. DocumentDB for MongoDB-compatible workloads. Redshift for data warehousing.
Prefer managed services over self-managed databases on EC2. Managed services handle patching, backups, and failover automatically.
# Database selection # PostgreSQL/MySQL → RDS or Aurora # Key-value, high scale → DynamoDB # Caching → ElastiCache (Redis/Memcached) # MongoDB → DocumentDB # Data warehouse → Redshift # Time series → Timestream