Design a Job Scheduling System

Hard
ConcurrencySchedulingThread PoolCronPriority QueueConcurrency

Problem Statement

Design a job scheduling system that executes tasks at specific times or recurring intervals, supports priorities, handles failures with retry, and manages a configurable worker thread pool — similar to Quartz Scheduler or cron.

Requirements

Functional Requirements

  • Schedule a job: one-time at a specific timestamp or recurring (cron expression / interval)
  • Job types: one-time, fixed-delay, fixed-rate, cron-based
  • Priority: HIGH, MEDIUM, LOW — higher priority jobs execute first when multiple are ready
  • Concurrency control: max N jobs running simultaneously (thread pool)
  • On failure: retry up to K times with configurable backoff
  • Cancel a scheduled job
  • Job status: PENDING, RUNNING, COMPLETED, FAILED, CANCELLED
  • View next execution time for recurring jobs
  • Job history with execution times and results

Non-Functional Requirements

  • Thread pool for concurrent job execution
  • Priority queue for scheduling decisions
  • Thread-safe job state management

Key Entities

  • Job: id, name, task (Callable/Runnable), schedule, priority, retryConfig, status
  • Schedule: type (ONE_TIME/FIXED_RATE/FIXED_DELAY/CRON), interval, nextExecutionTime
  • JobExecution: jobId, startTime, endTime, status, result, attempt
  • JobScheduler: priority queue of scheduled jobs, thread pool, scheduler daemon thread

Constraints

  • Scheduler daemon thread wakes every second to check for due jobs
  • Fixed-rate: next execution = lastScheduledTime + interval (regardless of execution duration)
  • Fixed-delay: next execution = lastCompletionTime + delay
  • Thread pool size configurable (default: CPU cores × 2)
  • Retry backoff: exponential (2^attempt seconds)

!Constraints

- Priority queue: higher-priority ready jobs execute before lower-priority ones - Thread pool bounded: max N concurrent jobs - Fixed-rate: interval measured from last trigger time - Fixed-delay: interval measured from last completion time - Retry up to K times with exponential backoff
Output

Run or submit your code to see output here.