Design a job scheduler 00:00:00 Make a prediction You will design a deadline queue that wakes for newly earlier work and explain fixed-rate versus fixed-delay scheduling. A scheduler is sleeping until ten. At nine someone schedules a new job for nine thirty. What must happen or the new job starts thirty minutes late? Wake the waiting scheduler, re-evaluate the earliest deadline and sleep only until the new head is due. An ordered queue by itself cannot notify a sleeping thread. 00:00:31 How one rooster keeps a whole farm on time A rooster keeper keeps a list of when to wake each farmhand. If an earlier appointment is written down without telling the sleeping keeper, the new time is missed even though the list is sorted. Couple the deadline structure with a notification mechanism. Run user work outside the scheduling lock so a slow job cannot prevent fresh arrivals. A job scheduler keeps promises about time. You give it a piece of work and a rule, and it runs the work at the right moments, until you cancel it. Programs are full of later: send the reminder, refresh the token, give up after 30 seconds, write the report at night. Without a scheduler each of them gets its own thread with its own sleep. 00:01:14 Wake for an earlier deadline A scheduler waits for a job due at ten o'clock. At nine, another caller adds a job due at nine thirty. Store jobs ordered by due time and signal the waiting scheduler when a new earliest deadline appears. On waking, recheck the top and current time. Dispatch the due job to a worker without holding the deadline-queue lock while user code runs. A slow job must not prevent insertion of another deadline. Let the timer worker sleep until ten past twelve, then insert a job due at two minutes past twelve. Correct insertion wakes the worker to recompute its earliest deadline. Simply pushing the job into the heap would sort it correctly while still running it eight minutes late. 00:02:04 Fixed rate and fixed delay differ A task takes four seconds and its interval is ten seconds. A fixed-delay schedule waits ten seconds after completion. A fixed-rate schedule targets starts on an underlying ten-second timeline. State how you handle overruns instead of accidentally piling up overlapping tasks. A job starts at zero and finishes at four seconds. With a ten-second fixed rate, its next scheduled start is ten. With ten seconds of fixed delay after completion, the next start is fourteen. Ask what to do about missed runs before handling jobs slower than their interval. A job running past its next fixed-rate target can overlap, queue or skip depending on policy. A name like periodic does not settle that choice. For work that should wait after each completion, fixed delay avoids accidental overlapping runs. Java’s scheduled executor can run work periodically. A task that runs longer than its configured period raises a question about how far later starts can slip. Python schedulers expose policies for missed runs and simultaneous instances. A time formula alone does not specify what should happen when the worker falls behind. 00:03:18 Try an interview follow-up How do you cancel a job in the heap? I do not search. The job has one valid ticket number, cancel sets it to 0, and an alarm with another number is thrown away at the top. If many are cancelled, I rebuild the heap when more than half is stale, which costs a constant for each cancel. A worker claims a job and completes an external action but crashes before acknowledging it. When the lease expires a second worker may run it again. Give the action an idempotency key or another durable deduplication boundary. Trace recovery to show the difference between retrying a job and repeating its effect. 00:04:03 What to take with you Jobs in a map, alarms in a heap, one condition for both. A worker throws away stale alarms, takes what is due, or sleeps for the gap. Every push and every take that leaves alarms behind sends a signal. The task runs without the lock. Afterwards the scheduler decides: another try, the next run, or the end. When and what after a failure are separate parts, and the clock is an argument.