Skip to main content

Classic design · Senior

Distributed task scheduler

Classic interview design. Key points: job scheduling and distributed locks.

16 min read · Content reviewed on October 22, 2026

Short answer

Classic interview design. Key points: job scheduling and distributed locks. A job scheduler is a program that automatically runs jobs at set times or intervals. A job is an instance of a task and is made up of the task, the schedule, and the parameters to run it.

01 · CONTEXT AND REQUIREMENTS

What a job scheduler needs to do

A job scheduler is a program that automatically runs jobs at set times or intervals. A job is an instance of a task and is made up of the task, the schedule, and the parameters to run it.

There are two core functional requirements: schedule jobs for immediate execution, at a future date, or on a recurring basis, and let the user track their status.

The Hello Interview guide suggests assuming 10k jobs per second and lists three deep-dive questions: run the job within 2s of the scheduled time, scale to that throughput, and guarantee at-least-once execution.

02 · THE BASIC DESIGN

From the API to the worker

This article's design separates four responsibilities: the API accepts and queries jobs, the database holds the state, the scheduler decides what is due, and the workers execute. Between the scheduler and the workers sits a fila for execution.

  1. The client asks the API to schedule a job: immediate, at a future date, or recurring.

    Architecture diagram: Scheduler: from request to execution
    CLClient
    CPJobs API
    DBJobs database
    CPAgendador
    QExecution queue
    CPWorkers
  2. The API writes the job to the database with the task, the schedule, and the execution parameters.

    Architecture diagram: Scheduler: from request to execution
    CLClient
    CPJobs API
    DBJobs databasejob created
    CPAgendador
    QExecution queue
    CPWorkers
  3. The scheduler queries the database for jobs whose time has come.

    Architecture diagram: Scheduler: from request to execution
    CLClient
    CPJobs API
    DBJobs database
    CPAgendador
    QExecution queue
    CPWorkers
  4. The scheduler puts each due job on the execution queue.

    Architecture diagram: Scheduler: from request to execution
    CLClient
    CPJobs API
    DBJobs database
    CPAgendador
    QExecution queuejob on the queue
    CPWorkers
  5. A worker takes the job off the queue, runs the task, and records the status in the database, which is what the user queries.

    Architecture diagram: Scheduler: from request to execution
    CLClient
    CPJobs API
    DBJobs database
    CPAgendador
    QExecution queue
    CPWorkers

03 · AT-LEAST-ONCE EXECUTION

When the worker dies midway through the job

In a fila like SQS, if the message is not deleted before the visibility timeout ends, it becomes visible again and another consumer can pick it up.

This covers application failures, crashes, and connectivity problems, and ensures the message is not lost even if the first processing attempt fails. The timeout can be extended programmatically with ChangeMessageVisibility.

The cost is duplication: the SQS documentation does not guarantee that a message will not be delivered more than once within the visibility timeout. Cloud Scheduler also promises at-least-once execution: the job runs at least once per scheduled run and, in rare circumstances, may run multiple times for the same schedule instance.

What it guaranteesWhat it does not guarantee
Queue with a visibility timeoutThe message becomes visible again if it is not deleted before the timeoutDoes not guarantee that the message is not delivered more than once
Cloud SchedulerThe job runs at least once per scheduled runMay run multiple times for the same schedule instance
Kubernetes CronJobCreates a Job approximately once per scheduled timeMay create two Jobs or none

04 · MORE THAN ONE SCHEDULER

The database as referee

Airflow supports more than one scheduler at the same time, both for performance and for resilience, and the high-availability scheduler makes use of the existing metadata database.

  1. Two schedulers read the same metadata database, with no direct channel between them.

    Architecture diagram: Scheduler: two schedulers and a database as referee
    CPAgendador A
    CPAgendador B
    DBMetadata database
    QExecution queue
    CPWorkers
  2. Only one scheduler at a time can be in the critical section of the scheduling loop.

    Architecture diagram: Scheduler: two schedulers and a database as referee
    CPAgendador A
    CPAgendador B
    DBMetadata database
    QExecution queue
    CPWorkers
  3. The database enforces this rule with row locks (SELECT ... FOR UPDATE).

    Architecture diagram: Scheduler: two schedulers and a database as referee
    CPAgendador A
    CPAgendador B
    DBMetadata databaserow lock
    QExecution queue
    CPWorkers
  4. Whoever is in the critical section selects what is ready, respecting concurrency limits, and enqueues it for execution.

    Architecture diagram: Scheduler: two schedulers and a database as referee
    CPAgendador Acritical section
    CPAgendador B
    DBMetadata database
    QExecution queue
    CPWorkers

This is one way to coordinate several schedulers: database locks guarantee that only one of them enters the critical section.

Another way is to elect a leader by lease, the topic of the next section.

05 · LEASES AND FENCING

One leader at a time, even with pauses

A lease is a mechanism for locking a shared resource and coordinating activity among the members of a set. Kubernetes uses it to keep only one instance of a component active, with the others on standby.

The lease's expiry keeps a client that has crashed from holding the lock forever. But if a client pause lasts longer than the expiry and it doesn't notice that the lease has expired, it may make an unsafe change.

  1. One scheduler acquires the lease and acts as leader; the other instance stays on standby.

    Architecture diagram: Scheduler: leader with lease and fencing
    CPAgendador Aleader
    CPAgendador Bon standby
    DBLease store
    DBJobs database
  2. The lease has an expiry, so that a leader that has crashed does not hold the lock forever.

    Architecture diagram: Scheduler: leader with lease and fencing
    CPAgendador A
    CPAgendador B
    DBLease storeprazo
    DBJobs database
  3. The leader may pause for longer than the expiry, without noticing that the lease expired, and still try to write.

    Architecture diagram: Scheduler: leader with lease and fencing
    CPAgendador Apausado
    CPAgendador B
    DBLease store
    DBJobs database
  4. The other scheduler acquires the lease and receives a token larger than the first one's.

    Architecture diagram: Scheduler: leader with lease and fencing
    CPAgendador A
    CPAgendador Bhigher token
    DBLease store
    DBJobs database
  5. When the old leader comes back and writes with the smaller token, the database, which remembers the largest token already processed, rejects the write.

    Architecture diagram: Scheduler: leader with lease and fencing
    CPAgendador Aold token
    CPAgendador B
    DBLease store
    DBJobs databasechecks the token

06 · WHEN THE TIME PASSES

Delays and overlapping runs

Kubernetes CronJob has two rules for scheduled times that pass. If the start deadline is missed, it skips that instance and the following ones remain scheduled. With the Forbid policy, if the previous run has not finished yet, the new run is skipped.

Frequently asked questions

What is Distributed task scheduler?

Classic interview design. Key points: job scheduling and distributed locks.

What does the Distributed task scheduler design cover?

It is organized in these sections: What a job scheduler needs to do, From the API to the worker, When the worker dies midway through the job, The database as referee, One leader at a time, even with pauses, and Delays and overlapping runs.

How long is Distributed task scheduler and what level is it?

It takes about 16 minutes to read and is rated Senior level.

Now design it yourself

Run this case as an interview in the System Design school: you make the decisions, the tutor challenges them and you see how your design scores.