Design a distributed task scheduler for delayed and recurring tasks

Quick Overview

Design a distributed task scheduler that accepts one-off delayed tasks and recurring cron tasks and starts each one within 200 ms of its scheduled time, with bursts of 50,000 executions per second. It tests how you find due tasks quickly, recover from worker crashes, keep duplicate runs harmless and scale the storage layer.

Design a distributed task scheduler for delayed and recurring tasks

Role: Software Engineer

Category: System Design

Difficulty: hard

Interview Round: Other

Design a distributed task scheduler that allows users to submit, execute and manage one-off and recurring tasks. The system must support the following: 1. Users can submit one-off delayed tasks or recurring tasks (for example, cron expressions). 2. The system executes the provided task payload (an HTTP webhook, a script, or a message push) at the scheduled time. 3. Users can query the state of a task (Pending, Running, Completed, Failed, Canceled) or cancel an upcoming task. 4. The system stores the output and execution logs of completed tasks for auditing. ```hint Two separate paths Accepting a task and running it have different load patterns and different failure modes. Treat them as separate paths, and decide early where the durable record of a task lives. ``` ### Constraints & Assumptions - 10,000+ task submissions per second, with execution bursts of 50,000+ tasks per second. - 99.99% availability for task submission and for routing tasks to execution. - No data loss: once a task is acknowledged as accepted, it is stored durably and is guaranteed to run. - A task begins execution within 200 ms of its scheduled time. - The task store grows to billions of rows, and one recurring task can produce millions of execution log records over its life. ### Clarifying Questions to Ask - Which tasks need the 200 ms start bound, and can the others tolerate a small random delay? - Is it acceptable for a task to run twice, and can webhook targets deduplicate repeated calls? - How long can a single task run, and what retry policy applies when it fails? - Is the system multi-tenant, and must one tenant's burst be kept from delaying the others? - How long must execution history be kept, and how will users read it? ### Part 1 — Requirements, data model and API Confirm the non-functional requirements and say which of them drive the design. Then define the core entities and the states a task goes through, choose where each kind of data is stored, and outline the API for submitting a task, reading its status, canceling it and reading its execution history. ```hint Task versus run A recurring task runs many times, and each run has its own state, output and retries. Decide what belongs to the task and what belongs to a single run, and which of that data needs transactions. ``` #### What This Part Should Cover - Which non-functional requirements shape the design most, and how they rank against each other - The entities for a task's configuration and for a single run, and the states a run moves through - A store for each kind of data (definitions and state, the timer, the work queue, logs), with a reason for each - The endpoints with their inputs and outputs, and what happens when a client retries a submission ### Part 2 — High-level design Describe the components that accept tasks and run them on time, and trace one task from submission to a finished run. Keep precision, failure handling and storage scaling at a high level for now. ```hint Finding due tasks Picture every scheduler node running `SELECT ... WHERE execute_time <= NOW()` each second against billions of rows. Ask what smaller, faster source of "due now" tasks could take that load. ``` ```hint Bursts At the top of the hour, 50,000 tasks can come due in one second. Decide what absorbs that burst while workers are busy, and how one tenant's burst is kept from delaying everyone else. ``` #### What This Part Should Cover - The submission path and the execution path, and where the durable record of a task is written - How the system learns that a task is due without scanning the main database - How triggered tasks reach the workers, and how a burst of due tasks is absorbed - How one tenant's burst is kept from delaying other tenants ### Part 3 — Start tasks within 200 ms of their scheduled time Explain the techniques and components that let the system start tasks within 200 ms of their scheduled time, without overloading the database and without two scheduler nodes firing the same task. ```hint Read the database early The database can be read in predictable batches minutes before tasks are due. Look for a structure that can answer "what is due now?" many times per second, and work out the cost of that query. ``` ```hint Two nodes, one task If two scheduler nodes see the same due task in the same poll, both will fire it. Find a way to make reading the due tasks and claiming them one atomic step. ``` #### What This Part Should Cover - How, and how often, tasks that are due soon are moved out of the database ahead of time - The data structure that answers "what is due now?" and the cost of that query - How concurrent scheduler nodes avoid firing the same task, and what happens when a node crashes during the handoff - How load spikes at popular times such as midnight are handled ### Part 4 — Worker crashes and duplicate executions A worker can crash halfway through a task, and a lost acknowledgment can hide a run that actually succeeded. Explain how the system ensures every accepted task runs at least once, and how it reduces duplicate executions and keeps the ones that still happen harmless. A payment task, for example, must not charge a card twice. ```hint Between delivery and success Consider what the queue should do with a message from the moment a worker receives it until the worker reports success, and what should happen if that report never arrives. ``` ```hint Assume repeats Assume a run will sometimes be delivered twice. Ask what the database can check before the payload runs, and what the downstream target can check when the call arrives. ``` ```hint A check that blocks recovery Make sure the check that rejects a duplicate run does not also reject the retry of a run whose worker crashed. ``` #### What This Part Should Cover - What the queue does with a message between delivery and acknowledgment, including for long-running tasks - How a crashed worker's task becomes available again, and how quickly - How a worker claims a run so two workers never run it at the same time while a crashed run can still be retried - How downstream systems recognize and ignore a repeated call ### Part 5 — Scale the storage layer Explain the techniques for scaling the storage layer (PostgreSQL and Redis) to handle 10,000+ task submissions per second while the scheduling components keep reading upcoming tasks from it. ```hint Follow the hot spots After choosing partition keys, check where the writes of one very large tenant land, and where all the tasks due in the current minute land. ``` #### What This Part Should Cover - How the relational write load is split, and which partition key is used - How old runs are removed without slowing the active tables - How the in-memory timer is split across nodes so the current minute's tasks do not all sit on one node - The new problems partitioning brings, such as a single very large tenant ### What a Strong Answer Covers - How an accepted task survives the loss of any one component (the in-memory timer, a scheduler node, a worker) and still runs - How due tasks are found and started within the precision target at burst rate, without scanning the main database - The execution guarantee the system offers, and how duplicate runs are made rare and harmless - A store for each kind of data, and a partitioning scheme that spreads the hottest reads and writes - How recurring schedules turn into individual runs, and how a cancel reaches every layer ### Follow-up Questions - How do recurring tasks create their next run, and what should happen to runs missed during an outage? - A Redis failover drops the last second of writes. How does the system find and recover the runs it lost? - How would you add retries with backoff, and a dead-letter queue for tasks that keep failing? - How would you run user-supplied scripts safely?

Overview: Design a distributed task scheduler that accepts one-off delayed tasks and recurring cron tasks and starts each one within 200 ms of its scheduled time, with bursts of 50,000 executions per second. It tests how you find due tasks quickly, recover from worker crashes, keep duplicate runs harmless and scale the storage layer.

Nov 24, 2026
hardSoftware EngineerOtherSystem Design
0
0

Design a distributed task scheduler that allows users to submit, execute and manage one-off and recurring tasks.

The system must support the following:

  1. Users can submit one-off delayed tasks or recurring tasks (for example, cron expressions).
  2. The system executes the provided task payload (an HTTP webhook, a script, or a message push) at the scheduled time.
  3. Users can query the state of a task (Pending, Running, Completed, Failed, Canceled) or cancel an upcoming task.
  4. The system stores the output and execution logs of completed tasks for auditing.

Constraints & Assumptions

  • 10,000+ task submissions per second, with execution bursts of 50,000+ tasks per second.
  • 99.99% availability for task submission and for routing tasks to execution.
  • No data loss: once a task is acknowledged as accepted, it is stored durably and is guaranteed to run.
  • A task begins execution within 200 ms of its scheduled time.
  • The task store grows to billions of rows, and one recurring task can produce millions of execution log records over its life.

Clarifying Questions to Ask Guidance

  • Which tasks need the 200 ms start bound, and can the others tolerate a small random delay?
  • Is it acceptable for a task to run twice, and can webhook targets deduplicate repeated calls?
  • How long can a single task run, and what retry policy applies when it fails?
  • Is the system multi-tenant, and must one tenant's burst be kept from delaying the others?
  • How long must execution history be kept, and how will users read it?

Part 1 — Requirements, data model and API

Confirm the non-functional requirements and say which of them drive the design. Then define the core entities and the states a task goes through, choose where each kind of data is stored, and outline the API for submitting a task, reading its status, canceling it and reading its execution history.

What This Part Should Cover Guidance

  • Which non-functional requirements shape the design most, and how they rank against each other
  • The entities for a task's configuration and for a single run, and the states a run moves through
  • A store for each kind of data (definitions and state, the timer, the work queue, logs), with a reason for each
  • The endpoints with their inputs and outputs, and what happens when a client retries a submission

Part 2 — High-level design

Describe the components that accept tasks and run them on time, and trace one task from submission to a finished run. Keep precision, failure handling and storage scaling at a high level for now.

What This Part Should Cover Guidance

  • The submission path and the execution path, and where the durable record of a task is written
  • How the system learns that a task is due without scanning the main database
  • How triggered tasks reach the workers, and how a burst of due tasks is absorbed
  • How one tenant's burst is kept from delaying other tenants

Part 3 — Start tasks within 200 ms of their scheduled time

Explain the techniques and components that let the system start tasks within 200 ms of their scheduled time, without overloading the database and without two scheduler nodes firing the same task.

What This Part Should Cover Guidance

  • How, and how often, tasks that are due soon are moved out of the database ahead of time
  • The data structure that answers "what is due now?" and the cost of that query
  • How concurrent scheduler nodes avoid firing the same task, and what happens when a node crashes during the handoff
  • How load spikes at popular times such as midnight are handled

Part 4 — Worker crashes and duplicate executions

A worker can crash halfway through a task, and a lost acknowledgment can hide a run that actually succeeded. Explain how the system ensures every accepted task runs at least once, and how it reduces duplicate executions and keeps the ones that still happen harmless. A payment task, for example, must not charge a card twice.

What This Part Should Cover Guidance

  • What the queue does with a message between delivery and acknowledgment, including for long-running tasks
  • How a crashed worker's task becomes available again, and how quickly
  • How a worker claims a run so two workers never run it at the same time while a crashed run can still be retried
  • How downstream systems recognize and ignore a repeated call

Part 5 — Scale the storage layer

Explain the techniques for scaling the storage layer (PostgreSQL and Redis) to handle 10,000+ task submissions per second while the scheduling components keep reading upcoming tasks from it.

What This Part Should Cover Guidance

  • How the relational write load is split, and which partition key is used
  • How old runs are removed without slowing the active tables
  • How the in-memory timer is split across nodes so the current minute's tasks do not all sit on one node
  • The new problems partitioning brings, such as a single very large tenant

What a Strong Answer Covers Guidance

  • How an accepted task survives the loss of any one component (the in-memory timer, a scheduler node, a worker) and still runs
  • How due tasks are found and started within the precision target at burst rate, without scanning the main database
  • The execution guarantee the system offers, and how duplicate runs are made rare and harmless
  • A store for each kind of data, and a partitioning scheme that spreads the hottest reads and writes
  • How recurring schedules turn into individual runs, and how a cancel reaches every layer

Follow-up Questions Guidance

  • How do recurring tasks create their next run, and what should happen to runs missed during an outage?
  • A Redis failover drops the last second of writes. How does the system find and recover the runs it lost?
  • How would you add retries with backoff, and a dead-letter queue for tasks that keep failing?
  • How would you run user-supplied scripts safely?