Design a Job Scheduler (with recurring/one-time jobs & callback support)
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-04-04
Problem Statement
Design and implement a distributed job scheduler system that can schedule and execute tasks at a specified time in the future. The system should support:
1.<span></span>One-time jobs (execute once at a given timestamp).
2.<span></span>Recurring jobs (cron-like schedules).
3.<span></span>Callback support (invoke an HTTP endpoint or a registered handler when a job completes).
The system must be scalable, fault-tolerant, and performant while ensuring jobs execute at (or close to) their scheduled time.
Requirements
Functional
1.<span></span>Job Submission
2.<span></span>Create one-time or recurring jobs with metadata (jobId, schedule time/cron expression, callback URL, payload).
3.<span></span>Update or cancel existing jobs.
4.<span></span>Job Execution
5.<span></span>Execute job logic or trigger callback at the scheduled time.
6.<span></span>Support retries with exponential backoff on failures.
7.<span></span>Monitoring & Control
8.<span></span>Query job status (Scheduled, Running, Completed, Failed).
9.<span></span>View execution logs.
10.<span></span>Callback Support
11.<span></span>Support HTTP callbacks or in-process handlers.
12.<span></span>Handle failures gracefully (retries, dead-letter queue).
Non-Functional
1.<span></span>Scalability – Handle millions of jobs concurrently.
2.<span></span>Fault Tolerance – Recover jobs after crashes or restarts.
3.<span></span>High Availability – No single point of failure.
4.<span></span>Time Accuracy – Execute jobs within ± few seconds of schedule.
5.<span></span>Durability – Persist jobs in reliable storage.
Design Considerations
1.<span></span>API Layer
2.<span></span>POST /jobs – create a job (one-time or recurring).
3.<span></span>PUT /jobs/{id} – update/cancel job.
4.<span></span>GET /jobs/{id} – check job status.
5.<span></span>POST /callbacks – register a callback handler.
6.<span></span>Scheduler Service
7.<span></span>Maintains job metadata in DB.
8.<span></span>Uses priority queue / min-heap keyed on execution time.
9.<span></span>Supports distributed scheduling (sharded by time or jobId).
10.<span></span>Worker/Executor Service
11.<span></span>Picks jobs due for execution.
12.<span></span>Executes job logic or triggers callback.
13.<span></span>Reports status & logs to DB.
14.<span></span>Storage Layer
15.<span></span>Persistent DB for job metadata (Postgres/MySQL or Cassandra).
16.<span></span>Redis/priority queue for near-term jobs.
17.<span></span>Dead-letter queue (Kafka/SQS) for failed callbacks.
18.<span></span>Scalability Enhancements
19.<span></span>Sharded workers for parallelism.
20.<span></span>Leader election for scheduler coordination.
21.<span></span>Distributed lock (Zookeeper/etcd/Consul) to prevent duplicate execution.
Trade-offs to Discuss
1.<span></span>Polling vs Event-driven Scheduling – Polling DB is simpler; event-driven via timers is more efficient but complex at scale.
2.<span></span>Accuracy vs Throughput – Millisecond accuracy costs more infra vs batching jobs per second.
3.<span></span>Retry Strategy – Immediate vs exponential backoff.
4.<span></span>Consistency vs Availability – Prefer at-least-once execution over exactly-once to ensure reliability.