Skip to the guide

Scheduling work for later: delayed jobs, cron at scale and leases

IntermediatePattern5 pages

A florist takes orders for future dates, a birthday next month or an anniversary in March, and standing orders such as flowers to an office every Friday. Each goes into a diary on its day's page. Each morning a driver writes her name and the time beside an order before loading it, so no two drivers take the same one. An order signed out at nine with no delivery note by noon goes to another driver. A standing order isn't copied onto every Friday for years; next week's is written in when this week's goes out.

The orders are delayed jobs, such as "remind this user in three days", and the diary is a jobs table indexed by due time. The drivers are workers that claim due jobs, signing out is a claim with a lease, and the noon rule is the lease expiring so a stuck job goes to someone else. Standing orders are cron schedules, each run created as it falls due. Interviewers ask this in notification, crawler and scheduler questions, and listen for what happens when many jobs fall due at once, or their machine dies. The move: keep due work as rows indexed by time, let any worker claim due rows under a lease, create each run of a schedule exactly once, and make every job safe to run twice.