Design a Distributed Job Scheduling System

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-05-31

Problem Statement

Design a Distributed Job Scheduling System that can schedule, run, monitor, and retry jobs across a fleet of distributed machines. Think of systems like Airflow, Celery, Kubernetes CronJobs, or Quartz Scheduler.


✅ Functional Requirements:

1.Job Registration
•Users can register jobs (scripts, HTTP calls, tasks).
•Jobs may be one-time, repeated (e.g., every hour), or cron-based.
2.Job Execution
•Jobs are assigned and executed on worker nodes.
•System must ensure exactly-once or at-most-once execution.
•Job execution should be logged and monitored.
3.Job Monitoring & Retry
•Track job status: pending, running, success, failed.
•Support automatic retries with configurable backoff policies.
4.Failure Handling
•Detect failed or stuck jobs.
•Reschedule or reassign if a worker node crashes.
5.Scalability
•Support thousands to millions of jobs/day.
•Horizontal scalability across worker nodes and schedulers.


⚙️ Non-Functional Requirements:

•High Availability: System should tolerate node failures.
•Fault Tolerance: Jobs shouldn't be lost during node crashes.
•Scalability: Able to handle a large fleet of jobs and worker machines.
•Accuracy: Jobs should be triggered on schedule with minimal drift.
•Monitoring: Expose metrics (success/failure rate, execution time, etc.)
•Security: Validate job ownership and authorization.


🔐 Constraints:

•Jobs may be short- or long-running.
•Must support time-based triggers (cron/interval).
•Avoid duplicate executions (idempotency, locking, leasing).
•Should support multitenancy (optional).


🧠 Components You Might Need to Design:

•Job Scheduler Service (leader/election logic, timer queue)
•Job Metadata Store (PostgreSQL, etc.)
•Distributed Locking (e.g., Zookeeper, Redis, Consul)
•Worker Nodes / Executors
•Queue System (e.g., Kafka, SQS, Celery queues)
•Retry Manager
•Dashboard / Monitoring Service
•Notification Service (for job failure alerts)


🧪 Example Scenario:

1plaintext 2CopyEdit 31. User registers a job that hits an HTTP endpoint every 15 minutes. 42. Scheduler stores metadata and assigns it to a worker at 10:15. 53. Worker executes the job and logs the result. 64. Job fails with a timeout — system retries after 5 minutes. 75. Retry succeeds, status is marked as success. 86. Metrics show average execution time and success/failure trends.


Solution

Comments