Deadlock Free Position Transfers With Per Account Locks
Last updated: November 29, 2026
Quick Overview
Implement a thread safe position keeper where fills update one account and transfers move quantity between two accounts. Use fine grained per account locks rather than one global lock, and guarantee transfers cannot deadlock.
DRW
November 29, 20260
0
0 solved
Implement a thread safe position keeper where fills update one account and transfers move quantity between two accounts. Use fine grained per account locks rather than one global lock, and guarantee transfers cannot deadlock.
DRW systems process fills from many venues concurrently, so position state must be correct under contention without a single global lock becoming a bottleneck. The interviewer checks that you know the classic two lock deadlock, fix it with lock ordering, and can reason about what else could go wrong.
What the Interviewer Expects
- Explains the deadlock when two transfers lock in opposite order
- Imposes a global lock order such as sorting account ids
- Keeps the registry lock scope tiny
- Handles the same account transfer edge case
- Discusses alternatives such as single writer sharding
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 take a consistent snapshot across all accounts?
- Would a single threaded event loop per shard be better? Why or why not?
- How would you detect a deadlock in production?
- What changes in Python versus C++ regarding the GIL?
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
apply_fill(account, symbol, qty) and transfer(src, dst, symbol, qty) may run on many threads. Totals must be exact and no thread may block forever. Reads return a consistent per account snapshot....
High-Level Design
One lock per account, created lazily under a small registry lock held only for the dict lookup. A transfer needs both accounts; if thread 1 locks A then B while thread 2 locks B then A, each waits for...