Report #5224
[bug\_fix] ERROR: deadlock detected
Implement application-level retry logic with exponential backoff to automatically re-run transactions that fail due to deadlocks, and refactor the database access layer to always acquire row-level locks in a consistent global order \(e.g., always lock table A before table B\) to eliminate the circular wait condition that causes deadlocks.
Journey Context:
An e-commerce platform runs fine under normal load, but during flash sales with 10,000 concurrent checkouts, logs fill with "ERROR: deadlock detected" and transactions roll back, causing checkout failures. The developer examines pg\_stat\_activity and finds two concurrent sessions: Session A holds a lock on inventory row \#123 and is waiting for a lock on user\_balance row \#456. Session B holds a lock on user\_balance row \#456 and is waiting for a lock on inventory row \#123. This circular dependency creates a deadlock. The developer initially tries reducing the isolation level from SERIALIZABLE to READ COMMITTED, but deadlocks persist because the row-level lock contention remains. Analyzing the code reveals that the OrderService updates inventory first and then payment, while the RefundService \(which can run concurrently during partial refunds\) updates payment first and then inventory. This inconsistent lock ordering causes the circular wait. The developer implements a retry loop in the Spring Boot service layer: catch the SQLException, check if the SQLState is "40P01" \(deadlock detected\), wait a random interval \(exponential backoff up to 100ms\), and retry the transaction up to 3 times. Additionally, the developer refactors all inventory-related operations to always acquire database locks in alphabetical order: always lock the customer row first, then the inventory row, then the payment row. This consistent ordering eliminates the circular dependency, and deadlock frequency drops to zero.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T20:52:39.158662+00:00— report_created — created