AlgoMaster Logo

Design a Task Scheduler

Ashish

Ashish Pratap Singh

hard

A Task Scheduler is a system that manages the execution of tasks at predefined times or intervals. It is commonly used in operating systems, distributed systems, and backend services to automate jobs like backups, notifications, report generation, and periodic cleanup tasks.

For example, a task might be scheduled to run once at 8:00 AM, every day at midnight, or 5 minutes after another task completes. The scheduler must ensure these tasks run reliably and at the correct times, even under heavy load or failures.

In this chapter, we will explore the low-level design of a Task Scheduler.

Let’s start by clarifying the requirements:

1. Clarifying Requirements

Before starting the design, it's important to ask thoughtful questions to uncover hidden assumptions and better define the scope of the system.

Here is an example of how a conversation between the candidate and the interviewer might unfold:

After gathering the details, we can summarize the key system requirements.

1.1 Functional Requirements

  • Support one-time task: The system must allow a user to schedule a task (a piece of code) to be executed once at a specific future time.
  • Support recurring tasks: The system must support scheduling a task that runs repeatedly at a fixed interval (e.g., every 5 seconds).
  • Schedule with a delay: The system must allow scheduling a task to run after an initial delay.
  • Execution: The scheduler must execute the tasks at their designated times. Allow concurrent execution of tasks using multiple worker threads

1.2 Non-Functional Requirements

  • Thread-Safety: The scheduler must be safe to use in a multi-threaded environment. Multiple threads might try to schedule tasks concurrently.
  • Efficiency: The scheduler should not burn CPU cycles while waiting for the next task. It should sleep or wait efficiently.
  • Robustness: A long-running task should not block the scheduler from executing other tasks.
  • Extensibility: The design should be open to adding new types of scheduling logic in the future (e.g., CRON-based schedules) without major refactoring.

2. Core Entities and Classes

Premium Content

This content is for premium members only.