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.
Trying a failed call again. It helps with temporary failures and hurts when every client does it at the same moment.
Each retry waits twice as long as the previous one (0.5 s, 1 s, 2 s, 4 s…), up to a cap.
A random part of the wait, so a million clients spread their retries out instead of returning in the same second.
A header the server sends with 429 or 503 saying exactly how long to wait. It overrides your own schedule.
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.
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 ask | Interviewer 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. |
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.
Wait a random time between zero and the exponential ceiling; it beats equal or decorrelated jitter for spreading load.
1 shl attempt.coerceIn(0, 16) avoids overflow and negative shifts.
Tests pass a fixed random function and a recording sleep: no flaky timing.
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.
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}1import java.time.Duration;2import java.util.concurrent.Callable;3import java.util.function.DoubleUnaryOperator;4import java.util.function.Predicate;5 6public final class Backoff {7 private Backoff() {}8 9 /** Errors that carry the server's Retry-After hint (HTTP 429 / 503). */10 public interface RetryAfterProviding {11 Duration retryAfter();12 }13 14 @FunctionalInterface15 public interface Sleeper {16 void sleep(Duration duration) throws InterruptedException;17 }18 19 /**20 * Exponential backoff with full jitter: a random wait in [0, min(cap, base · 2^attempt)].21 * [random] receives that ceiling in seconds and returns a value between 0 and it.22 * Jitter spreads retries out, so a million clients do not all return in the same second.23 */24 public static Duration delay(int attempt, Duration base, Duration cap, DoubleUnaryOperator random) {25 Duration exponential = base.multipliedBy(1L << Math.min(Math.max(attempt, 0), 16));26 Duration ceiling = exponential.compareTo(cap) < 0 ? exponential : cap;27 double seconds = random.applyAsDouble(ceiling.toNanos() / 1e9);28 return Duration.ofNanos((long) (seconds * 1e9));29 }30 31 public static Duration delay(int attempt) {32 return delay(attempt, Duration.ofMillis(500), Duration.ofSeconds(60), max -> Math.random() * max);33 }34 35 /** Retries [operation] with jittered backoff, honouring Retry-After and cancellation (interruption). */36 public static <T> T withRetry(Callable<T> operation, int maxAttempts, Predicate<Exception> isRetryable, Sleeper sleep) throws Exception {37 int attempt = 0;38 while (true) {39 try {40 return operation.call();41 } catch (InterruptedException e) {42 throw e; // never retry a cancelled caller43 } catch (Exception e) {44 attempt++;45 if (attempt >= maxAttempts || !isRetryable.test(e)) throw e;46 Duration hint = e instanceof RetryAfterProviding provider ? provider.retryAfter() : null;47 sleep.sleep(hint != null ? hint : delay(attempt));48 }49 }50 }51}1export const Backoff = {2 /**3 * Exponential backoff with full jitter: a random wait in [0, min(cap, base · 2^attempt)] milliseconds.4 * Jitter spreads retries out, so a million clients do not all return in the same second.5 */6 delay(attempt: number, baseMs = 500, capMs = 60_000, random: () => number = Math.random): number {7 const exponential = baseMs * 2 ** Math.min(Math.max(attempt, 0), 16);8 const ceiling = Math.min(capMs, exponential);9 return random() * ceiling;10 },11};12 13/** Errors that carry the server's Retry-After hint (HTTP 429 / 503), in milliseconds. */14export interface RetryAfterProviding {15 retryAfterMs?: number;16}17 18export interface RetryOptions {19 maxAttempts?: number;20 isRetryable?: (e: unknown) => boolean;21 sleep?: (ms: number) => Promise<void>;22}23 24const isCancelled = (e: unknown) => e instanceof Error && e.name === "AbortError";25 26/** Retries [operation] with jittered backoff, honouring Retry-After and cancellation. */27export async function withRetry<T>(operation: () => Promise<T>, options: RetryOptions = {}): Promise<T> {28 const { maxAttempts = 5, isRetryable = () => true, sleep = (ms) => new Promise<void>((r) => setTimeout(r, ms)) } = options;29 let attempt = 0;30 for (;;) {31 try {32 return await operation();33 } catch (e) {34 if (isCancelled(e)) throw e; // never retry a cancelled caller35 attempt++;36 if (attempt >= maxAttempts || !isRetryable(e)) throw e;37 const hint = (e as RetryAfterProviding).retryAfterMs;38 await sleep(hint ?? Backoff.delay(attempt));39 }40 }41}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.
Exponential, capped
base · 2^attempt with the shift clamped to 0–16, then capped at 60 seconds. Duration does the unit maths.
20 val exponential = base * (1 shl attempt.coerceIn(0, 16))21 val ceiling = minOf(cap, exponential)Full jitter
A random wait in [0, ceiling]: clients spread out instead of returning in lockstep.
22 return random(0.0..ceiling.toDouble(DurationUnit.SECONDS)).secondsServer hints
Exceptions that know the server's Retry-After expose it through an interface.
27interface RetryAfterProviding {28 val retryAfter: Duration?29}Try the operation
Return on the first success.
40 try {41 return operation()Give up when it is pointless
Rethrow cancellation untouched; stop after maxAttempts or on non-retryable errors.
42 } catch (e: CancellationException) {43 throw e // never retry a cancelled caller44 } catch (e: Exception) {45 attempt++46 if (attempt >= maxAttempts || !isRetryable(e)) throw eWait the right amount
Use Retry-After if the server sent one, otherwise the jittered backoff. sleep defaults to delay(), which is cancellable.
47 sleep((e as? RetryAfterProviding)?.retryAfter ?: Backoff.delay(attempt))Complexity: O(attempts) calls; waits grow to the cap
Q&A
The questions interviewers ask after the code works. Answer out loud, then open.
4.1Follow-up questions
Why not a fixed delay?
What else protects the server?
Retry POSTs?
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
| Approach | Swift / iOS |
|---|---|
| Background jobs | BGTaskScheduler has no backoff policy: reschedule with earliestBeginDate set to your own jittered delay. |
| async/await | The same loop with Task.sleep(for:); check Task.isCancelled before retrying. |