Design a Job Scheduler with Asynchronous Multi-Task Execution
Company: Mercor
Role: Software Engineer
Category: System Design
Difficulty: medium
Interview Round: Onsite
Design a job scheduler service. Clients submit jobs, each job is made up of one or more tasks, and the service executes the tasks asynchronously on a pool of workers. Submitting a job returns a job ID at once, without waiting for any task to run. Clients can later check the status of the job and of each of its tasks, cancel a job, and fetch results when the tasks finish.
Cover the submission and status APIs, how jobs and tasks are stored, how ready tasks reach workers, how several tasks of one job run concurrently, how the scheduler detects that a whole job has finished, and what happens when a task fails or a worker dies in the middle of a task.
```hint Separate the record from the hand-off
Keep the durable state of each job and task apart from the mechanism that hands ready tasks to workers, and decide which of the two you trust after a crash.
```
```hint Lose a worker
A worker can take a task and then die without reporting anything. Decide how the system notices, and what happens to that task.
```
### Clarifying Questions
- Do jobs run as soon as they are submitted, at a requested time, or on a recurring schedule?
- Are the tasks within a job independent, or can a task depend on the output of another task?
- What do tasks do: short function calls, long batch processes, or both? Are they safe to run twice?
- How many jobs per second are submitted, and how many tasks can one job contain?
- If one task fails after its retries, should the job fail at once and cancel its other tasks, or finish the rest and report a partial result?
- Do some clients need priority, or a guaranteed share of the workers?
### What a Strong Answer Covers
- An API for submitting, checking, cancelling and fetching results, with safe client retries of the submit call.
- A data model for jobs, tasks and attempts with explicit state transitions.
- A dispatch path from ready tasks to workers, how a lost worker's task is detected and reassigned, and the delivery guarantee that results.
- Concurrent execution of a job's tasks, completion tracking and the policy for partial failure.
- Retries, timeouts, tasks that fail every time, and idempotent task execution.
- Scaling of the API, the store, the queue and the workers, with metrics for queue depth, latency and failures.
### Follow-up Questions
- The tasks of a job now form a dependency graph. How do the data model and the release of ready tasks change?
- One tenant submits a job with a million tasks. How do you keep other tenants' jobs moving?
- How do you run delayed and recurring jobs so each one fires once even when several scheduler instances are running?
- A task has external side effects, such as charging a card or sending an email. How do you avoid repeating them on retry?
Overview: A system design question that asks for a job scheduler in which each job holds several tasks executed asynchronously on a pool of workers. It tests the submission and status API, job and task state, dispatch to workers, completion tracking, retries and recovery when a worker fails mid-task.