Cloudflare's architecture for their Spend Management system utilizes a two-stage metering pipeline, account-partitioned processing with Write-Ahead Logging (WAL), an independent Rating Engine, and asynchronous cap enforcement via a global key-value store to handle cost controls for millions of developers.
Read original on Cloudflare BlogBuilding a global spend control system requires balancing data ingestion scale, processing latency, and eventual enforcement consistency. Cloudflare's Spend Management architecture decouples high-frequency telemetry collection from the critical request path, ensuring that enforcement checks never introduce latency into user workloads.
Transporting and processing raw usage events directly from the edge to a rating engine would overwhelm downstream infrastructure. To solve this, Cloudflare implements a two-stage aggregation pipeline close to the source:
The Usage Engine partitions workloads by account, allowing an account's usage metrics and subscription details to be processed together without cross-partition coordination. Each partition maintains local state alongside a durable Write-Ahead Log (WAL) on disk, guaranteeing recoverability and independent parallel scaling.
Handling Independent Data Streams
Usage records and subscription updates arrive via independent asynchronous streams. If usage arrives before subscription context, the Rating Engine retains it as pending rather than prematurely pricing it as free, ensuring robust fault tolerance against webhook failures or delivery delays.
To protect request latency, the Rating Engine stays completely out of the hot request path. Once a spending cap or threshold breach is calculated, the policy service pushes the update to Quicksilver, Cloudflare's distributed key-value store. Edge runtimes periodically poll this local state to reject over-budget invocations asynchronously.
Want the fundamentals behind stories like this? Take the free System Design Academy course with certificate.