Skip to main content

The GOV.UK PaaS billing system

The GOV.UK Platform as a Service (PaaS) billing system generates bills for GOV.UK PaaS tenants. Our tenants use GOV.UK PaaS to create and delete various services and resources, such as compute instances running applications and databases. We need to calculate how much these services and resources cost on a monthly basis so we can recoup this money from tenants.

The GOV.UK PaaS billing system comprises:

  • 2 applications written in Golang: the paas-billing-collector and the paas-billing-api, both sharing the paas-billing code base
  • the GOV.UK PaaS billing Postgres database application, which uses stored functions to calculate tenants’ bills in the database

Accessing GOV.UK PaaS billing

To access GOV.UK PaaS billing information, use the PaaS Admin web application through the Explore your costs and usage link on your organisation’s page. You can also access more detailed information, such as resource IDs and charging plan details, through the PaaS Billing API.

PaaS Admin calls the \billable_events endpoint of the API. The API then queries the billing database to get the required data and calls a stored function. For example, the get_tenant_bill stored function is the database entry point for calculating tenant bills.

How GOV.UK PaaS billing works

We use database SQL code to calculate tenants’ bills. There are 2 parts to the procedure for calculating bills:

  1. Assembling what needs to be billed

    We put together a set of billable resources and services and a time frame across which to bill them. The get_tenant_bill_api stored function calculates bills and is invoked through the API. Information is collated from the event-sourced Cloud Foundry usage events for the queried time frame and written to a temporary database table by a stored function.

    This stored function takes an organisation name as well as the start and end date of the queried time frame as arguments. It then goes through the resources table for the tenant and time frame, collects records of which services or resources the tenants used, and writes them to a temporary table.

  2. Calculating the bill

    The calculate_bill stored function calculates the bill. This function accepts no parameters. It takes the contents of the temporary table populated by get_tenant_bill_api and calculates the bill, taking account of factors such as VAT rates and currency exchange rates. The exchange rates are important because many services provisioned by GOV.UK PaaS are billed in US dollars (USD), but we produce bills for our tenants in British pounds (GBP).

How we calculate bills

The PaaS billing system calculates tenants’ costs, based on pricing information collected from our service providers. We do this by writing simple formulae in the billing database, and each service or resource we provide to tenants has a formula to calculate the cost. An example formula is:

($storage_in_mb/1024) * ($time_in_seconds/2678401) * $external_price

This formula calculates the charge using the storage (in GB) * an external_price (typically in USD) charged by our external service provider * proportion of the month the tenant has provisioned the resource (number of seconds in a month is taken as 2678401, assuming a 31 day month).

We calculate cost for all resources accurate to the second.