Lab 16: Database Cost Optimization

Time: 50 minutes | Level: Architect | DB: AWS RDS, PostgreSQL, MySQL, DynamoDB

Cloud database costs can spiral quickly without deliberate architecture decisions. This lab covers instance right-sizing, storage selection, compression, cold data archiving, and DynamoDB cost modeling with concrete ROI calculations.


Step 1: Understanding Cloud Database Pricing Models

Cloud database pricing has three main levers: compute, storage, and I/O.

┌─────────────────────────────────────────────────────────────┐
│              CLOUD DB COST COMPONENTS                        │
├──────────────────┬──────────────────────────────────────────┤
│ COMPUTE          │ Instance type × hours × Multi-AZ factor  │
│ STORAGE          │ GB/month × storage type (gp2/gp3/io1)    │
│ I/O              │ IOPS provisioned (io1) or request count  │
│ TRANSFER         │ Egress GB (reads to application servers) │
│ BACKUP           │ Automated backup storage beyond 1×DB size│
│ LICENSING        │ Oracle/SQL Server license costs          │
└──────────────────┴──────────────────────────────────────────┘

Purchase model comparison:

Model
Discount
Commitment
Best For

On-Demand

0%

None

Dev/test, variable workloads

1yr Reserved (No Upfront)

~35%

1 year

Steady production workloads

1yr Reserved (All Upfront)

~42%

1 year

Predictable, cash available

3yr Reserved (All Upfront)

~60%

3 years

Long-running stable systems

Savings Plans

~30-45%

1-3 years

Flexible instance family

💡 Reserved instances don't lock you to a specific instance — you can modify the instance type within a family (e.g., r6g.large → r6g.xlarge) with no cost penalty.


Step 2: RDS Instance Right-Sizing

The most common cost mistake is over-provisioning. Use CloudWatch metrics to find the right size.

Right-sizing decision tree:


Step 3: Storage Type Selection & Migration

Storage type is often the easiest cost win with no downtime required.

When to use io1/io2:


Step 4: Data Compression

Compression reduces storage costs and often improves performance by reducing I/O.

MySQL InnoDB compression:

PostgreSQL TOAST compression:

💡 PostgreSQL 14+ introduced LZ4 and ZSTD compression for TOAST. LZ4 is ~3x faster to compress than pglz with similar ratios — switch to it for high-write tables.


Step 5: Cold Data Archiving

Move infrequently-accessed data to cheaper storage tiers.


Step 6: DynamoDB Cost Modeling

DynamoDB's capacity units (RCU/WCU) model requires careful planning.

DynamoDB cost optimization strategies:

💡 DynamoDB On-Demand can cost 5-10x more than Provisioned at high traffic. Always benchmark your peak traffic and add 20% headroom for Provisioned mode.


Step 7: Read Replica Cost-Benefit Analysis

Read replicas cost money but can save more by allowing smaller primary instances.


Step 8: Capstone — Cost Calculator & ROI Report

Run verification:

📸 Verified Output:


Summary

Optimization
Typical Savings
Effort
Risk

gp2 → gp3 storage migration

20% storage cost

Low

None (zero downtime)

On-demand → 3yr Reserved

60% compute cost

Low

Medium (commitment)

Right-size over-provisioned instance

30-50% compute

Medium

Low (with testing)

Add read replica for analytics

Enables smaller primary

Medium

Low

InnoDB/TOAST compression

40-70% storage

Medium

Low

Cold data archiving to S3

75-96% storage

High

Low (with testing)

DynamoDB: On-Demand → Provisioned

80-90% DynamoDB

Low

Low (with monitoring)

Add PgBouncer (vs bigger instance)

$500-2000/month

Medium

Low

Last updated