/Projects

Retry with exponential backoff and jitter

Retries that recover calmly instead of stampeding a struggling server.

PracticeMachine codingTier 2 · MechanismsCreate25 minMobile systems

You will be able to: build retry with exponential backoff and jitter in 25 minutes, edge cases included.

Source: Tested in tests/mobile/MobileTests.kt

Mobile engineeringReact Native in one place19 of 22Next: Offline-first inspection app with huge uploads →

When a push fails, this decides when to try again. Jitter stops 200,000 phones from returning in the same second after an outage. Start from the basics in Day 4 →

Study with AI

25-minute round · MediumKotlin · coroutines · tested Watch as reels
Part 1 of 5

Basics

The concepts this component is built on. With these in mind, every line of the solution has a reason.

1.1Key concepts

One idea per card, in the order the solution uses them.

01Retry

Trying a failed call again. It helps with temporary failures and hurts when every client does it at the same moment.

02Exponential backoff

Each retry waits twice as long as the previous one (0.5 s, 1 s, 2 s, 4 s…), up to a cap.

03Jitter

A random part of the wait, so a million clients spread their retries out instead of returning in the same second.

04Retry-After

A header the server sends with 429 or 503 saying exactly how long to wait. It overrides your own schedule.

Part 2 of 5

Question

What exactly you are asked to build, what must be true when you are done, and what to ask before you start.

2.1Prompt

The question as the interviewer asks it, and the requirements to check your solution against.

Interviewer

After an outage, five million phones reconnect at once and knock the API over again. Write the client retry helper so recovery is calm.

Requirements

  • Exponential growth with a cap
  • Full jitter to spread clients out
  • Honour Retry-After from 429 / 503
  • Stop on non-retryable errors or cancellation
  • Testable without real sleeping

2.2Clarify first

Questions worth asking, and the answers this interviewer gives.

You askInterviewer answers
Which errors are retryable?Timeouts, connection loss, 429 and 5xx. Not 4xx validation errors.
Do we have a server hint?Yes, Retry-After on 429 and 503. It wins over our own schedule.
What is the cap?60 seconds per wait, 5 attempts for user-facing calls.
Part 3 of 5

Detailed answer

The design decisions, the complete Kotlin solution, and a walk through it one block at a time.

3.1Design decisions

Settle these before writing code; each one shows up in the solution.

Full jitter

Wait a random time between zero and the exponential ceiling; it beats equal or decorrelated jitter for spreading load.

Shift with a clamp

1 shl attempt.coerceIn(0, 16) avoids overflow and negative shifts.

Inject randomness and sleep

Tests pass a fixed random function and a recording sleep: no flaky timing.

Never retry cancellation

CancellationException is rethrown first, so a closed screen stops retrying immediately.

3.2Solution

Complete, compiling Kotlin with coroutines, built with warnings as errors and covered by tests/mobile/MobileTests.kt. The Java and TypeScript tabs do the same job with each language's own tools: Java compiled and run in our tests, TypeScript run in our tests.

Backoff.ktKotlin
1import kotlin.coroutines.cancellation.CancellationException2import kotlin.random.Random3import kotlin.time.Duration4import kotlin.time.Duration.Companion.milliseconds5import kotlin.time.Duration.Companion.seconds6import kotlin.time.DurationUnit7import kotlinx.coroutines.delay8 9object Backoff {10  /**11   * Exponential backoff with full jitter: a random wait in [0, min(cap, base · 2^attempt)].12   * Jitter spreads retries out, so a million clients do not all return in the same second.13   */14  fun delay(15    attempt: Int,16    base: Duration = 500.milliseconds,17    cap: Duration = 60.seconds,18    random: (ClosedFloatingPointRange<Double>) -> Double = { it.start + Random.nextDouble() * (it.endInclusive - it.start) },19  ): Duration {20    val exponential = base * (1 shl attempt.coerceIn(0, 16))21    val ceiling = minOf(cap, exponential)22    return random(0.0..ceiling.toDouble(DurationUnit.SECONDS)).seconds23  }24}25 26/** Errors that carry the server's Retry-After hint (HTTP 429 / 503). */27interface RetryAfterProviding {28  val retryAfter: Duration?29}30 31/** Retries [operation] with jittered backoff, honouring Retry-After and cancellation. */32suspend fun <T> withRetry(33  maxAttempts: Int = 5,34  isRetryable: (Throwable) -> Boolean = { true },35  sleep: suspend (Duration) -> Unit = { delay(it) },36  operation: suspend () -> T,37): T {38  var attempt = 039  while (true) {40    try {41      return operation()42    } catch (e: CancellationException) {43      throw e                                          // never retry a cancelled caller44    } catch (e: Exception) {45      attempt++46      if (attempt >= maxAttempts || !isRetryable(e)) throw e47      sleep((e as? RetryAfterProviding)?.retryAfter ?: Backoff.delay(attempt))48    }49  }50}
50 linesUTF-8Kotlin

3.3Line-by-line walkthrough

The same code in the order you would explain it in the room. The line numbers follow the Kotlin file; the Java and TypeScript tabs above have the same steps in the same order.

1

Exponential, capped

base · 2^attempt with the shift clamped to 0–16, then capped at 60 seconds. Duration does the unit maths.

Backoff.ktKotlin
20    val exponential = base * (1 shl attempt.coerceIn(0, 16))21    val ceiling = minOf(cap, exponential)
lines 20–21UTF-8Kotlin
2

Full jitter

A random wait in [0, ceiling]: clients spread out instead of returning in lockstep.

Backoff.ktKotlin
22    return random(0.0..ceiling.toDouble(DurationUnit.SECONDS)).seconds
lines 22–22UTF-8Kotlin
3

Server hints

Exceptions that know the server's Retry-After expose it through an interface.

Backoff.ktKotlin
27interface RetryAfterProviding {28  val retryAfter: Duration?29}
lines 27–29UTF-8Kotlin
4

Try the operation

Return on the first success.

Backoff.ktKotlin
40    try {41      return operation()
lines 40–41UTF-8Kotlin
5

Give up when it is pointless

Rethrow cancellation untouched; stop after maxAttempts or on non-retryable errors.

Backoff.ktKotlin
42    } catch (e: CancellationException) {43      throw e                                          // never retry a cancelled caller44    } catch (e: Exception) {45      attempt++46      if (attempt >= maxAttempts || !isRetryable(e)) throw e
lines 42–46UTF-8Kotlin
6

Wait the right amount

Use Retry-After if the server sent one, otherwise the jittered backoff. sleep defaults to delay(), which is cancellable.

Backoff.ktKotlin
47      sleep((e as? RetryAfterProviding)?.retryAfter ?: Backoff.delay(attempt))
lines 47–47UTF-8Kotlin

Complexity: O(attempts) calls; waits grow to the cap

Part 4 of 5

Q&A

The questions interviewers ask after the code works. Answer out loud, then open.

4.1Follow-up questions

Why not a fixed delay?
Every client retries at the same moment, recreating the spike that caused the outage.
What else protects the server?
A circuit breaker that stops calls for a while after repeated failures, and a client-side rate limiter.
Retry POSTs?
Only with an idempotency key, otherwise a lost response leads to duplicate writes.
Part 5 of 5

Extra depth

The cases that break naive solutions, how the same design looks on iOS, and what to practise next.

5.1Edge cases

Say these before the interviewer does.

Show detailsHide details
  • attempt = 0 or negative
  • Huge attempt counts (overflow)
  • Cancellation during the wait
  • Retry-After larger than the cap (respect the server)

5.2On iOS

The same design in Swift and Apple's frameworks.

Show detailsHide details
ApproachSwift / iOS
Background jobsBGTaskScheduler has no backoff policy: reschedule with earliestBeginDate set to your own jittered delay.
async/awaitThe same loop with Task.sleep(for:); check Task.isCancelled before retrying.
AI Engineer · Retry with exponential backoff and jitter