KatInterview

Build a Retry-Safe Background Job Service for Heavy Tasks

System DesignModel answer
MediumModel answerSystem Designidempotencymessage-queuesdistributed-systems

Last updated

Design an HTTP service that accepts long-running jobs, returns immediately, deduplicates client retries, and survives worker crashes without repeating side effects.

Design a Background Job Service

Your team needs a service that lets clients start heavy, slow operations over HTTP, for example shrinking very large images or building big reports. The work takes anywhere from a few seconds to several minutes, so the caller cannot wait on the request.

What the service must do

  1. Submit: POST /jobs accepts a task and responds right away with 202 Accepted and a job_id.
  2. Check progress: GET /jobs/{job_id} returns the current state (pending, processing, completed, or failed) and, once finished, the result.
  3. Safe retries: Clients on flaky networks may send the same submission several times. Regardless of how many arrive, exactly one job must be created and run.

Operating constraints

  • Peak load is around 1,000 requests per second.
  • Individual jobs run from seconds to minutes.
  • Worker crashes and service restarts must not lose jobs or corrupt state.

What to cover in your answer

  • The API contract and the data model.
  • The path of one request from submission to its final status update.
  • The exact mechanism that guarantees idempotency.
  • How worker failures and retries are handled without duplicating side effects.
  • Your storage choices for job metadata, status changes, and results.

Be explicit about sizing assumptions and the trade-offs behind your choices.

Hints

Hint 1

Break the 1,000 RPS into submissions versus status polls, then use jobs per second times average duration to estimate how many jobs run concurrently.

Hint 2

Exactly-once delivery is not realistic. Aim for at-least-once delivery combined with processing that is safe to repeat, and let a database uniqueness constraint decide which duplicate submission wins.

Hint 3

Think about two failure windows: saving the job but failing to enqueue it, and a worker that stalls and later wakes up after another worker took over. Look at the outbox pattern, leases with heartbeats, and fencing tokens.