Problem Statement
Your app occasionally hits deadlock victims. Describe a robust handling strategy at the application layer.
Explanation
Treat deadlocks as expected under contended workloads. Make the unit of work idempotent, catch the specific error code, and retry the whole transaction a small number of times with backoff. Keep transactions short and ordered to lower the chance of a second collision.
Log the query shapes involved so you can re-order updates or add selective indexes to shrink lock footprints and reduce contention hot spots.
Code Solution
SolutionRead Only
-- pseudocode
for attempt in 1..3:
begin tx
try { do_work(); commit; break; }
catch deadlock { rollback; sleep(jitter); continue; }Practice Sets
This question appears in the following practice sets:
