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:

MethodReturns
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

3. Retry engine design decisions to make first

Write one line for each before opening the editor.

  1. 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.)
  2. What exactly is a failure? The contract names two ways an attempt can fail. What gets stored as the last error in each case?
  3. Which delay goes with which retry? Write delay(1), delay(2) and delay(3) for configure(3, 100, 2.0, 250) by hand, including the cap.
  4. When is cancellation checked? If cancel arrives while the engine is sleeping between attempts, what happens next?
  5. What does configure change for an operation that is already running? The contract answers this. Find the sentence.
  6. 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%

4. Retry engine pitfalls

⚠️ Trap: getNextRetryDelayMs is the delay before jitter. Reporting the jittered value you actually slept makes it random, and a random value can't match.

⚠️ Trap: maxRetries = 0 still means one attempt. The attempt count for an operation that always fails is 1 + 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

Further reading

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.

Open the problem