3/28
Part 1 of 6 · A queue and its holdersLearn and do

A reservation hides a task, and only its holder can finish it

Why not pop the task and forget about it? Because workers crash. If the queue forgets a task the moment a worker takes it, a crash loses that task for good.

So reserve doesn't delete. It moves the task from waiting to reserved and writes down who holds it. The task is hidden from every other worker, but the queue still knows about it. Later the holder sends an ack, short for acknowledgment: the work is done, and the queue can mark it finished.

That needs two dicts:

DictMapsExample
statustask id to its state{"a": "reserved", "b": "waiting"}
holdera reserved task to its worker{"a": "w1"}

An ack counts only when it comes from the holder. Any other ack returns False and changes nothing:

  • another worker's ack, because it never had the task
  • a second ack from the holder, because the task is already done
  • an ack for an id the queue never saw

Why False and not an exception? In a real system, acks arrive late, twice, or from a worker that lost its task. Those are normal events. A boolean lets the worker log it and move on.

Predict

w1 reserves a. Then w2 calls ack("w2", "a"). What should happen?

Pick an answer above to see the rest