Video in progress

How do you plan database resources for the coming year (capacity planning)?

I tie the current indicators (volume growth, TPS, CPU, IOPS, connections, hot data size) to business metrics and extrapolate from the business forecast. For each resource I calculate a "when it runs out" date, and put together a plan of measures several months before it.

📖 Answer

Planning without numbers is guessing. So I measure first:

  • Volume: how much the tables and indexes grow per month (the history of pg_total_relation_size). Also the size of WAL and backups.
  • The hot data (working set): the part actively read in the last 30 days. If it outgrows RAM, latency jumps sharply, and that is usually the first "wall".
  • Load: TPS, the number of the heaviest queries, CPU p95, IOPS and throughput, pg_stat_bgwriter/pg_stat_io (PG 16+).
  • Connections and lock waits.

Then I tie them to a business unit: "1000 active users = 4 TPS and 300 MB a day". If the product team says "next year users triple", it gives a linear calculation; but some resources aren't linear (lock contention, for example).

The result looks like a table: "disk 80% in 7 months, RAM working set in 5 months, CPU in 10 months". A plan for each:

  • disk: archiving/partition retention, or a bigger volume;
  • RAM: a bigger instance, dropping unneeded indexes;
  • CPU: heavy queries to a replica, caching, then vertical scaling;
  • if all that isn't enough: functional separation or sharding (the most expensive and slowest, so it starts 6-12 months ahead).

I confirm it with a load test: on a prod-sized copy, at the expected 2-3x load. And I review the plan every quarter: the forecast almost never turns out right, but the trend is visible.