Design Concurrent Bank Transfers
🏦 What is a concurrent bank transfer system? It is the core of any ledger: a set of accounts and an operation that moves money from one to another. The twist is that thousands of transfers run at the same time, on many threads. Each one must be all or nothing, no account may go negative, and the money in the bank as a whole must never change by a single cent.
This is the classic interview problem for one idea: deadlock. The single-account parts are easy, a lock per account fixes the obvious races, and then two transfers going in opposite directions freeze the whole program. The question is really "do you know why, and do you know the standard fix?" The standard fix is a rule called lock ordering, and the rest of the design falls out of it.
Requirements
"Design a bank that supports transfers between accounts. Many threads call it at the same time. Make it correct and don't let it deadlock."
The data model is small. The questions that matter are about what "correct" means once threads overlap.
Clarifying Questions
You: Is the set of accounts fixed, or can accounts be opened and closed while transfers run?
Interviewer: Fixed. The bank is created with all its accounts, numbered from zero.
You: What happens if the source account does not have enough money?
Interviewer: The transfer fails and nothing changes. No overdrafts, no partial transfers.
You: Can someone transfer money from an account to itself?
Interviewer: That is a mistake on the caller's side. Reject it.
You: Besides transfers, what else do callers need?
Interviewer: Deposits, withdrawals and reading a balance. And the auditors want a total of all the money in the bank.
You: Does that total have to be exact while transfers are running, or is an approximate number fine?
Interviewer: Exact. If the auditor ever sees a total that is off, even for a moment, they raise an alarm.
You: How much concurrency do you expect? Is it fine if all transfers run one at a time?
Interviewer: There are many accounts and many threads. Two transfers that touch different accounts should be able to run at the same time.
The last two answers shape the design. "Exact total while transfers run" rules out simply adding up the balances without locks. "Transfers on different accounts run in parallel" rules out the easy answer of one big lock for the whole bank. Together they force per-account locks, and per-account locks are where deadlocks come from.
💡 Interview tip: Ask whether one global lock is acceptable before you design anything finer. Sometimes the interviewer says yes, and then the correct answer is ten lines long. When they say no, you have a clear reason for the extra complexity, and you can say so.
Final Requirements
- The bank holds accounts
0ton - 1, each with a starting balance. The set of accounts never changes. transfer(from, to, amount)moves the money and returnstrue, or returnsfalseand changes nothing if an account does not exist,from == to,amount <= 0, orfromholds less thanamount.depositandwithdrawchange one account, with the same validation. A withdrawal never takes a balance below zero.get_balance(id)returns one balance, or-1for an unknown account.total()returns the sum of all balances as one consistent snapshot: it never sees money that has left one account but not yet arrived in the other.- Every method is safe to call from many threads at once. Transfers on disjoint accounts can run in parallel.
- The system never deadlocks, whatever order the transfers arrive in.
Out of scope
- Currencies, interest, fees and transaction history.
- Persistence and recovery after a crash.
- Accounts on different machines (we touch on this in Extensibility).