Design GPU Credits

Cloud GPU Billing

Design the credits system behind a cloud GPU service: users buy credits, launch instances that burn credits by the second, and get stopped when the credits run out.

Overview

A cloud GPU service sells time on graphics cards. A user buys a pile of credits, launches an instance of some GPU type, and the instance eats credits for as long as it runs. When the credits are gone, the instance must stop, and the user must be able to see exactly where every credit went. The interview asks you to design the part that keeps that count honest.

The problem looks like a billing ledger with a timer attached. Two things make it harder than a bank balance. First, spending is continuous: an instance owes money every second it runs, but nobody calls a method every second, so the system bills in intervals and has to decide what happens between them. Second, many instances can be charging the same account at the same moment, so the check 'is there enough left?' and the deduction have to be one atomic step.

Interviewers use this problem to see whether you keep money out of floating point, whether you make the balance check and the charge atomic, whether you can explain why a ledger of movements beats a mutable counter, and whether you handle the moment the credits run out without losing either the user's work or the provider's revenue.

Scoping the problem

Before drawing anything, settle six questions with the interviewer. Each answer below is the one the rest of this chapter uses.

  • Is a credit a currency unit or a fixed amount of GPU time? Treat it as a currency unit, and give each GPU type a rate in credits per hour. A rate card in credits per hour makes each GPU type a price, which is the simpler model.
  • How fine is billing: per hour, per minute or per second? Bill per second, with a 60-second minimum per run. Coarser billing overcharges short jobs; finer billing needs more charges.
  • Who calls billing, and how often? A scheduler calls bill(now) every few minutes. The gap between calls is the amount the account can go negative by, so this answer shapes the run-out logic.
  • What happens when the credits run out mid-run? The choice is to terminate at once or to warn and give a grace period. Here the service charges what is left and terminates, and a low-balance event warns first.
  • Are there cheaper preemptible instances? Yes, through a spot policy with a discount. The provider can take those GPUs back, so the design needs a preempted state.
  • Is the balance the source of truth, or the ledger? The ledger is. A ledger can rebuild any balance and explains every charge, so balances are derived from it.

Functional requirements

  • Buy credits: an amount is added to the user account and recorded in the ledger
  • Launch an instance of a GPU type under a pricing policy (on demand or spot); refused when the account cannot cover the minimum bill
  • Metered billing: bill(now) charges every running instance for the seconds since its last charge, at its hourly rate
  • Stop settles the final charge; every run is billed for at least 60 seconds
  • When a charge exceeds the balance, the remaining credits are taken and the instance is terminated
  • Spot instances can be preempted by the provider; the bill is settled and the state recorded
  • A low-balance event fires once when an account drops below a threshold, until credits are bought again
  • Every credit movement is an immutable posting with a source, a destination, an amount and a memo

Non-functional requirements

  • The balance check and the deduction are atomic: many instances can charge one account at once without overdrawing it
  • Credits are exact: Decimal with a fixed rounding rule, never float
  • The ledger is auditable: balances are derived from postings and the sum of all balances is always zero
  • Billing is deterministic under test: the clock is an argument, not a global
  • A stopped, terminated or preempted instance is never charged again

Interview questions

  • Why a ledger of postings instead of a balance field you add to and subtract from?
  • Two instances bill the same account at the same moment. How do you stop the balance going negative?
  • Billing runs every few minutes. What goes wrong between two runs, and how would you bill by events instead?
  • How would you add reserved capacity paid up front, or a spot discount?
  • How do you keep this correct across a crash or across many servers?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio