Inside a Task Scheduler 00:00:01 Follow one timed lighting cue through the theater A theater schedules a lighting cue for ten o'clock. A durable task row describes it, a time index finds when it is due, and a ready queue offers the work to a stage technician. The client calls the submit service. At 10:00 the dispatcher asks for ids with a score up to now. The row becomes running with a lease and token 1. A duplicate message would get nothing. On failure the row and the sorted set get a later time. The durable row is the authority for whether a claim may proceed. Index entries, dispatch messages and worker wakeups may repeat while the cue still has only one current attempt. 00:00:42 Drop the message between the clock and the queue A dispatcher removes a due identifier and crashes just before sending it. The scheduler must recover this gap, because the row still represents a promise to run the cue. The id leaves the due set. The dispatcher dies before it sends. t1 is in no queue. The next dispatcher sends it again. The claim lets one run. Every minute the sweeper adds overdue scheduled rows again. It also rebuilds Redis after a loss. In this example a sweeper rebuilds missing index entries from overdue durable rows. A transactional outbox is another way to persist the handoff. Pick a design whose crash points can all be recovered. 00:01:26 The old technician wakes up late Worker A reserves the task with a lease and attempt token. After its lease expires, B can claim a newer token. Let both try to finish and watch the conditional update on the durable row. One atomic step: running, until second 30, token 1. The task is not scheduled any more. Token 2. It reports with token 1 and is refused. B finishes with token 2. A stale token can be rejected by the scheduler. When the physical lighting controller itself can be affected twice, it must enforce ordering or accept a deduplicated cue identifier as well. 00:02:06 Decide who gets on stage first Time determines eligibility, and priority decides how eligible jobs compete. Combining both into one poorly chosen numeric score can make priority meaningful only for tasks due in the same instant. Due 10:00:00.000 with priority 5. The same formula with seconds. Priority decides only between tasks due within the same second. It hardly does anything. The time index finds due tasks. Each goes into the queue of its level. Use the due-time index to release ready work into explicit priority queues. Define fairness and aging so one stream of urgent tasks cannot indefinitely postpone another required cue. 00:02:52 The handler that ignores its timeout The scheduler marks a hung task as failed, but its handler keeps the worker slot and continues making side effects. More retries can soon exhaust the whole theater crew. One slot is held for ever. Then another. The task is marked failed, but the handler still runs. Retries pile up beside it. A cancel signal reaches the handler. The slot is free and the retry waits its turn. Propagate deadlines, bound work at the executing process and design idempotent effects. Verify the scheduler by killing it at each handoff and asking how one acknowledged cue ends in a visible terminal state.