System Design · Failure recovery · Advanced
Accepted—but never processed?
Illustrative walkthrough written for this guide. This is an authored example, not a recorded AI grading result.
Scenario and requirements
A report-generation API accepts jobs through a database and a queue. Explain how an accepted job can still be found and processed after a server or worker restarts.
Key requirements
202 means accepted, not complete. Saved jobs must be found after restart. The retry deadline is 24 hours from first acceptance; request deduplication is already atomic.
Public requirements
- 202 means accepted. A job is ready only when its report file has finished generating.
- After a restart, every accepted job must remain discoverable until it succeeds or reaches an observable final failure.
- Transient failures may be retried for 24 hours from the original acceptance time. Restarting must not extend that deadline.
- The database already guarantees atomic job creation and deduplication by organization ID and request key.
- The current worker reads only the queue. It has no path to rediscover unprocessed jobs in the database.
- The generation API is idempotent for repeated calls with the same job ID throughout the retry period.
Initial answer
If a job with the same request key already exists, return 202 with its existing job ID. For a new request, save a pending job in the database, publish it to a durable queue, then return 202. The worker generates the file and retries transient failures.
Feedback and evidence
The job is saved, but there is no recovery path to make it runnable if it never reaches the queue.
- From the answer
return 202 with its existing job ID
- Why it mattersReturning the existing ID is valid. The missing piece is how the stored job gets discovered and executed after an interruption.
- From the answer
save a pending job in the database, publish it to a durable queue
- Why it mattersDurable storage and a durable queue are useful, but the server can stop between these two actions. A worker that reads only the queue cannot find a job left only in the database.
- From the answer
retries transient failures
- Why it mattersRetrying queued work is useful. It does not recover work that was never queued, and retries still need the original 24-hour deadline.
Trace the interruption
- The database saves the pending job.
- The server stops before publishing to the queue.
- The client resends the same request.
- The API returns 202 with the existing job ID.
- The job remains only in the database, where the current worker cannot find it.
Improved answer
Have the worker find unfinished jobs in the database, so a job saved before a crash can still run after restart.
The API saves a pending job and returns 202. The worker finds pending jobs and takes over work left behind when a previous worker stops and its allotted processing time runs out.
Only the worker currently responsible for a job may change its status. A previous worker returning late must not overwrite a newer attempt or a final result.
Call the generation API with the same job ID on every attempt. Record completion on success and reschedule transient failures. Once 24 hours have passed since the original acceptance time, record a final failure and its reason. A restart never resets the deadline.
Alternatives and boundaries
Different implementations can be valid when they meet the same public requirements.
How do recovery and state updates stay safe?
A processing claim can have a time limit, often called a lease. Record a token that identifies the current execution when a worker claims or reclaims the job. Accept a state change only if its token still matches the active claim. This prevents an older worker from overwriting a newer execution or a final state.
The processing claim's expiry and the retry deadline serve different purposes. Expiry allows another worker to recover abandoned work. The retry deadline stays fixed at 24 hours after the first acceptance, regardless of restarts or new claims.
Reusing the same job ID relies on the generation API's stated idempotency guarantee throughout that retry period. Database job creation and request deduplication are already atomic in this problem.
An equivalent approach can save the job and a publication record in one database transaction, then have a relay recover unpublished records. This is commonly called an outbox. Explain the recovery path; using the pattern name is not itself a requirement.
Proposed checks
These are proposed checks for this example, not reported test results.
- Stop the server immediately after saving a job and verify that a worker rediscovers it after restart.
- Let an older worker return after a new execution has taken over and verify that its state update is rejected.
- Restart during retries and verify that the original acceptance time and 24-hour deadline remain unchanged.