Design a Subscription Billing System
Understanding the Problem
🧾 What is a subscription billing system? A billing system decides what each customer owes and collects it. For subscriptions (a $20/month plan, an annual license) that means charging the right amount on the right date, handling upgrades, downgrades and cancellations mid-cycle, and chasing failed payments. For usage-based or pay-as-you-go pricing (per API call, per GB stored, per compute hour, as cloud providers and API companies charge) it also means counting every unit of usage accurately and pricing it. Stripe Billing, Chargebee, Zuora and the billing systems inside AWS or Twilio all solve this.
This question shows up in three flavors: "design recurring subscriptions like Netflix or Spotify billing," "design pay-as-you-go billing for a cloud or API product," and "design a cost explorer that shows customers what they've spent so far, accurately." They're one system. Subscriptions and usage both end up as lines on an invoice, and the cost explorer is a live preview of the next invoice. The interesting part is making that preview agree with the invoice.
Functional Requirements
Core Requirements
- Customers should be able to subscribe to a plan, change plans mid-cycle (with proration) and cancel.
- The system should bill each subscription automatically every cycle: create an invoice, charge the payment method, and handle failed payments.
- The system should meter usage events and bill them with usage-based pricing (per unit, tiered, volume).
- Customers should be able to see their costs so far in the current period, broken down by product and tag, and that number should match the final invoice.
Below the line (out of scope)
- Sales tax and VAT calculation (we'd call a tax service).
- Revenue recognition and accounting exports, beyond posting to the ledger.
- Quotes, contracts and sales-negotiated enterprise pricing.
- Free trials and coupons beyond a mention.
Non-Functional Requirements
Ask about scale. For our design: 5 million active subscriptions and, on the usage side, a cloud-style product whose services emit 2 million usage events per second at peak across about 1 million metered customers.
Core Requirements
- Every subscription must be billed exactly once per period, and every payment attempt must be idempotent. No double charges, no skipped cycles.
- Usage must be counted exactly once: no lost events (lost revenue) and no double counting (overbilling, which customers notice and hate).
- Invoices should be issued within an hour or so of a period's close.
- The cost explorer should be fresh to within about an hour, and its period total should match the final invoice to the cent once the period closes.
- Everything must be auditable: for any invoice line, we can show the usage and prices it came from.
Below the line (out of scope)
- Real-time spend caps that cut off service within seconds of a budget being hit. (We'll mention budget alerts.)
- Multi-region active-active billing for one customer.
Back of the envelope. Subscriptions: 5M subscriptions anchored on their sign-up dates means about 170k renewals a day on average. If we bill everyone on the 1st of the month (calendar billing), it's 5M on one day. Either way, a few thousand invoices a minute is easy for a database. The challenge is correctness, not load.
Usage: 2M events/s is about 170 billion events a day. At ~100 bytes each, that's ~17 TB of raw events a day. We can't store and price individual events in an OLTP database. But customers don't need per-event prices. If we aggregate into (customer, meter, hour) buckets, 1M customers × ~20 meters × 24 hours is about 480 million rows a day, and much less where customers use only a few meters. That's a stream aggregation problem, very similar to /system-design/ad-click-aggregator.
Notice how different the two halves are. Subscriptions are a low-volume scheduling and state-machine problem. Usage is a high-volume counting problem. Say that early, and design each half for what it is.