Prevent Double Spends When Debits Race on One Balance
Last updated: November 29, 2026
Quick Overview
Two requests debit the same account at the same moment and both pass the balance check, overdrawing it. Explain the race, then implement debits and transfers that are safe under concurrency and cannot deadlock.
Coinbase
November 29, 20260
0
0 solved
Two requests debit the same account at the same moment and both pass the balance check, overdrawing it. Explain the race, then implement debits and transfers that are safe under concurrency and cannot deadlock.
At Coinbase a race between a withdrawal and a trade on the same balance is a real loss, so engineers are expected to reason precisely about check-then-act bugs. The interviewer checks that you can explain the race, fix it in-process and in the database, and avoid deadlocks on transfers.
What the Interviewer Expects
- Identify the check-then-act race and show an interleaving
- Make the check and the write one atomic step
- Acquire locks for transfers in a global order to prevent deadlock
- Translate the fix to the database: row locks, conditional updates or version checks
- Test with many threads and an invariant check
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
- How would you do this with a single SQL statement and no explicit lock?
- When is optimistic concurrency better than pessimistic locking here?
- How do you prevent double spends across two services that each hold part of the balance?
- What isolation level would you choose in Postgres and why?
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
Debits must never take a balance below zero, even with many concurrent requests. Transfers move value atomically between two accounts. No deadlocks. The total across accounts is conserved....
High-Level Design
The bug: thread A reads balance 100, thread B reads 100, both see enough for a 70 debit, both write 30, and 140 left the account. The fix is to make read, check and write atomic per account. In-proces...