Implement BFS and a Job Scheduler
Concurrent Periodic Job Scheduler
Design and implement a thread-safe, in-memory scheduler capable of managing and executing periodic jobs.
Job Definition
Each job is uniquely identified by a jobId and includes:
- A unique
jobId(string or integer) - An
intervalMsspecifying the execution frequency in milliseconds - A
task(callback function) to be executed
Core Operations
The scheduler must support the following public API:
register(jobId, intervalMs, task): Adds a new job to the scheduler.cancel(jobId): Removes a job from the scheduler, ensuring safe cancellation even if the job is currently being polled or executed.start(): Initiates the scheduler's operation, beginning to execute registered jobs.stop(): Halts the scheduler's operation, preventing further job executions.
Key Requirements and Considerations
- Concurrency: The scheduler must allow multiple worker threads to execute jobs concurrently.
- Timing Accuracy: Jobs should be triggered approximately once per specified interval.
- Idempotency: A specific scheduled run of a job must not execute more than once.
- Robust Cancellation: Cancellation must be thread-safe and effective, even if the job is in a transitional state (e.g., being picked up by a worker or actively running).
- Advanced Scenarios: Discuss strategies for handling:
- Tasks that run longer than their defined interval.
- The impact and mitigation of clock drift.
- Managing backpressure under heavy system load.
AI interview prep
Core dilemma
Balancing precise, timely job execution with thread safety, resource management, and resilience to failures/overload in a concurrent, in-memory environment.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement BFS and a Job Scheduler
Concurrent Periodic Job Scheduler
Design and implement a thread-safe, in-memory scheduler capable of managing and executing periodic jobs.
Job Definition
Each job is uniquely identified by a jobId and includes:
- A unique
jobId(string or integer) - An
intervalMsspecifying the execution frequency in milliseconds - A
task(callback function) to be executed
Core Operations
The scheduler must support the following public API:
register(jobId, intervalMs, task): Adds a new job to the scheduler.cancel(jobId): Removes a job from the scheduler, ensuring safe cancellation even if the job is currently being polled or executed.start(): Initiates the scheduler's operation, beginning to execute registered jobs.stop(): Halts the scheduler's operation, preventing further job executions.
Key Requirements and Considerations
- Concurrency: The scheduler must allow multiple worker threads to execute jobs concurrently.
- Timing Accuracy: Jobs should be triggered approximately once per specified interval.
- Idempotency: A specific scheduled run of a job must not execute more than once.
- Robust Cancellation: Cancellation must be thread-safe and effective, even if the job is in a transitional state (e.g., being picked up by a worker or actively running).
- Advanced Scenarios: Discuss strategies for handling:
- Tasks that run longer than their defined interval.
- The impact and mitigation of clock drift.
- Managing backpressure under heavy system load.