KatInterview

Deadline-Aware Task Dispatcher with Dependencies and Leases

System DesignModel answer
MediumModel answerSystem Designschedulerpriority-queuetopological-sort

Last updated

Build a task dispatcher that serves the earliest-deadline runnable task, handles prerequisites, and grows into a fault-tolerant multi-worker service.

You are asked to design a dispatcher for a stream of tasks. It accepts work continuously and hands it out to workers, honoring both deadlines and dependencies. The design is built up in three stages, and a strong answer keeps one coherent design across all of them.

Each task has:

  • id: a unique identifier
  • deadline: an orderable value, such as an epoch timestamp
  • prerequisites: a list of task ids that must finish first (empty in the first two stages)

Rules

  • Among tasks that are runnable (every prerequisite finished), the earliest deadline goes first. Ties are broken deterministically, for example by id.
  • Running a task at-least-once is acceptable, so workers must be idempotent. However, one task must never be leased to two workers at the same time.
  • Stages 1 and 2 may assume a single-process, single-threaded implementation. Concurrency, persistence and distribution only appear in stage 3.
  • Pseudocode or Python/Go is fine, but data structures and complexities must be explicit.

Questions you may want to raise first: Can a task reference a prerequisite that has not been submitted yet? Is there one global dependency graph or many independent workflows? What happens to dependents if a prerequisite fails permanently? Is a deadline a hard SLA or just a priority hint? What are the expected ingest rates and live-task counts?

Stage 1: Deadline ordering

Implement a Scheduler with:

  • add_tasks(tasks): accepts a single task or a batch.
  • consume_task(): removes and returns the task with the earliest deadline, or a sentinel (None or "no task") when nothing is available.

State the time complexity of each operation and the memory cost.

Stage 2: Prerequisites

Tasks now carry prerequisites. Add complete_task(id) so the scheduler learns when a task finishes. consume_task() must return the earliest-deadline task among runnable ones only. Explain how this stays efficient as tasks complete and how it relates to a topological ordering.

Stage 3: Turn it into a service

Extend the design so that it:

  1. Accepts batches or a continuous stream of tasks.
  2. Validates input: malformed tasks, duplicate ids, self-dependencies, and prerequisites that can never resolve.
  3. Rejects submissions that would create a dependency cycle.
  4. Dispatches only runnable tasks, earliest deadline first.
  5. Supports many concurrent workers without double assignment.
  6. Recovers from worker crashes and retries (a task held by a dead worker must become runnable again).
  7. Scales to large volumes with bursty ingest and offers useful monitoring.

Cover the API, storage model, scheduling logic, DAG validation (batch versus streaming), concurrency control, fault tolerance and scaling.

Hints

Hint 1

For stage 1, you only ever need the minimum element repeatedly. Put a tuple like (deadline, id) into the ordering key so ties are deterministic, and keep a side dictionary for lookup and cancel.

Hint 2

For stage 2, store for each task a count of unfinished prerequisites plus a reverse map from a task to the tasks that depend on it. A task enters the heap exactly when its count reaches zero. This is Kahn's algorithm with a heap as the frontier.

Hint 3

For stage 3, model a task state machine (WAITING, RUNNABLE, LEASED, COMPLETED, FAILED) and make the RUNNABLE to LEASED transition atomic in the store, for example with a row-locking query that skips locked rows or a single atomic script. Give every lease an expiry so a sweeper can return abandoned tasks to RUNNABLE.