Prevent Deadlock When Transferring Inventory Between Two Warehouses
Last updated: November 29, 2026
Quick Overview
A service moves units of a SKU between fulfillment centers by locking the source and destination, and under load it occasionally hangs. Explain the deadlock and implement a transfer that cannot deadlock.
Amazon
November 29, 20260
0
0 solved
A service moves units of a SKU between fulfillment centers by locking the source and destination, and under load it occasionally hangs. Explain the deadlock and implement a transfer that cannot deadlock.
Inventory rebalancing between fulfillment centers runs as many parallel jobs moving stock in both directions. The interviewer wants the four Coffman conditions, a concrete fix based on lock ordering, and awareness of how the same issue appears in database row locks.
What the Interviewer Expects
- Explains the circular wait when A to B and B to A run at once
- Names the conditions required for deadlock
- Imposes a global lock order using a stable unique id
- Keeps the stock invariant: total units are conserved
- Compares lock ordering with try-lock with timeout and backoff
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 if a transfer touches three or more warehouses at once?
- How does a relational database detect and break this deadlock for you?
- When would you prefer tryLock with a timeout over ordering?
- How would you detect a deadlock in a running Java or Python service?
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
transfer(src, dst, sku, units)moves stock atomically or returns False if the source lacks stock.- Transfers in opposite directions run concurrently.
- No thread may block forever, and total unit...
High-Level Design
Thread 1 locks SEA1 then waits for PDX2; thread 2 locks PDX2 then waits for SEA1. All four Coffman conditions hold: mutual exclusion, hold and wait, no preemption, circular wait. Breaking any one suff...