Prevent Double Spending Under Concurrent Withdrawals
Last updated: November 29, 2026
Quick Overview
Two requests try to withdraw from the same SoFi Money account at the same moment and both see a sufficient balance. Explain how the race happens, then implement withdraw and transfer so the balance can never go negative and concurrent opposite-direction transfers cannot deadlock.
SoFi
November 29, 20260
0
0 solved
Two requests try to withdraw from the same SoFi Money account at the same moment and both see a sufficient balance. Explain how the race happens, then implement withdraw and transfer so the balance can never go negative and concurrent opposite-direction transfers cannot deadlock.
Moving money correctly under concurrency is central to a chartered bank like SoFi, and this question is a common fundamentals deep dive. The interviewer checks that you can name the check-then-act race, fix it in code with locks and ordering, and translate the same guarantee to a database with conditional updates or row locks.
What the Interviewer Expects
- Identifies the check-then-act race on read balance then write balance
- Holds a per-account lock across both the check and the debit
- Acquires locks in a global order for transfers to avoid deadlock
- Explains the database equivalent: conditional UPDATE, SELECT FOR UPDATE, or optimistic versioning
- Discusses multi-instance services where in-process locks are not enough
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- What isolation level would you choose in Postgres for this, and why?
- How would you handle a transfer between accounts in two different databases?
- How would you test that the fix actually prevents the race?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
Balance must never go below zero, total money across accounts is conserved, and concurrent transfers in opposite directions (a to b and b to a) must not deadlock. Operations raise InsufficientFunds ...
High-Level Design
The bug: request 1 reads 1000, request 2 reads 1000, both check 1000 >= 800, both write 200, and the member has withdrawn 1600. The fix is to make check and write one atomic critical section per accou...