LLD Guide · Module 2: Problem prep · Lesson 02.01
Prep: Retry Engine with Exponential Backoff
The easiest problem to start with. What to decide before you write it, and what the brief quietly requires.
By Cruscible Engineering · Published
A retry engine looks like a loop with a
sleep. The points are in everything around the loop: state per operation, cancellation, and what each getter returns at every moment.
This lesson prepares you for Build a Retry Engine with Exponential Backoff. It doesn't solve it: it tells you what to decide before you start, so your first submission isn't a guess.
1. What the retry engine contract asks for
One engine, many independent operations. Each execute(operationId, action) calls the action, and if it fails, waits and calls it again, up to maxRetries more times. The wait grows each time (exponential backoff) and gets a little randomness (jitter) so many clients don't retry in lockstep.
Read the contract with this table in mind. Every getter has an answer even before execute runs:
| Method | Returns |
|---|---|
configure(maxRetries, initialDelayMs, multiplier, maxDelayMs) | nothing; applies to later execute calls |
execute(id, action) | true if an attempt succeeded, false when attempts ran out |
getAttemptCount(id) | attempts made so far (1 = no retries happened) |
getStatus(id) | PENDING, RETRYING, SUCCEEDED, FAILED or CANCELLED |
getLastError(id) | the last failure's message, or null |
getNextRetryDelayMs(id) | the next delay before jitter; 0 once completed |
getRetryTimestamps(id) | one epoch-ms timestamp per attempt, oldest first; empty if none |
cancel(id) | stops further attempts; a no-op once completed |
reset(id) | forgets the operation: back to PENDING |
2. Lessons to read before the retry engine
- 01.01 How LLD problems work: how
executegets called by the grader, and why exceptions andnullreturns matter. - 01.02 Thread-safety: different operations run on different threads at the same time.
- 01.05 State machines: the status lifecycle is a state machine.
3. Retry engine design decisions to make first
Write one line for each before opening the editor.
- Where does per-operation state live? The problem recommends the
kv-store, but allows in-memory structures. What does one operation's record hold? (Count, status, last error, timestamps, and which configuration it started with.) - What exactly is a failure? The contract names two ways an attempt can fail. What gets stored as the last error in each case?
- Which delay goes with which retry? Write
delay(1),delay(2)anddelay(3)forconfigure(3, 100, 2.0, 250)by hand, including the cap. - When is cancellation checked? If
cancelarrives while the engine is sleeping between attempts, what happens next? - What does
configurechange for an operation that is already running? The contract answers this. Find the sentence. - What are the legal status transitions? Draw them. Which getters change at each one?
Figure: Backoff and jitter: each retry waits longer, up to the cap, give or take 10%
- configure(3, 100, 2.0, 300): attempt 1 fails at 0 ms.
- After about 100 ms (±10% jitter) attempt 2 fails; the next delay is 200 ms.
- After about 200 ms attempt 3 fails; the formula gives 400 ms, capped at 300 ms.
- After about 300 ms attempt 4 succeeds: status SUCCEEDED, next delay 0.
4. Retry engine pitfalls
⚠️ Trap:
getNextRetryDelayMsis the delay before jitter. Reporting the jittered value you actually slept makes it random, and a random value can't match.
⚠️ Trap:
maxRetries = 0still means one attempt. The attempt count for an operation that always fails is1 + maxRetries.
⚠️ Trap: an action that throws is a failure, not a crash. Catch it, store its message as the last error, and keep retrying.
💡 Tip: Delays in the grader are short (tens of milliseconds), but they add up. Sleep only between attempts, never after the last one.
Before you submit
- Every getter returns something sensible for an id that has never been executed
maxRetries = 0runs the action exactly once- An action that throws counts as a failed attempt, and its message becomes the last error
- The delay grows by the multiplier and never exceeds
maxDelayMs - Jitter stays within ±10%, and
getNextRetryDelayMsreports the delay without it getNextRetryDelayMsis0once the operation has completed- One timestamp is recorded per attempt, in order
cancelstops further attempts and leaves completed operations aloneresetreturns the operation toPENDINGwith no historyconfigureaffects laterexecutecalls, not ones already running- Operations running at the same time on different threads don't affect each other
Further reading
- Exponential backoff and jitter (AWS Architecture Blog)
- Retry pattern (Azure Architecture Center)
ThreadLocalRandom(Java 17 API)
After you've tried it
Submit first. Once you have a score, the full walkthrough explains one complete solution: Building a Retry Engine with Exponential Backoff.