Design a retry executor with exponential backoff and a maximum retry limit

Quick Overview

Implement a retry executor that simulates exponential backoff with a retry limit, returns a task's value or FAILED, and tracks the total delay and the attempts in the last call. It tests stateful class design, handling of the final attempt, and how the backoff adds up across calls.

Design a retry executor with exponential backoff and a maximum retry limit

Company: Airbnb

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a retriable function executor that simulates retrying a task with exponential backoff and a maximum retry limit. The executor is initialized with a maximum number of retries and an initial backoff delay in milliseconds. Tasks can be registered with a specified number of failures before they succeed. When a task is executed, the executor attempts it up to `max_retries + 1` times total (1 initial attempt plus up to `max_retries` retries). After each failed attempt (except the last), the executor accumulates a simulated backoff delay, which doubles after each failure. If the task succeeds within the allowed attempts, the executor returns the task's success value as a string. If all attempts are exhausted, it returns `"FAILED"`. The executor also tracks the total accumulated delay across all executions and the number of attempts made in the most recent execution. Implement the following methods: - `RetriableFunction(max_retries: int, initial_backoff_ms: int)`: Create the executor with its retry limit and first backoff delay. - `register(task_id: str, fail_count: int, success_value: int)`: Register a task with a given ID. `fail_count` specifies how many times the task will fail before succeeding. `success_value` is the value returned on success. - `execute(task_id: str) -> str`: Execute the registered task with retries and exponential backoff. Returns the success value as a string if the task succeeds within the retry limit, or `"FAILED"` otherwise. Resets the task's failure counter each time, so a task registered with `fail_count = 2` fails twice again on every call. - `get_total_delay() -> int`: Return the total simulated backoff delay accumulated across all `execute` calls, in milliseconds. - `get_last_attempts() -> int`: Return the number of attempts made during the most recent `execute` call. - `reset()`: Reset all internal state: clear registered tasks, total delay, and attempt counters. Example 1: ```text Input: ["RetriableFunction", "register", "execute", "get_last_attempts", "get_total_delay", "execute", "get_last_attempts", "get_total_delay"] [[2, 500], ["taskX", 0, 7], ["taskX"], [], [], ["taskX"], [], []] Output: [null, null, "7", 1, 0, "7", 1, 0] Explanation: rf = RetriableFunction(2, 500) # max_retries=2, initial backoff 500 ms rf.register("taskX", 0, 7) # taskX never fails (fail_count=0) rf.execute("taskX") # -> "7" (succeeds on first attempt) rf.get_last_attempts() # -> 1 (only 1 attempt needed) rf.get_total_delay() # -> 0 (no failures, no backoff) rf.execute("taskX") # -> "7" (succeeds immediately again) rf.get_last_attempts() # -> 1 rf.get_total_delay() # -> 0 (still 0 total delay) ``` Example 2: ```text Input: ["RetriableFunction", "register", "execute", "get_last_attempts", "get_total_delay", "register", "execute", "get_last_attempts", "get_total_delay", "execute", "get_total_delay", "reset", "get_total_delay"] [[3, 100], ["taskA", 2, 42], ["taskA"], [], [], ["taskB", 5, 9], ["taskB"], [], [], ["taskA"], [], [], []] Output: [null, null, "42", 3, 300, null, "FAILED", 4, 1000, "42", 1300, null, 0] Explanation: rf = RetriableFunction(3, 100) # max_retries=3, initial backoff 100 ms rf.register("taskA", 2, 42) # taskA fails twice, then returns 42 rf.execute("taskA") # -> "42" (fail, wait 100, fail, wait 200, succeed) rf.get_last_attempts() # -> 3 rf.get_total_delay() # -> 300 (100 + 200) rf.register("taskB", 5, 9) # taskB fails 5 times, more than 3 retries allow rf.execute("taskB") # -> "FAILED" (4 attempts, waits 100, 200, 400, no wait after the last) rf.get_last_attempts() # -> 4 rf.get_total_delay() # -> 1000 (300 + 100 + 200 + 400) rf.execute("taskA") # -> "42" (failure counter restarts, so 2 failures again) rf.get_total_delay() # -> 1300 (1000 + 100 + 200) rf.reset() # clears tasks, total delay and attempt count rf.get_total_delay() # -> 0 ``` ```hint Which failures wait For a task that fails `f` times, list the attempts that fail and mark the ones that still have a retry after them. Only those add a backoff. ``` ```hint Where each value lives Sort the state into values that belong to one `execute` call and values that persist across calls, then check what `reset` has to clear. ``` ### Constraints & Assumptions - `1 <= max_retries <= 10` - `1 <= initial_backoff_ms <= 5000` - Task IDs are unique non-empty strings. - `0 <= fail_count <= 20` - `-10^4 <= success_value <= 10^4` - At most `100` calls total across all methods. - `execute` is only called on previously registered task IDs. ### Clarifying Questions to Ask - Should the executor actually sleep for the backoff, or only record it? - If `register` is called again with an existing ID, should the new settings replace the old ones? - What should `get_last_attempts` return before any `execute` call, and right after `reset`? - After `reset`, can `execute` be called on a task that was registered before the reset? ### What a Strong Answer Covers - The per-task state and the executor-wide state, and which of them `execute` and `reset` change. - The attempt loop: the limit of `max_retries + 1` attempts, which failures add a backoff, and when the backoff doubles. - Correct results for the boundary cases: a task that never fails, one that succeeds on the last allowed attempt, and one that runs out of attempts. - The time and space cost of each method. ### Follow-up Questions - How would you turn this into a real retry wrapper, such as a decorator that calls a function, sleeps between attempts and retries only on certain exceptions? - Why do production retry policies add random jitter to exponential backoff, and how would you add it here while keeping tests deterministic? - How would you cap the backoff at a maximum delay, or bound the total time spent retrying one call? - Why is retrying a non-idempotent operation, such as charging a card, dangerous, and how would you make it safe?

Overview: Implement a retry executor that simulates exponential backoff with a retry limit, returns a task's value or FAILED, and tracks the total delay and the attempts in the last call. It tests stateful class design, handling of the final attempt, and how the backoff adds up across calls.

Airbnb logo
Airbnb
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
1
0

Design a retriable function executor that simulates retrying a task with exponential backoff and a maximum retry limit.

The executor is initialized with a maximum number of retries and an initial backoff delay in milliseconds. Tasks can be registered with a specified number of failures before they succeed. When a task is executed, the executor attempts it up to max_retries + 1 times total (1 initial attempt plus up to max_retries retries). After each failed attempt (except the last), the executor accumulates a simulated backoff delay, which doubles after each failure.

If the task succeeds within the allowed attempts, the executor returns the task's success value as a string. If all attempts are exhausted, it returns "FAILED". The executor also tracks the total accumulated delay across all executions and the number of attempts made in the most recent execution.

Implement the following methods:

  • RetriableFunction(max_retries: int, initial_backoff_ms: int) : Create the executor with its retry limit and first backoff delay.
  • register(task_id: str, fail_count: int, success_value: int) : Register a task with a given ID. fail_count specifies how many times the task will fail before succeeding. success_value is the value returned on success.
  • execute(task_id: str) -> str : Execute the registered task with retries and exponential backoff. Returns the success value as a string if the task succeeds within the retry limit, or "FAILED" otherwise. Resets the task's failure counter each time, so a task registered with fail_count = 2 fails twice again on every call.
  • get_total_delay() -> int : Return the total simulated backoff delay accumulated across all execute calls, in milliseconds.
  • get_last_attempts() -> int : Return the number of attempts made during the most recent execute call.
  • reset() : Reset all internal state: clear registered tasks, total delay, and attempt counters.

Example 1:

Input:
["RetriableFunction", "register",      "execute", "get_last_attempts", "get_total_delay", "execute", "get_last_attempts", "get_total_delay"]
[[2, 500],            ["taskX", 0, 7], ["taskX"], [],                  [],                ["taskX"], [],                  []]

Output: [null, null, "7", 1, 0, "7", 1, 0]

Explanation:
rf = RetriableFunction(2, 500)    # max_retries=2, initial backoff 500 ms
rf.register("taskX", 0, 7)        # taskX never fails (fail_count=0)
rf.execute("taskX")               # -> "7"   (succeeds on first attempt)
rf.get_last_attempts()            # -> 1     (only 1 attempt needed)
rf.get_total_delay()              # -> 0     (no failures, no backoff)
rf.execute("taskX")               # -> "7"   (succeeds immediately again)
rf.get_last_attempts()            # -> 1
rf.get_total_delay()              # -> 0     (still 0 total delay)

Example 2:

Input:
["RetriableFunction", "register", "execute", "get_last_attempts", "get_total_delay", "register", "execute", "get_last_attempts", "get_total_delay", "execute", "get_total_delay", "reset", "get_total_delay"]
[[3, 100], ["taskA", 2, 42], ["taskA"], [], [], ["taskB", 5, 9], ["taskB"], [], [], ["taskA"], [], [], []]

Output: [null, null, "42", 3, 300, null, "FAILED", 4, 1000, "42", 1300, null, 0]

Explanation:
rf = RetriableFunction(3, 100)    # max_retries=3, initial backoff 100 ms
rf.register("taskA", 2, 42)       # taskA fails twice, then returns 42
rf.execute("taskA")               # -> "42"      (fail, wait 100, fail, wait 200, succeed)
rf.get_last_attempts()            # -> 3
rf.get_total_delay()              # -> 300       (100 + 200)
rf.register("taskB", 5, 9)        # taskB fails 5 times, more than 3 retries allow
rf.execute("taskB")               # -> "FAILED"  (4 attempts, waits 100, 200, 400, no wait after the last)
rf.get_last_attempts()            # -> 4
rf.get_total_delay()              # -> 1000      (300 + 100 + 200 + 400)
rf.execute("taskA")               # -> "42"      (failure counter restarts, so 2 failures again)
rf.get_total_delay()              # -> 1300      (1000 + 100 + 200)
rf.reset()                        # clears tasks, total delay and attempt count
rf.get_total_delay()              # -> 0

Constraints & Assumptions

  • 1 <= max_retries <= 10
  • 1 <= initial_backoff_ms <= 5000
  • Task IDs are unique non-empty strings.
  • 0 <= fail_count <= 20
  • -10^4 <= success_value <= 10^4
  • At most 100 calls total across all methods.
  • execute is only called on previously registered task IDs.

Clarifying Questions to Ask Guidance

  • Should the executor actually sleep for the backoff, or only record it?
  • If register is called again with an existing ID, should the new settings replace the old ones?
  • What should get_last_attempts return before any execute call, and right after reset ?
  • After reset , can execute be called on a task that was registered before the reset?

What a Strong Answer Covers Guidance

  • The per-task state and the executor-wide state, and which of them execute and reset change.
  • The attempt loop: the limit of max_retries + 1 attempts, which failures add a backoff, and when the backoff doubles.
  • Correct results for the boundary cases: a task that never fails, one that succeeds on the last allowed attempt, and one that runs out of attempts.
  • The time and space cost of each method.

Follow-up Questions Guidance

  • How would you turn this into a real retry wrapper, such as a decorator that calls a function, sleeps between attempts and retries only on certain exceptions?
  • Why do production retry policies add random jitter to exponential backoff, and how would you add it here while keeping tests deterministic?
  • How would you cap the backoff at a maximum delay, or bound the total time spent retrying one call?
  • Why is retrying a non-idempotent operation, such as charging a card, dangerous, and how would you make it safe?