Design a delayed job execution app

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2018-03-17

Problem Statement

Designing and implementing a system that schedules and executes jobs after a specified delay or at a specific future time. The system should support concurrent job execution, persistence, retries, and cancellation while being extensible and fault-tolerant.


🔶 Functional Requirements


1.Job Scheduling
•Schedule a job to execute after a given delay or at a specific timestamp.
•Support recurring jobs with a configurable interval.
2.Job Execution
•Execute jobs at the scheduled time.
•Handle concurrent execution of multiple jobs.
•Ensure thread-safe execution for shared resources.
3.Job Management
•Cancel a scheduled job before it executes.
•Reschedule jobs to a new execution time.
•Query job status (Pending, Running, Completed, Failed).
4.Retry & Failure Handling
•Retry failed jobs a configurable number of times.
•Support exponential backoff for retries (optional).
5.Persistence
•Persist job details to survive system restarts or crashes.
•Restore pending jobs on application restart.


🔶 Non-Functional Requirements


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


🔶 Constraints


•Scheduled times can be in the past or future (past → execute immediately).
•Maximum number of concurrent threads is configurable.
•Jobs must not interfere with each other if accessing shared resources.


🔶 Example Use Cases


1.Schedule an email to be sent 2 hours later.
2.Execute a recurring data backup every 24 hours.
3.Trigger notifications for events at specific times.
4.Cancel a job before its scheduled execution.


👉 In short:

Design a Delayed Job Execution System that allows scheduling, executing, retrying, and managing jobs with support for concurrency, persistence, and fault-tolerance.

Solution

Comments