KatInterview

Design a Usage-Based Billing System with Holds, Batches, and Reconciliation

System DesignModel answer
HardModel answerSystem Designpaymentsidempotencyreconciliation

Last updated

Design a billing platform that authorizes funds, captures final amounts in batches, reconciles against processor settlements, and guarantees no customer is ever charged twice.

Scenario

You're building the billing infrastructure for a cloud platform that charges customers based on measured consumption — think compute time, API calls, or storage. Customers provide a payment method upfront. The platform periodically estimates what they owe, reserves those funds, then settles the final amount once usage is confirmed.

Your system must interact with one or more external card processors that are outside your control: they time out, return ambiguous status codes, and send settlement reports on their own schedule.

What You Need to Design

1. Authorization and capture flow The platform reserves an estimated amount on a customer's card before the billing period ends (an authorization or "hold"). Once actual usage is known, it captures the real amount, which may be lower than the hold. If billing is cancelled, the hold is released without charging anything.

2. Batch charge submission At the end of a billing cycle, thousands of customers are billed simultaneously. Charges must be grouped and submitted to processors in batches rather than one at a time. The system must handle partial batch failures gracefully and resume without re-submitting what already went through.

3. Reconciliation Every night (or on demand), the processor sends a settlement report listing what it actually processed and settled. Your internal records must be compared against that report. Any discrepancy — a charge your system thinks succeeded but the processor never saw, or a processor entry with no matching internal record — must be detected, classified, and resolved.

4. Idempotency Retries happen everywhere: a client retries a timed-out HTTP request, an internal job queue delivers a message twice, a batch is re-run after a crash. No customer should be charged more than once for the same billing event, regardless of how many times any part of the system retries.

Constraints

  • Money must be conserved: no amount can appear or disappear without a recorded, auditable entry.
  • Processor calls can time out or return an outcome-unknown error. Your system must handle the case where you don't know whether the charge went through.
  • The system should scale to tens of thousands of charges per billing cycle, with hold placements happening continuously throughout the day.

Expected Discussion

Walk through your data model, the state machine for a single payment, how you enforce idempotency at the database and processor layers, how batching works end-to-end, and how reconciliation catches and heals discrepancies. Call out where consistency is hardest and how you'd handle it.

Address the following clarifying questions as part of your answer, or explain how the answers would change your design:

  • What daily volume and peak rate are expected?
  • Do holds have a processor-enforced expiry window?
  • Are multiple currencies in scope?
  • What are the latency requirements for the hold step vs. the batch capture step?
  • Where does card data live, and what compliance constraints apply?

Hints

Hint 1

Start by drawing the states a single payment moves through — PENDING_AUTH, AUTHORIZED, CAPTURED, RELEASED, FAILED — and ask yourself what database-level mechanism prevents two concurrent requests from moving it to the same terminal state simultaneously.

Hint 2

Idempotency for your own database is solved with a unique key per billing event, but idempotency for processor calls is trickier: if a call times out you don't know the outcome. Think about what you store before the call, and how you use the processor's own idempotency-key header to safely retry without issuing a second charge.

Hint 3

Reconciliation is a matching problem: you have two lists of transactions (yours and the processor's) that need to be joined on a shared reference ID. Think about the four possible match outcomes — matched, internal-only, processor-only, amount mismatch — and what the right remediation action is for each one.