BayLearnBayAreaLa8s

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.

Two workers, one waiting reportReport #4821WaitingCreate daily sales reportServer AWorkerServer BWorker
Two workers find the same report job. Both start building it. How would you let only one claim it?
Both workers see “Create report #4821.” Each thought the job was still available.

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
  1. Seeing a waiting job does not reserve it.
  2. Ask the database to change the job only if it is still waiting.
  3. Save which worker claimed it, and let only that assigned worker save completion.
0/4000
0/4000
0/4000

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.

How to walk the interviewer through the answer1Both workers started job 48212A process lock does not cross workers3Claim only while the job is waiting4Commit the claim before the work counts5A crash after claim leaves it running6One winner is not exactly-once work
Walk these steps in order. Each box is one thing to say before you move on.
  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

Interviewers listen for the boundary between the tempting shortcut and the rule you actually enforce.

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.