· Cloud

What serverless databases actually charge for

A serverless database bills for the hours its compute is awake and for the storage it holds, hour by hour. What keeps the compute awake, what the storage meter counts and how to read your own usage.

A serverless database charges for two things: the time its compute is awake, and the storage it holds. The number of queries hardly comes into it. If you arrive from serverless functions, where you pay per request, that habit of thought will give you the wrong estimate.

I run several small projects on a serverless Postgres service, so I have spent more time reading usage pages than I planned to. Prices change often and the mechanisms change much less, so the mechanisms are the part to learn.

The compute meter runs on a clock

The unit is a size multiplied by time. Neon, one of the serverless Postgres providers, bills compute in CU-hours and defines the unit as "1 CU running for 1 hour," where a CU is a fixed bundle of CPU and memory. Amazon Aurora's serverless option measures capacity in units it calls ACUs and bills for them over time in the same way.

Both can scale down to nothing when the database is idle. Neon's documentation says a compute "automatically scales to zero after 5 minutes" of inactivity. On Aurora you can set the minimum capacity to 0 ACUs, and you choose how long the instance must be idle before it pauses. The default is 300 seconds, which is also the shortest setting allowed, and the longest is 86,400 seconds.

So the bill for a quiet database depends on how many hours it was awake, and that depends on what "idle" means. For both services it means, roughly, that nothing is connected and nothing is running.

The arithmetic is worth doing once. Suppose a scheduled job runs one query every hour, and the query takes a second. Each run wakes the database. After the query finishes, the database stays up for the idle window before it suspends. With a five-minute window that is about five minutes of billed compute per run, 24 times a day. You pay for two hours a day to do 24 seconds of work.

Now run that job every minute. The database never reaches five idle minutes, so it never suspends. You pay for 24 hours a day, every day, and the query still takes a second.

Billed compute per day for one small scheduled job
hour
Every minute24 hours
Every 5 minutes24 hours
Every 10 minutes12 hours
Every 15 minutes8 hours
Every 30 minutes4 hours
Every hour2 hours
Every 3 hours40 minutes
Every 6 hours20 minutes
Every day5 minutes

A job that runs every hour keeps the database billed for 2 hours a day to do 24 seconds of work.

Assumes a five-minute idle window and a query that takes one second, as in this post.

What keeps a database awake

Anything that touches the database more often than the idle window will hold it up around the clock. The usual causes are ordinary things.

  • Scheduled jobs. A job that polls every minute or every few minutes is enough. Neon's cost guidance names scheduled jobs among the things that prevent a compute from scaling to zero, "keeping it active 24/7."
  • Open connections. AWS states that an Aurora instance does not pause while any user connection is open. A connection pool that holds a few connections for speed, or an admin tool left open in a browser tab, counts.
  • Proxies and replication. AWS lists its connection proxy and logical replication among the things that prevent pausing. Neon says the same about logical replication subscribers.
  • Health checks. An uptime monitor that calls an endpoint, where the endpoint runs a query to prove the database is reachable, wakes the database on every check.
  • Traffic nobody invited. If a public page queries the database when it renders, every crawler that requests the page wakes the database. Crawlers work through the night, so the database does too.
  • Read replicas. On Neon a read replica is billed as its own compute, with its own hours.

None of these does much work, and that is why the bill is confusing. The database looks idle by every measure except the one being billed.

One detail on scheduling runs the other way. AWS notes that jobs scheduled inside the database with the pg_cron extension do not wake a paused Aurora instance. If a nightly cleanup depends on that scheduler, check that it still runs once pausing is turned on.

What waking up costs

Scale to zero is a trade between money and the first request after a quiet period. The size of the trade depends on the service. Neon says a suspended compute reactivates "within a few hundred milliseconds." AWS says an Aurora instance takes approximately 15 seconds to resume, and longer, 30 seconds or more, if it has been paused for more than 24 hours.

A few hundred milliseconds is invisible on an internal tool and noticeable on a checkout page. Fifteen seconds will trip most web request timeouts unless the application is written to wait and retry.

Whether to accept that delay is the application owner's decision. It should be made per database. A reporting tool used on weekday mornings can sleep the rest of the week. A customer-facing database probably should stay up, and then you are paying for a small always-on database and should compare it with the price of one.

The storage meter never sleeps

Storage is billed whether the compute is awake or not. AWS says so directly for a paused Aurora instance: the instance charge stops and the storage charge continues. Neon's description of its own metering is "We meter storage hourly, then sum it across the month."

Two parts of the storage bill are easy to miss.

The first is history. To offer point-in-time restore, the service keeps a record of every change for a period you choose. Neon bills that retained history separately from the data itself. A database that is small and busy, with rows updated all day, can hold more history than data. The retention period is a setting, and a longer period costs more.

The second is branches. A branch is a copy of the database for development or testing, and it starts out sharing its parent's data. Neon's wording is that for child branches "only the delta" counts toward storage. A new branch is close to free, then. A branch that someone created in March and forgot about has been collecting its own changes, and its own history, ever since. Branches beyond a plan's included number are also metered by the hour.

Reading your own usage

Open the usage view and look at compute hours per database for one week, before you look at any dollar figure. A week has 168 hours. A database showing a number near 168 never slept. If you believed it was idle, something on the list above is talking to it.

To find out what, look at the connections. Postgres will show you who is connected and what each session last ran. Work through them one at a time:

  1. Find every scheduled job that touches the database and write down its interval. Anything shorter than the idle window keeps the database up by itself.
  2. Check whether the application's connection pool closes idle connections, and after how long.
  3. Check what your health checks do. A check that proves the web server is alive does not need to run a query.
  4. Look at which public pages query the database on every request, and whether those pages could be cached.
  5. List the branches and delete the ones nobody is using.
  6. Read the history retention setting and decide whether you need that many days.

Then look at the week again.

The ownership splits cleanly. The service owns the meter and publishes how it works. The application team owns everything on that list of six, because each item is a choice in application code or configuration. Whoever holds the account owns the three settings that cap the bill: the idle window, the maximum compute size and the history retention.

A limit on all of this. I have described two services from their public documentation as it stands this spring. Plans, units and defaults change, and some features depend on the plan you are on. Read the current pricing page for your provider before you act on any of it, and trust your own usage view over anyone's description.

For a small project, the cheapest improvement I know of is to change a one-minute polling job to run every fifteen minutes, or to replace it with something that runs only when there is work to do. It costs an afternoon, and the usage view will show within a week whether it worked.

Views are my own.