Diagnose a Deadlock When Flash-Sale Orders Reserve Bundled Inventory
Last updated: November 29, 2026
Quick Overview
During a Douyin e-commerce flash sale, orders for product bundles update several SKU rows in one MySQL transaction, and the database starts reporting deadlocks; explain why it happens and how you would fix it in both the database and application code.
ByteDance
November 29, 20260
0
0 solved
During a Douyin e-commerce flash sale, orders for product bundles update several SKU rows in one MySQL transaction, and the database starts reporting deadlocks; explain why it happens and how you would fix it in both the database and application code.
Douyin's live commerce produces huge write bursts on a few hot SKUs, and ByteDance onsites for e-commerce backend roles probe whether you understand row locks and deadlock detection in InnoDB. The interviewer checks lock ordering, reading the deadlock log, and broader mitigation for hot rows.
What the Interviewer Expects
- Explain the circular wait: two transactions locking the same rows in opposite order
- Read SHOW ENGINE INNODB STATUS to find the two statements and locks involved
- Fix with consistent lock ordering (sort SKU ids) and short transactions
- Retry deadlock victims with backoff since InnoDB rolls one back
- Address hot-row contention with pre-split stock buckets or a queue
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
- Gap locks under REPEATABLE READ cause deadlocks even on inserts: explain.
- How would you prevent overselling without row locks, for example with Redis?
- When would you choose optimistic concurrency with version columns instead?
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
Clarify: one MySQL primary, transaction runs UPDATE stock SET qty = qty - 1 WHERE sku = ? AND qty > 0 for each SKU in the bundle. Bundle A+B and bundle B+A are processed concurrently. Isolation is REP...
High-Level Design
Transaction 1 locks row A then waits for B, while transaction 2 holds B and waits for A. InnoDB detects the cycle and rolls one back. The cure is to remove the cycle: always lock rows in a global orde...