Problem Statement
How would you design safe retries for transient errors like deadlocks or lock timeouts?
Explanation
First, make each unit idempotent: the same unit can run again without double effects. Use natural or surrogate keys to detect duplicates and upserts where possible. Log correlation IDs so you can spot duplicates.
Next, implement bounded exponential backoff with jitter. On specific transient codes, roll back, wait, and retry a few times. Keep the unit small and consistent in resource order to reduce collisions. If retries exhaust, surface a controlled failure and alert.
Code Solution
SolutionRead Only
for attempt in 1..5 {
BEGIN; -- do work
if success COMMIT; else ROLLBACK; sleep(backoff(attempt)+jitter);
}Practice Sets
This question appears in the following practice sets:
