KatInterview

Build a Recurring Billing Engine for a Subscription Service

System DesignModel answer
MediumModel answerSystem Designsubscriptionspaymentsbilling

Last updated

Design the backend that manages subscriptions, handles recurring charges, processes plan changes mid-cycle, and retries failed payments at scale.

You're building the subscription and billing backend for a video streaming platform. Users sign up for a paid tier, are billed automatically each cycle, can move between tiers at any time, and may cancel whenever they choose. The system must always know whether a given account can access content and which tier's features apply. All monetary transactions flow through a third-party payment processor whose API can decline requests, time out, or return errors.

Your design should handle the complete subscription journey: initial signup, automated recurring charges, tier switches that happen mid-cycle, retries when payments fail, and cancellations. The conversation will move through each of these areas in turn, starting with a working end-to-end design and then drilling into the details of billing correctness, concurrency, and scale.

Scope and Assumptions

  • The payment processor is external. You design the service that orchestrates calls to it and persists results, not the card network itself.
  • Billing repeats on a fixed cadence—assume monthly unless you choose differently. No specific plan catalog, pricing, or traffic volume is given; state your own assumptions.
  • Core guarantees: no user is ever double-charged for the same period, and a paying user never loses access due to an internal bug or transient failure.
  • Video delivery is outside this design. The playback system only needs a fast way to ask "can this account stream right now, and under which tier?"

Questions to Consider

  • When a user upgrades mid-cycle, should the new tier activate immediately? What about downgrades—immediate or deferred until renewal?
  • After a renewal charge fails, how long does the user retain access while retries happen, and at what point does the subscription terminate?
  • Are free trials, promotional discounts, or third-party billing (app stores, bundles) in scope, or only direct card charges?
  • Which regions and currencies matter, and is tax computation handled here or delegated to another service?
  • How quickly must a cancellation or tier change become visible to playback—is brief eventual consistency acceptable?

Part 1 — Data Model and Lifecycle

Define the core entities: accounts, tiers, subscriptions, invoices, and payment records. Map out the states a subscription passes through from creation to cancellation, the transitions between them, and what triggers each transition. Describe the APIs other services use to create, query, modify, and cancel subscriptions, and explain how the playback path determines entitlement.

Cover:

  • Entities and their relationships, including how tier and pricing history is preserved
  • An explicit state machine with clear triggers for each transition
  • Management APIs and a separate, low-latency entitlement check suitable for every playback request
  • Where the authoritative state lives and how other services learn about updates

Part 2 — Automated Recurring Charges

Walk through how the system identifies subscriptions due for renewal, generates the charge for the next cycle, invokes the payment processor, and records the outcome. Address what happens if a billing worker crashes mid-execution or if the processor call times out with no definitive response.

Cover:

  • Scheduling renewals so that none are missed and none are processed more than once
  • Idempotent charge creation and reconciliation with the processor's own ledger
  • A durable, auditable trail of invoices and financial events
  • Handling ambiguous results: timeouts, duplicate webhooks, out-of-order notifications

Part 3 — Mid-Cycle Tier Changes

A user on a lower tier upgrades partway through their billing period; another user downgrades. Describe when each change becomes effective, how the adjusted amount is computed, and how you prevent a tier change from racing with a concurrent renewal job.

Cover:

  • Policy for when upgrades versus downgrades take effect and the impact on access
  • Prorated charge calculation and how it appears on the invoice
  • Concurrency control between user-initiated actions and background billing processes
  • Behavior when the immediate charge for an upgrade is declined

Part 4 — Failure Recovery and Scaling

Design the retry strategy for declined renewals: when to retry, what the user sees and can do during retries, and when the subscription ultimately expires. Then explain how the design performs as the subscriber base grows and renewals cluster on the same calendar dates.

Cover:

  • Retry scheduling that differentiates transient from permanent declines, with user notifications
  • A grace period with access rules that stay consistent with billing state
  • Distributing renewal load and keeping entitlement checks fast as volume increases
  • Rate-limiting calls to the payment processor and recovering from processor downtime

Hints

Hint 1

Start with a simple state machine and add idempotency keys to every payment operation.

Hint 2

Separate the write path (billing, plan changes) from the read path (entitlement checks) so playback stays fast.

Hint 3

Use optimistic locking or versioning to prevent concurrent plan changes and renewals from interfering with each other.