Background Job Processor with Retries and Exponential Backoff

Quick Overview

Design a background job processor with a FIFO queue, per-job states and retries that use exponential backoff. It tests modeling a job state machine, counting attempts against a retry limit without off-by-one errors, and keeping every operation constant time.

Background Job Processor with Retries and Exponential Backoff

Company: Shopify

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a background job processor that manages a task queue and retries failed jobs with exponential backoff. Jobs are added to the queue with a unique ID and a maximum number of retries. The processor dequeues jobs one at a time for execution, and a processed job is then marked as completed or failed. When a job fails and has retries left, it goes back on the queue with a backoff delay that doubles with each successive failure: `baseDelay * 2^(attempt - 1)`. A job is always in one of four states: `QUEUED`, `PROCESSING`, `COMPLETED` or `EXHAUSTED` (all retries used up). The processor can report a job's state, backoff delay and attempt number, and the queue size, at any time. Implement a class `JobProcessor`: - `JobProcessor(baseDelay: int)` creates a processor whose first retry waits `baseDelay` seconds. - `addJob(jobId: str, maxRetries: int) -> bool` adds a new job to the back of the queue. It returns `True` if the job was added and `False` if the ID already exists. - `processNext() -> str` removes the job at the front of the queue, moves it to `PROCESSING` and returns its ID. It returns `""` if the queue is empty. - `completeJob(jobId: str) -> bool` moves a `PROCESSING` job to `COMPLETED` and returns `True`. It returns `False` if the job does not exist or is not `PROCESSING`. - `failJob(jobId: str) -> str` records a failure for a `PROCESSING` job. If retries remain, the job goes to the back of the queue with its new backoff delay and the method returns `"QUEUED"`. If none remain, the job becomes `EXHAUSTED` and the method returns `"EXHAUSTED"`. It returns `"ERROR"` if the job does not exist or is not `PROCESSING`. - `getJobState(jobId: str) -> str` returns the job's state, or `"NOT_FOUND"` for an unknown ID. - `getBackoffDelay(jobId: str) -> int` returns the job's current backoff delay in seconds, or -1 for an unknown ID. - `getQueueSize() -> int` returns the number of jobs waiting in the queue. - `getAttempt(jobId: str) -> int` returns the job's current attempt number, or -1 for an unknown ID. The counters follow these rules: - A new job has attempt 0 and backoff delay 0. Every `failJob` call that does not return `"ERROR"` increments the attempt number by one. - `maxRetries` is the number of retries allowed after the first run, so a job can run at most `maxRetries + 1` times. - A failure that leads to a retry sets the delay to `baseDelay * 2^(attempt - 1)` using the new attempt number. A failure that exhausts the job sets the delay back to 0. - The processor has no clock. The delay is recorded for `getBackoffDelay` to report, and `processNext` returns queued jobs in first-in, first-out order without waiting. Example 1, with retries and exhaustion: ```python p = JobProcessor(10) p.addJob("a1", 2) # True p.addJob("a1", 5) # False, the ID already exists p.addJob("b2", 0) # True p.processNext() # "a1" p.getJobState("a1") # "PROCESSING" p.failJob("a1") # "QUEUED", first retry p.getBackoffDelay("a1") # 10 p.getAttempt("a1") # 1 p.getQueueSize() # 2, queue is b2 then a1 p.processNext() # "b2" p.failJob("b2") # "EXHAUSTED", no retries allowed p.getBackoffDelay("b2") # 0 p.processNext() # "a1" p.failJob("a1") # "QUEUED", second retry p.getBackoffDelay("a1") # 20 p.processNext() # "a1" p.failJob("a1") # "EXHAUSTED", third failure with maxRetries 2 p.getAttempt("a1") # 3 p.getJobState("a1") # "EXHAUSTED" p.getBackoffDelay("a1") # 0 p.failJob("a1") # "ERROR", the job is not PROCESSING p.getJobState("zz") # "NOT_FOUND" p.getQueueSize() # 0 ``` Job `a1` runs three times (one first run plus two retries) with delays of 10 and then 20 seconds. Job `b2` has `maxRetries` 0, so its first failure exhausts it. Example 2, with no failures: ```python p = JobProcessor(5) p.processNext() # "" p.addJob("j1", 0) # True p.addJob("j2", 3) # True p.processNext() # "j1" p.completeJob("j1") # True p.getJobState("j1") # "COMPLETED" p.processNext() # "j2" p.completeJob("j2") # True p.getQueueSize() # 0 ``` Processing an empty queue returns `""`. Jobs `j1` and `j2` are added and completed in order, and the queue ends empty. ```hint Draw the state machine List every state and every method that moves a job between states. Which transitions are legal, and what should each method return when it is called on a job in the wrong state? ``` ```hint Count before you compare Decide whether the attempt counter goes up before or after the check against `maxRetries`. Trace a job with `maxRetries` 0 and one with `maxRetries` 2 to confirm how many times each can run. ``` ### Constraints & Assumptions - `baseDelay` is between 1 and 100. - `maxRetries` is between 0 and 10. - Job IDs are non-empty strings of lowercase letters and digits. - At most 10,000 operations are made in total. - `completeJob` and `failJob` are only called on jobs that have been processed through `processNext`, though they must still reject a job that is not `PROCESSING` right now. ### Clarifying Questions to Ask - Can more than one job be `PROCESSING` at the same time, or does the processor hand out one job and wait for its result? - Once a job is `COMPLETED` or `EXHAUSTED`, may the same ID be added again? - Should the delay have a maximum value or random jitter added? - Should exhausted jobs be collected somewhere callers can inspect and replay them? ### What a Strong Answer Covers - The per-job record and the queue structure, and why each operation runs in constant time. - The transitions each method allows and what it returns on an illegal call. - How the attempt counter and the `maxRetries` check combine, verified against `maxRetries` 0. - When the backoff delay is set and reset, and the largest value it can reach under the constraints. - Edge cases: duplicate IDs, unknown IDs, an empty queue and calls on finished jobs. ### Follow-up Questions - How would you make `processNext` skip jobs whose backoff has not elapsed, given the current time? - Several workers call `processNext` concurrently. How do you stop two of them from taking the same job, and what happens to a job whose worker crashes while it is `PROCESSING`? - How would you add jitter and a maximum delay, and what goes wrong without them when many jobs fail at once? - How would you persist the queue so jobs survive a restart, and what delivery guarantee does that give?

Overview: Design a background job processor with a FIFO queue, per-job states and retries that use exponential backoff. It tests modeling a job state machine, counting attempts against a retry limit without off-by-one errors, and keeping every operation constant time.

Shopify logo
Shopify
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a background job processor that manages a task queue and retries failed jobs with exponential backoff.

Jobs are added to the queue with a unique ID and a maximum number of retries. The processor dequeues jobs one at a time for execution, and a processed job is then marked as completed or failed. When a job fails and has retries left, it goes back on the queue with a backoff delay that doubles with each successive failure: baseDelay * 2^(attempt - 1).

A job is always in one of four states: QUEUED, PROCESSING, COMPLETED or EXHAUSTED (all retries used up). The processor can report a job's state, backoff delay and attempt number, and the queue size, at any time.

Implement a class JobProcessor:

  • JobProcessor(baseDelay: int) creates a processor whose first retry waits baseDelay seconds.
  • addJob(jobId: str, maxRetries: int) -> bool adds a new job to the back of the queue. It returns True if the job was added and False if the ID already exists.
  • processNext() -> str removes the job at the front of the queue, moves it to PROCESSING and returns its ID. It returns "" if the queue is empty.
  • completeJob(jobId: str) -> bool moves a PROCESSING job to COMPLETED and returns True . It returns False if the job does not exist or is not PROCESSING .
  • failJob(jobId: str) -> str records a failure for a PROCESSING job. If retries remain, the job goes to the back of the queue with its new backoff delay and the method returns "QUEUED" . If none remain, the job becomes EXHAUSTED and the method returns "EXHAUSTED" . It returns "ERROR" if the job does not exist or is not PROCESSING .
  • getJobState(jobId: str) -> str returns the job's state, or "NOT_FOUND" for an unknown ID.
  • getBackoffDelay(jobId: str) -> int returns the job's current backoff delay in seconds, or -1 for an unknown ID.
  • getQueueSize() -> int returns the number of jobs waiting in the queue.
  • getAttempt(jobId: str) -> int returns the job's current attempt number, or -1 for an unknown ID.

The counters follow these rules:

  • A new job has attempt 0 and backoff delay 0. Every failJob call that does not return "ERROR" increments the attempt number by one.
  • maxRetries is the number of retries allowed after the first run, so a job can run at most maxRetries + 1 times.
  • A failure that leads to a retry sets the delay to baseDelay * 2^(attempt - 1) using the new attempt number. A failure that exhausts the job sets the delay back to 0.
  • The processor has no clock. The delay is recorded for getBackoffDelay to report, and processNext returns queued jobs in first-in, first-out order without waiting.

Example 1, with retries and exhaustion:

p = JobProcessor(10)
p.addJob("a1", 2)          # True
p.addJob("a1", 5)          # False, the ID already exists
p.addJob("b2", 0)          # True
p.processNext()            # "a1"
p.getJobState("a1")        # "PROCESSING"
p.failJob("a1")            # "QUEUED", first retry
p.getBackoffDelay("a1")    # 10
p.getAttempt("a1")         # 1
p.getQueueSize()           # 2, queue is b2 then a1
p.processNext()            # "b2"
p.failJob("b2")            # "EXHAUSTED", no retries allowed
p.getBackoffDelay("b2")    # 0
p.processNext()            # "a1"
p.failJob("a1")            # "QUEUED", second retry
p.getBackoffDelay("a1")    # 20
p.processNext()            # "a1"
p.failJob("a1")            # "EXHAUSTED", third failure with maxRetries 2
p.getAttempt("a1")         # 3
p.getJobState("a1")        # "EXHAUSTED"
p.getBackoffDelay("a1")    # 0
p.failJob("a1")            # "ERROR", the job is not PROCESSING
p.getJobState("zz")        # "NOT_FOUND"
p.getQueueSize()           # 0

Job a1 runs three times (one first run plus two retries) with delays of 10 and then 20 seconds. Job b2 has maxRetries 0, so its first failure exhausts it.

Example 2, with no failures:

p = JobProcessor(5)
p.processNext()            # ""
p.addJob("j1", 0)          # True
p.addJob("j2", 3)          # True
p.processNext()            # "j1"
p.completeJob("j1")        # True
p.getJobState("j1")        # "COMPLETED"
p.processNext()            # "j2"
p.completeJob("j2")        # True
p.getQueueSize()           # 0

Processing an empty queue returns "". Jobs j1 and j2 are added and completed in order, and the queue ends empty.

Constraints & Assumptions

  • baseDelay is between 1 and 100.
  • maxRetries is between 0 and 10.
  • Job IDs are non-empty strings of lowercase letters and digits.
  • At most 10,000 operations are made in total.
  • completeJob and failJob are only called on jobs that have been processed through processNext , though they must still reject a job that is not PROCESSING right now.

Clarifying Questions to Ask Guidance

  • Can more than one job be PROCESSING at the same time, or does the processor hand out one job and wait for its result?
  • Once a job is COMPLETED or EXHAUSTED , may the same ID be added again?
  • Should the delay have a maximum value or random jitter added?
  • Should exhausted jobs be collected somewhere callers can inspect and replay them?

What a Strong Answer Covers Guidance

  • The per-job record and the queue structure, and why each operation runs in constant time.
  • The transitions each method allows and what it returns on an illegal call.
  • How the attempt counter and the maxRetries check combine, verified against maxRetries 0.
  • When the backoff delay is set and reset, and the largest value it can reach under the constraints.
  • Edge cases: duplicate IDs, unknown IDs, an empty queue and calls on finished jobs.

Follow-up Questions Guidance

  • How would you make processNext skip jobs whose backoff has not elapsed, given the current time?
  • Several workers call processNext concurrently. How do you stop two of them from taking the same job, and what happens to a job whose worker crashes while it is PROCESSING ?
  • How would you add jitter and a maximum delay, and what goes wrong without them when many jobs fail at once?
  • How would you persist the queue so jobs survive a restart, and what delivery guarantee does that give?