Design Task Scheduler

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-04-22

Problem Statement

Design and implement a Task Scheduler system that can schedule, execute, and manage tasks/jobs at specific times or after certain intervals. The system should support one-time and recurring tasks, concurrent execution, retries, and monitoring of task status.


🔶 Functional Requirements


1.Task Scheduling
•Schedule tasks to execute at a specific time or after a delay.
•Support recurring tasks with fixed intervals or cron-like expressions.
2.Task Execution
•Execute tasks at their scheduled time.
•Support concurrent execution of multiple tasks.
•Handle thread-safety for shared resources.
3.Task Management
•Cancel or reschedule a task before execution.
•Query task status: Scheduled, Running, Completed, Failed.
•Retry tasks on failure with configurable retry policy.
4.Persistence & Recovery
•Persist tasks to survive system restarts.
•Restore pending tasks on application restart.
5.Monitoring & Logging
•Log task execution start, completion, and failure.
•Monitor execution time, errors, and system load.


🔶 Non-Functional Requirements


•Concurrency: Support multiple threads to execute tasks in parallel.
•Scalability: Handle thousands of tasks scheduled simultaneously.
•Reliability: Ensure tasks execute at least once, even after crashes.
•Latency: Minimize delay between scheduled time and actual execution.
•Extensibility: Easy to add new types of tasks or scheduling strategies.


🔶 Constraints


•Tasks may be CPU-bound or IO-bound; scheduler should handle them efficiently.
•Maximum number of concurrent threads should be configurable.
•Tasks must not interfere with each other if accessing shared resources.


🔶 Example Use Cases


1.Schedule a one-time email to be sent 2 hours later.
2.Schedule a recurring data backup every day at 2 AM.
3.Retry a failed job up to 3 times with exponential backoff.
4.Cancel a scheduled notification before it executes.


👉 In short:

Design a Task Scheduler system that can schedule, execute, manage, and monitor one-time or recurring tasks with support for concurrency, retries, persistence, and fault-tolerance.

Solution

Comments