Home›Operate›Why the bill is huge
Decision 10 · Cost model

Why is my database bill huge?

What am I paying for that the workload does not need?

Most database bills are large for the same few reasons. This page lists them, shows how to find each one with a query, and gives a safe order for cutting. The bill is a result of the design, so the design is where the savings are.

The decision

Pay for the load you have, plus a reserve you chose on purpose

The decision is not "spend less". It is to know what each line of the bill buys, and to size the reserve deliberately, not out of fear. A database that is too small fails loudly. A database that is too big fails quietly, on the invoice.

Where the money goes

Six lines on the invoice

Where your bill goes

The starting numbers are an example, not a benchmark. Replace them with the lines from your own monthly invoice. The largest line is drawn in orange.

    LineWhat it buysWhat usually inflates it
    ComputeCPU and memory for the primary and every replicaSized for the fear. Extra replicas. Connections held open.
    StorageTable and index files, logs, undoOld data, unused indexes, bloat, long transactions.
    I/OProvisioned IOPS and throughput, or pay-per-requestScans that should be lookups. A cache that is too small.
    BackupsSnapshots and archived logs for point-in-time recoveryRetention nobody reviewed. Copies kept in several regions.
    NetworkTraffic between zones, regions and the internetReplicas in other regions. Applications in another zone than the primary.
    Everything that is not productionDev, test, staging and forgotten copiesFull-size copies that run all night and all weekend.
    The six patterns

    Find each one with a query

    The commands below are the standard ones for each engine. Run them on a replica when you can, and read the numbers over a full business cycle, not one quiet afternoon.

    1. An instance sized for the fear

    Someone picked the size for the worst day they could imagine, and nobody went back to check.

    How to find itLook at 30 days of CPU at the 95th percentile and the cache hit ratio. If CPU is under about 25 percent and the working set fits in half the memory, there is room.
    -- PostgreSQL cache hit ratio
    SELECT sum(blks_hit)::float / nullif(sum(blks_hit) + sum(blks_read), 0)
    FROM pg_stat_database;

    The fixStep down one size, not three. Keep enough room for the failover case. See "What breaks when you cut too hard" below.

    2. IOPS and throughput nobody uses

    Provisioned I/O is billed whether the disk uses it or not.

    How to find itCompare the provisioned number with the 99th percentile of what the monitoring shows you used. Check the same figure for a month that includes your peak.
    -- MySQL: how often the buffer pool misses
    SHOW GLOBAL STATUS LIKE 'Innodb_buffer_pool_read%';

    The fixLower the provisioned figure to the peak plus 30 percent. Raise it again when the peak grows.

    3. Replicas nobody reads

    Every replica is a full copy of the data at full price. A replica that serves no reads and is not your failover target is pure cost.

    How to find itLook at the connections and the query rate on each replica. Zero reads over a month is an answer.
    -- PostgreSQL, on the primary
    SELECT client_addr, state FROM pg_stat_replication;

    The fixRemove it, or keep exactly one replica as the failover target, sized like the primary.

    4. Indexes that nothing uses

    Each unused index costs disk, cache space and a write on every change to the table.

    How to find itAsk the engine which indexes have never been read since statistics were last reset. Check across a full business cycle, such as a month end. Never drop an index that backs a unique or primary key.
    -- PostgreSQL
    SELECT relname, indexrelname, idx_scan
    FROM pg_stat_user_indexes WHERE idx_scan = 0
    ORDER BY pg_relation_size(indexrelid) DESC LIMIT 20;
    -- MySQL
    SELECT * FROM sys.schema_unused_indexes;
    -- MongoDB
    db.orders.aggregate([{ $indexStats: {} }])

    The fixDrop them one at a time, with the definition saved so you can recreate it.

    5. Data that should have left

    Old rows, old logs and old events sit on fast, expensive storage and drag every scan and every backup with them.

    How to find itList the biggest tables and ask how old the oldest row is, and whether anyone reads it.
    -- PostgreSQL
    SELECT relname, pg_size_pretty(pg_total_relation_size(oid))
    FROM pg_class WHERE relkind = 'r'
    ORDER BY pg_total_relation_size(oid) DESC LIMIT 10;
    -- MongoDB
    db.events.totalSize()

    The fixPartition by time and drop whole partitions instead of running a huge DELETE. In MongoDB, use a TTL index. Archive what the law requires you to keep.

    6. Connections forcing a bigger box

    PostgreSQL runs one process per connection. A thousand mostly idle connections cost memory that is then bought as a bigger instance.

    How to find itCompare the number of connections with the number doing work right now.
    SELECT state, count(*) FROM pg_stat_activity
    GROUP BY state ORDER BY 2 DESC;

    The fixPut a pooler in front, such as PgBouncer, and lower the number of server connections.

    The order to cut in

    Cheapest and safest first, commitments last

    Each step makes the next one safer. Committing to a one or three year price is last because it locks in a size, so it should lock in the size you reached after the other steps.

    1. Find the top queries by total timeCost is work. Work is pages read. Start with what the database spends its time on.
    2. Fix the queries and the indexesOne missing index can be the reason for a whole instance size. Remove the unused indexes at the same time.
    3. Pool the connectionsThis often frees memory that was being bought as instance size.
    4. Let old data leavePartition by time, drop partitions, use TTL indexes, archive the rest.
    5. Offload reads only if reads dominateA replica is a full price copy. Add one only if the primary is the bottleneck for reads.
    6. Rightsize the instance and the storage I/OWith the work reduced, step down one size at a time and watch.
    7. Then commit to reserved capacityThe size is now known, so the commitment is safe.
    Worked example, with stated assumptions, not measuredAssume a primary with 64 GB of memory and 16 vCPUs. The working set, the data touched in a normal week, is 18 GB. CPU at the 95th percentile is 22 percent, and there are 200 connections at about 10 MB each. Memory needed: 18 GB x 1.3 for headroom is about 23 GB, plus 2 GB for connections, plus 2 GB for the operating system, about 27 GB. The next size up from that is 32 GB. CPU: 22 percent of 16 vCPUs is about 3.5 vCPUs in use. On 8 vCPUs that is about 44 percent at the 95th percentile, which still leaves room. So the candidate is 32 GB and 8 vCPUs, half the current size, tried first on a replica or in staging. The numbers are an illustration of the method. Use your own working set and your own percentiles.
    What breaks when you cut too hard

    Six ways to save money and cause an outage

    How to operate it

    A monthly review that takes an hour

    1. CPU, memory and I/O at the 95th percentileCompare with the instance and the provisioned I/O. Write the numbers down so next month has something to compare with.
    2. The top ten queries by total timeDid a new one appear? Did one grow?
    3. Unused indexes, biggest firstDrop one or two, with the definition saved.
    4. The biggest tables and their oldest rowsIs the retention still right?
    5. Replicas and non-production environmentsIs each one used? Can the test copy sleep at night?
    6. Backup retention and copiesDoes it still match what is required, and no more?
    Tested onNothing yet. The behavior described follows the documentation of PostgreSQL 18, MySQL 8.4 and MongoDB 8.0, and the commands are the standard ones, but this page has not been run through the automated command check. The worked example uses stated assumptions, and none of its figures were measured. Prices are not quoted on purpose, because they change and differ by provider.
    Your turn

    The database bill doubled in a month and the traffic did not. Name the first three things you would check, in order.

    Write your answer down first. Then open mine.

    Show my answer

    1. Storage and backups: table and index growth (bloat counts), snapshot retention, and logs kept longer than anyone meant. In PostgreSQL, pg_total_relation_size on the largest tables; in the bill, storage and backup lines.

    2. I/O: a new report, export or missing index that scans a big table every few minutes. On I/O-billed storage, such as Aurora Standard, that shows up directly as cost.

    3. Data transfer: a new replica in another zone or region, or an application now reading across zones. Replication traffic is billed every day, quietly.

    Then instance size changes. Cost Explorer grouped by usage type usually points at one of these in a few minutes.

    Connected

    Where this shows up

    The same mechanism, sharding and partitioning, caches and memory, elsewhere on the site: in real incidents, in measurements, and in designs.

    Part 4 of 4 · Operate · 15 pagesAll pages

    How do I run it in production? What to check when it is slow or broken, where it runs and what it costs, how big it must be, pooling, backups, security, upgrades and other changes made safely.