Build a Tiny CPaaS · Chapter 3 · 12 min
Billing events and meters
Emit immutable usage events, aggregate them into meters and reconcile with carrier invoices.
Event-sourced usage tracking
Accurate billing in a CPaaS requires a shift from state-based records to an event-sourced architecture. Every billable action—whether it is an SMS segment, a voice minute, or a phone number rental—emits an immutable usage event. These events contain all the data needed to calculate the price, such as the tenant ID, the region, and the specific unit of measure. By storing these events in an append-only log, we create an audit trail that can never be altered or deleted.
This approach prevents the common "billing drift" that occurs when prices are calculated by querying mutable message tables. Since prices can change over time, the price at the moment of the transaction must be captured within the event itself. The ub3r billing system uses these events as the primary source of truth for all financial operations. This ensures that every cent charged to a customer can be traced back to a specific, immutable event in our log.
Metering and aggregation
Meters are responsible for aggregating the stream of usage events into human-readable balances and invoices. They process events in real-time or in batches, updating a tenant's consumed quota for the current billing period. Because the underlying events are immutable, any corrections are handled by issuing a new "reversal" event rather than updating existing records. This "no-updates" policy makes the entire billing pipeline easier to reason about and debug.
The ub3r platform supports several metering models, including pre-paid balances and post-paid monthly commitments. Our aggregation engine is designed to handle millions of events per hour with high precision, ensuring that customers are billed accurately down to the millisecond. This scalability is achieved by using distributed stream processing tools that can recover gracefully from failures. Reliable metering is the difference between a project and a sustainable business.
interface UsageEvent {
eventId: string;
tenantId: string;
type: "sms_segment" | "voice_minute";
quantity: number;
unitPrice: number;
timestamp: number;
}Financial reconciliation
Reconciliation is the process of comparing our internal records with the invoices we receive from our carrier partners. Discrepancies are common and can be caused by lost delivery reports, misconfigured pricing, or carrier errors. By comparing our event log with the carrier's CDRs (Call Detail Records), we can identify these issues and claim credits where appropriate. This rigorous process protects our margins and ensures we are only paying for the service we actually received.
We also provide our customers with their own reconciliation tools, allowing them to verify their invoices against their own logs. Transparency in billing builds trust and reduces the number of support tickets related to "mysterious" charges. The ub3r platform aims to be the most transparent communications provider in the industry by giving users full access to their usage data. Financial integrity is a core value that permeates every layer of our engineering stack.
Discussion (3)
The distinction between 'sent' and 'delivered' finally clicked for me here. We had dashboards treating them as the same thing for a year.
Good point — we're adding a short section on this in the next revision.
Would love a follow-up on how this behaves under carrier throttling. We see very different numbers per route.