AlgoMaster Logo

Design a Task Scheduler

Ashish

Ashish Pratap Singh

hard

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.