Skip to content

Metering usage without charging twice: inside Compute metering

Founder, AIUBY
Illustration: usage readings in sequence, with the repeated reading diverted and blocked before the record

Cloud Run metrics + PostgreSQL

On this page
  1. Where the number comes from
  2. Delay and periods
  3. Why a repeat does not charge twice
  4. The math lives in the database
  5. Shutdown on balance and cap
  6. When metering fails
  7. Limits

Paid CPU deployments in AIUBY Compute charge for time in use. For that to work, something has to say how much time it was, and that something will fail, restart and repeat work. This post shows how metering was designed so that a repeat never becomes a double charge, and where it accepts imprecision on purpose.

#Where the number comes from

We do not measure inside the customer's container and we do not count requests ourselves. We read two series from Cloud Run's own metrics, filtered by service name and region:

  • Billable instance time. The same number the cloud uses to bill AIUBY. Since a CPU application only has CPU allocated while it is serving requests, it adds no time while idle.
  • Bytes sent. The service's network egress for the period.

#Delay and periods

Cloud metrics do not arrive in real time. So metering only looks at data that is at least 5 minutes old. A timer inside the app runs every 10 minutes and, for each paid deployment with usage not yet billed, does the following:

  1. Asks the database where to resume

    Each deployment stores the point in time up to which its usage has been metered. The new period starts exactly there.

  2. Reads metrics for a period of up to 1 hour

    If metering has fallen behind, it does not try to bill everything in one block: it advances in periods of at most 1 hour, up to four per pass. The rest goes into the following passes.

  3. Hands the database only what it measured

    Deployment ID, start, end, billable seconds and bytes sent. No monetary amount.

This path explains the notice on the Compute Billing screen: usage shows up on the statement a few minutes late. When the application is stopped, metering waits out the same delay and settles the account up to the time it stopped.

#Why a repeat does not charge twice

The timer may run twice in a row, the app may restart in the middle of a pass, a call may be resent after a network error. The defense is two database rules:

  • The period must continue from where metering stopped. The function that records usage compares the given start with the already-metered point in time for that deployment. If they differ, it rejects with an error. An old period submitted again does not get in; neither does a period with a gap.
  • A unique constraint on deployment and period start. The statement table does not accept two rows with the same deployment and the same start. Even if the first rule had a bug, the second row would not be written.

Writing the statement row, debiting the balance and advancing the "metered up to" marker happen in the same transaction, with the deployment row locked. Either all three happen, or none does.

The automated test covers this directly: it meters two periods, runs the pass again and checks that nothing extra was charged; then it submits an already-metered period to the database again and expects the rejection.

#The math lives in the database

The application reports what it measured. Turning that into money is the job of a PostgreSQL function, based on the size stored on the deployment. It does four things:

  • Caps the seconds at the period duration times the application's replicas. With one replica, one wall-clock hour cannot have more than one hour of usage; with three, no more than three. An absurd value coming from the metric is cut before it becomes a charge.
  • Computes the cost of time: seconds times the size's hourly cost, with upward rounding.
  • Sums the organization's network egress for the month and charges only the part above the 10 GiB allowance. The month follows Brasília time, not the server's time zone.
  • Converts to reais at the account's rate, the same function used for AI consumption, always rounding up.

The statement is a table the application can only read: rows are created by this function.

#Shutdown on balance and cap

The same function that charges also answers whether the application needs to stop. After debiting, it checks the organization's prepaid balance and the workspace's Compute spend for the month against the cap. If the balance has reached zero or the cap has been hit, it returns the reason.

The timer then deletes the service in the cloud and ends the deployment with that reason, recording the event in the audit log. The stop uses the same global deployment lock. If the lock is held by someone deploying, the stop retries right away and, if it still cannot get it, the next pass takes care of it.

Usage up to the time of the stop is still metered and charged, once. After that, the deployment leaves the metering queue.

#When metering fails

If reading the metrics fails, the pass logs the error and moves on to the next application. Nothing is charged by estimate. On the next pass the period starts again from the same point, and the usage left behind is charged once the metrics come back.

There is a limit to this patience. If a live application goes 6 hours without metering, it is stopped, with a message that says exactly that. It is a trade-off in favor of a predictable bill and against availability, and it is worth knowing before you deploy.

#Limits

Limit

Shutdown happens during the metering pass. With the 10-minute interval and the 5-minute delay, the application can consume about 15 minutes of usage beyond the balance or cap before it is stopped.
  • Network egress has no cap of its own: it is limited by the workspace's balance and cap.
  • The balance used for shutdown does not subtract reservations for AI calls in progress on the same account.
  • The timer runs inside the app. With the app down, metering stops and resumes from where it left off when the app comes back. Usage for that interval is charged later, and shutdown is delayed.
  • We do not publish validation numbers against the real invoice: the cloud invoice closes late, and that comparison has not been done yet.

What this means for users, with sizes and balance rules, is in Paid CPU deployment in AIUBY Compute. The product reference is in Compute, in the documentation.

Biweekly digest

New posts and what reached the changelog, every two weeks. No other emails.