Beginner · About 10 minutes · Distributed Systems Coordination
Two Servers Picked the Same Job
Every morning, an application creates a sales report. Two background workers both start building the same waiting job.
Answers stay in this browser until you save them on this device.
What happened?
Every morning, an application creates a sales report.
Two background workers—Server A and Server B—look for waiting jobs.
Both see “Create report #4821.”
Both start building the same report, wasting time and computing resources.
Each worker thought the job was still available.
A worker is a program that handles tasks in the background. Server A and Server B each run one worker in this story.
Your task
How would you let only one worker claim the report job?
For this exercise
- Both workers use the same saved job record.
- Each worker has a different identifier.
- Workers finish normally after claiming. Automatic recovery is a later extension.
- The report is fictional text. No email, payment or other external action occurs.
- Either worker can win; clicking or starting first does not guarantee it.
Optional hints
- Seeing a waiting job does not reserve it.
- Ask the database to change the job only if it is still waiting.
- Save which worker claimed it, and let only that assigned worker save completion.
Related path: System Design Interview Sprint
Interview answer
Explain it in an interview
Two workers both found report job 4821 and both started building it. How do you let only one claim it?
A fictional sales report is waiting as job 4821. Two background workers read that waiting row and both start the work. I need the database to award the claim to one worker. The other worker must see that the job is no longer waiting.
Step 1
Both workers started job 4821
Each worker looked up job 4821, saw that it was waiting, and began building the report. The check and the update were separate steps, so both checks passed. Two reports would be produced for one job. The race is in the gap between read and write.
Step 2
A process lock does not cross workers
A lock inside one process does not stop the other worker, which has its own database connection. Reading the status and then updating it still races. Treating a repeated claim from the same worker as success would also hide a second attempt. The condition has to live in the update itself.
Step 3
Claim only while the job is waiting
The update matches job 4821 only when its status is still waiting, and it records the assigned worker in that same statement. Only that assigned worker can complete the job. The report text, the completion time, and the complete status save together. A second completion conflicts and cannot overwrite the saved result.
Step 4
Commit the claim before the work counts
The claim uses an explicit transaction, so a rollback leaves the job waiting. Once the claim commits, the winner is stored, and a restart does not silently reopen a running job. I read the stored state instead of assuming my process still owns the work. A same-worker claim replay also conflicts.
Step 5
A crash after claim leaves it running
If the winner stops after the claim commits, job 4821 stays running, because this lab has no lease expiry, heartbeat, or takeover. That is different from a crash before commit, which leaves the job waiting. I do not automatically resubmit a claim or a completion. I inspect the row.
Step 6
One winner is not exactly-once work
One SQLite file on one computer picked a single claimant for job 4821. Worker ids are labels the caller supplies, not authenticated identities. There is no queue broker, no fairness guarantee, and no exactly-once execution. This API does not hand the loser a different job.
Do not stop here
I would let each worker check that job 4821 is waiting and then update it, using a lock inside the worker process.
Say this boundary
I claim job 4821 with an update that matches only the waiting row, and a committed claim stays running if that worker later disappears.
Leave these claims out
- Exactly-once report execution
- The worker id is authenticated
- A crashed worker's job returns to waiting by itself
I would show one worker getting the claim and the other getting a conflict on job 4821. A crash after commit is a stuck running job, not a free one.
Now say the same idea in your own words, using the story from this challenge.