AlgoViz

Design a job scheduler

hard 80 min

Build a library that runs jobs later and again in one process: a heap of alarms, workers that sleep for exactly the gap, cancel without a search, retries that are alarms, and tests without a single sleep.

Watch lesson4:30

Where it shows up

The same pattern appears across everyday software.

  • Java ScheduledThreadPoolExecutor

    If a run takes longer than its period, later runs may start late but never at the same time as it. Cancelled tasks stay in the queue by default until their time; `setRemoveOnCancelPolicy` removes them at once. Both are questions of this page.

  • APScheduler for Python

    Job options `coalesce`, `max_instances` and `misfire_grace_time`: the three questions about missed and overlapping runs, as three settings.

  • Quartz

    Each trigger has a misfire instruction: what to do when its moment passed because the scheduler was down or no thread was free. The default is a smart policy that depends on the type.

  • Go runtime

    Each processor of the runtime has its own heap of timers. Stopped timers are marked and counted as zombies, and cleaned later: lazy removal in a language runtime.

  • Netty HashedWheelTimer

    Made for approximate input and output time limits. At each tick it looks for timers that are behind and runs them. Its documentation names the paper by Varghese and Lauck.

  • Python asyncio

    `call_later` schedules a callback. The event loop uses a monotonic clock, so a change of the system time does not move it.

  • Kubernetes CronJob

    `concurrencyPolicy` can be Allow, Forbid or Replace, and `startingDeadlineSeconds` limits how late a missed run may start. Overlap and grace, for containers.

  • systemd timers

    With `Persistent=` true the last trigger time is stored on disk, and a run that was missed while the timer was inactive starts at once when it is activated.

  • Celery beat

    Sends tasks to workers at intervals. The manual asks for a single scheduler at a time for a schedule, otherwise tasks are duplicated.