← Engineering Dispatches / Architecture
Usage-Based Metered Billing Engine Architecture: Scaling Real-Time Event Ingestion Without Revenue Leaks
How modern B2B SaaS platforms replace rigid monthly subscriptions with real-time usage metering using Kafka, ClickHouse, and Stripe Billing webhooks.
Hammad Haider
Principal Engineer & Product Architect · Oct 31, 2026 · 14 min read read
Architectural Takeaways
- Usage-based billing requires a decoupled three-stage architecture: High-Throughput Event Ingestion, Idempotent Aggregation (ClickHouse/Redis), and Financial Settlement (Stripe/Ledger).
- Every billable event must contain a unique idempotency key to prevent double-charging caused by network retries or distributed queue replays.
- Pre-aggregating event metrics into tumbling time windows cuts database storage overhead by over 90% while providing instant customer usage dashboards.
- Triamorph Systems engineers custom financial systems and metered billing pipelines with zero revenue leakage and automated ledger reconciliation.
Table of Contents
1. The Death of Flat-Rate Subscriptions: Why UBP Won
Aligning software revenue directly with customer value and resource consumption
In traditional software, human employees sat in chairs and clicked buttons. Charging per seat made intuitive sense: if a company had 50 customer support agents, they bought 50 Zendesk licenses. But what happens when an autonomous AI agent resolves 80% of those tickets without human intervention? The customer downsizes from 50 seats to 10 seats, and the software vendor’s revenue plummets by 80%—even though their software delivered 5x more value.
This fundamental misalignment forced the software industry to embrace Usage-Based Pricing. Companies like Snowflake, Twilio, Datadog, and OpenAI proved that usage-based models generate higher Net Revenue Retention (NRR) rates (frequently exceeding 130%) because customer spend naturally expands as their business scales.
However, the engineering challenge is profound. Instead of running a simple cron job on the 1st of every month that charges $500 to a credit card, your infrastructure must ingest 50,000 events per second, assign each event to a tenant, check real-time spending limits, calculate tier brackets, and generate accurate invoices with sub-penny precision.
Average NRR for top-performing usage-based SaaS companies
Sustained throughput on distributed Kafka metering clusters
Zero tolerance for duplicate or dropped billable customer events
- Value Alignment: Customers only pay when they extract real business value from your platform.
- Lower Adoption Friction: Allows new enterprise clients to start with modest pilot spends without massive upfront procurement approvals.
- Natural Expansion: As your clients grow their transaction volumes, your revenue scales automatically without awkward upsell calls.
- Margin Protection: Prevents power users from consuming massive cloud GPU or compute resources on an unmetered flat plan.
2. The 3-Tier Metering Architecture: Ingestion to Invoice
Separating high-speed ingest from durable financial settlement
Never attempt to write billable events directly to an ACID transactional SQL database like PostgreSQL on every incoming API request. Under peak traffic spikes, row-level locks and write contention will crash your database and bring down your entire application.
A production metering engine consists of three decoupled layers:
1. Ingestion Buffer: A lightweight HTTP proxy or edge worker receives billable events and immediately pushes them to an Apache Kafka or AWS Kinesis topic. The client receives a 202 Accepted response in under 5 milliseconds.
2. Aggregation Store: A column-oriented OLAP database (such as ClickHouse) or distributed Redis cluster consumes events from Kafka, dedupes them using UUID hashes, and rolls up raw events into hourly and daily customer buckets.
3. Settlement & Billing Engine: At the end of the billing cycle (or when a prepaid balance is depleted), an asynchronous scheduler reads the consolidated usage totals from ClickHouse, calculates tiered pricing formulas, and dispatches invoice line-items to Stripe Billing, Lago, or an internal ERP ledger.
Decoupling event capture from invoice calculation ensures that even if your payment processor experiences a 3-hour outage, zero customer usage events are lost.
— Triamorph Systems Distributed Architecture
3. Preventing Double-Billing: Idempotency & Deduping
Eliminating duplicate charges with Redis bloom filters and unique transaction keys
In distributed systems, the network is fundamentally unreliable. If a microservice reports that a customer consumed 150 AI tokens, but the acknowledgment packet is dropped by a transient network hiccup, the microservice will retry the request. If your metering engine is not strictly idempotent, the customer will be billed twice.
We enforce idempotency by requiring every billable event payload to include a deterministically generated `idempotency_key` (such as a hash of the tenant ID, timestamp, and unique request ID).
When the ingestion engine receives an event, it checks the key against a distributed Redis cluster using `SET key 1 NX EX 86400`. If Redis returns nil, the event has already been processed within the last 24 hours and is immediately discarded as a duplicate without incrementing the customer balance.
Redis idempotency gate prevents duplicate charges on network retries
In-memory key verification adds negligible delay to ingestion
Idempotency memory buffer shields against delayed replay queues
4. High-Concurrency Event Aggregation with ClickHouse
Using SummingMergeTree tables for blisteringly fast usage rollups
ClickHouse is the gold standard database for high-throughput metered billing. Its columnar storage format and `SummingMergeTree` table engine automatically sum numeric columns in the background, transforming billions of raw usage logs into fast, queryable customer summaries.
Below is a production ClickHouse schema and TypeScript event dispatcher that safely captures and aggregates billable API and compute events.
-- ClickHouse Metered Billing Schema with Automated Summing
CREATE TABLE IF NOT EXISTS metered_events (
tenant_id UUID,
meter_name LowCardinality(String),
event_time DateTime64(3, 'UTC'),
quantity Float64,
idempotency_key String
) ENGINE = ReplacingMergeTree(idempotency_key)
ORDER BY (tenant_id, meter_name, toStartOfHour(event_time), idempotency_key);
-- Materialized View for Sub-Millisecond Hourly Customer Rollups
CREATE TABLE IF NOT EXISTS hourly_customer_usage (
tenant_id UUID,
meter_name LowCardinality(String),
hour_bucket DateTime,
total_quantity Float64
) ENGINE = SummingMergeTree()
ORDER BY (tenant_id, meter_name, hour_bucket);
CREATE MATERIALIZED VIEW IF NOT EXISTS mv_hourly_usage TO hourly_customer_usage AS
SELECT
tenant_id,
meter_name,
toStartOfHour(event_time) AS hour_bucket,
sum(quantity) AS total_quantity
FROM metered_events
GROUP BY tenant_id, meter_name, hour_bucket;
5. Engineering Resilient Billing Systems with Triamorph
Bulletproof financial platforms built for enterprise scale
At Triamorph Systems, we engineer custom financial platforms, payment gateways, and high-concurrency business systems with cryptographic rigor. As showcased in our Operations Management Platform case study, we build software where financial accuracy is non-negotiable.
We design and deploy custom usage-based billing engines that connect directly to your application stack, whether you use Stripe Billing, Adyen, PayPal, or internal general ledgers.
Every solution is shipped with comprehensive automated end-to-end reconciliation testing, zero vendor lock-in, and full intellectual property transfer upon delivery.
Transitioning your SaaS product to usage-based or hybrid billing? Discover our Custom ERP & Business Systems or reach out to our principal software architects.
— Triamorph Financial Engineering Practice
Read more technical guides and articles on our Articles & Guides Index →