Design a Task Scheduler system that allows clients to schedule tasks to be executed at a specified time.
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:
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:
Candidate: Should the scheduler support recurring tasks or only one-time scheduled tasks?
Interviewer: Good question. For this version, let’s support both one-time and recurring tasks (e.g., every 5 minutes).
Candidate: Should tasks be executed exactly on time, or is a small delay acceptable?
Interviewer: A small delay is acceptable. We're not aiming for real-time precision, but tasks should execute as close as possible to the scheduled time.
Candidate: Should the scheduler support retries if a task fails?
Interviewer: No retries for now. However, you can keep the design open to supporting retries later.
Candidate: Can a task have dependencies, such that one task should only start after another completes?
Interviewer: Let's not handle task dependencies in this version. All tasks are independent.
Candidate: Can multiple tasks run in parallel?
Interviewer: Yes. The system should be able to run multiple tasks concurrently using a configurable number of worker threads.
After gathering the details, we can summarize the key system requirements.