Command Palette

Search for a command to run...

Hectal
PHASE 15Advanced ~8 min· topic 5 of 5

Topic 15.5

Database Cost Engineering

In one line

Database cost is compute, storage, IOPS, backups, replicas, cross-region and data-transfer charges, caches and monitoring. Reduce it by fixing queries before buying hardware, right-sizing, tiering cold data, using reserved or committed pricing, and choosing managed versus self-hosted deliberately.

0/5 · 0%

Think of it like this

Household bills. Before buying a bigger water tank, fix the leaking tap (slow queries); pay a yearly plan instead of monthly (reserved instances); move rarely used items to a cheap storage unit (cold tier).

Key ideas

  1. 01

    Compute: the largest line item. A single index that removes a sequential scan can let you halve the instance size. Right-size from actual CPU and memory use; reserved or committed-use pricing saves ~30–60% for steady workloads.

  2. 02

    Storage and IOPS: provisioned IOPS volumes are expensive; gp3-style volumes let you buy IOPS separately. Bloat and unused indexes cost real money at TB scale.

  3. 03

    Replicas: each is a full copy (compute + storage). Use them for availability and read scaling you need, not by habit; a cache can be cheaper than two more replicas for read-heavy keys.

  4. 04

    Cross-region: replication and cross-AZ traffic are billed per GB; multi-region architectures can spend more on transfer than on instances. Compress, filter what you replicate, and keep chatty services in one zone where availability allows.

  5. 05

    Managed vs self-hosted: managed (RDS, Aurora, Cloud SQL, Atlas) costs more per unit and saves operations engineering (backups, failover, patching). Self-hosting pays off only at scale with a capable team. Decide with total cost of ownership, including people.

Code & diagrams

cost-comparison.txttext
Monthly cost sketch (illustrative on-demand prices; check your provider)
Option A: 1 x 32 vCPU primary + 2 x 32 vCPU replicas, 4 TB SSD each      ~ 3 x ($2,300 + $400) = $8,100
Option B: fix top 3 queries + 2 indexes -> 16 vCPU primary + 1 replica
          + 2-node Redis cache for product reads                          ~ $1,150x2 + $800 + $600  = $3,700
Option C: B + 1-year reserved pricing (~35% off compute)                 ~ $2,700
Plus: backups (35 days, ~1.5x DB size in object storage), cross-AZ replication traffic, monitoring.

Interview problem

The problem

Cut the database bill by 40% without reducing reliability

A company spends $60K/month on databases: oversized primaries, 4 replicas per cluster, 3 years of logs in PostgreSQL, provisioned IOPS everywhere, and cross-region replication of everything. Produce a plan.

When it breaks

Cutting replicas without checking read traffic

What you see

The remaining replica saturates at peak; reads fall back to the primary, which then slows writes too.

Fix & prevent

Measure per-replica load at peak, add caching first, and remove replicas one at a time while watching latency.

Explain it without notes

01

Why do query fixes usually save more money than any pricing trick?

Practice

01

List the trade-offs of adding more cache vs a larger database instance for read latency.

Trade-offs

  • ↔

    Every saving changes risk or complexity; keep reliability targets explicit while optimising cost.

Done when you can

  • I can break down database costs and propose savings that keep reliability targets.