KatInterview

Double-Entry Ledger with Account Consolidation

System DesignModel answer
MediumModel answerSystem Designsystem-designdistributed-systemstransactions

Last updated

Design an ACID, idempotent, sharded banking ledger that supports transfers, paginated history, and merging one account into another under concurrent traffic.

You are asked to design the backend of a corporate banking platform. The platform tracks account balances, moves money between accounts, and lets a customer fold one of their accounts into another.

Functional requirements

  1. Move money between two accounts held on the platform.
  2. Consolidate accounts: a customer designates a source account and a destination account. The source account's remaining balance and its visible transaction history become part of the destination, and the source account is permanently retired.
  3. Statement retrieval: return a paginated list of transactions for a given account.

Non-functional requirements

  • Balances must be strictly consistent (ACID); money is never created or destroyed.
  • A client retry of the same transfer must not post the transfer twice.
  • Every state change must be immutably recorded for auditors.
  • The system must sustain roughly 5,000 transactions per second globally.

What to deliver

  • The service endpoints (request/response shapes and status codes).
  • A schema for accounts and ledger entries, plus the invariant that protects the money.
  • How consolidation interacts with transfers that are hitting the source account at the same moment.
  • A sharding strategy and how you handle a transfer or merge that crosses shards.

Hints

Hint 1

Decide early whether 'moving history' means physically rewriting old rows or only changing what a reader sees — immutability and rewriting are in direct conflict.

Hint 2

If balances live in the same row as the accounts, closing an account and zeroing it must be one atomic step; think about what lock protects that.

Hint 3

A unique constraint written in the same database transaction as the ledger rows is the only reliable idempotency guard; a cache in front of it is just an optimization.

Hint 4

Pick your shard key so the common case (two accounts of the same customer) stays on one machine.