KatInterview

Shared Account Balance API: Preventing Double-Spend at Scale

System DesignModel answer
HardModel answerSystem Designconcurrencypaymentsdistributed-systems

Last updated

Design an API for a shared payment account that keeps the balance consistent when multiple users submit payments simultaneously, at high throughput.

Shared Account Balance API

Two people hold access to the same payment account — think a joint household account or a shared corporate card. Both can initiate a payment at any moment, independently and without coordination.

Your task is to design the API and the backend system that makes this safe.

What you need to cover:

  • API contract: endpoints, request and response shapes, relevant HTTP status codes, and how clients handle errors
  • Safe retries: how the system guarantees a payment is executed exactly once even if a client retries after a timeout or network failure
  • Concurrency control: what happens when two payments hit the same account at the same millisecond — explain the mechanisms, name the trade-offs, and justify your choice
  • Data model: what you store and why, including how you reconstruct or verify the balance
  • Scaling: the system must handle roughly 1 billion payments per day with bursts reaching 100K writes per second; explain how per-account contention is different from aggregate throughput
  • Failure modes and monitoring: what can go wrong and how you detect it

Constraints and assumptions to state explicitly before designing:

  • A payment must either fully succeed or fully fail — no partial debits
  • The balance must never go negative unless you explicitly allow overdraft
  • No double-spend: retrying the same intended payment must not debit the account twice
  • p99 latency target: under 200 ms on the write path

A strong answer states its assumptions up front, walks through the hard part (per-account concurrency), explains why it chose one locking strategy over the others, and acknowledges what the design gives up.

Hints

Hint 1

Start by separating two distinct problems: aggregate throughput (solved with horizontal sharding) and per-account contention (solved with locking or atomic writes inside a single shard). Mixing them up leads to over-engineered answers.

Hint 2

A read-modify-write in application code — SELECT balance, check in code, then UPDATE — is the classic double-spend bug. Think about how the database can make the check and the write atomic without two round trips.

Hint 3

Idempotency keys need to be stored durably and their status (in-progress vs. done) needs to be updated in the same transaction as the debit. Think about what happens if the process crashes between the debit and the key update.