KatInterview

Crash-Safe Job Dispatcher with Lease Timeouts and Dependency Graphs

Data Structure DesignModel answer
HardModel answerData Structure Designleasesfencing-tokensdag

Last updated

Build an in-memory job dispatcher where workers hold timed leases, stale workers are fenced off, jobs form a dependency DAG, and you justify the delivery guarantees.

Design and implement a job dispatcher for a compute platform. Clients enqueue jobs. A fleet of workers repeatedly asks for a job, executes it, and reports back. Workers may crash or stall at any time without telling anyone, yet every job must eventually either finish or be recorded as permanently failed.

Jobs may also declare prerequisites. A job may start only after every prerequisite has finished successfully, and the prerequisite relationships must form a directed acyclic graph.

Before coding, state any assumptions you make about the following:

  • Whether a job can depend on jobs from earlier submissions, the same submission, or jobs not yet submitted.
  • How long a job may run, and whether a worker can extend its hold on a job.
  • How many attempts a job gets, and what happens to its downstream jobs when it fails for good.
  • Whether side effects must occur exactly once, or whether at-least-once is fine for idempotent jobs.
  • Whether the service is purely in-memory for now, with durability discussed afterward.

Part 1: Claim, lease, and report

Implement claim(now) so a worker obtains a runnable job, plus the calls a worker uses to renew its hold and to report success or failure. Requirements:

  • A job held by a worker that dies must eventually be given to another worker.
  • A slow worker that has already lost its hold must not be able to change the job's state when it finally reports.
  • Jobs have a maximum number of attempts, after which they enter a terminal failed state.
  • Expired holds should be found efficiently rather than by scanning every job.

Part 2: Dependency graph

Extend the dispatcher so a job becomes runnable only when all prerequisites have succeeded.

  • Reject any submission that would introduce a cycle, with an error that is useful to the caller.
  • Release downstream jobs as their prerequisites finish.
  • Decide and document what happens to downstream jobs when a prerequisite fails permanently.
  • Validate unknown and duplicate job IDs.

Part 3: Delivery guarantees

Say whether your dispatcher gives at-most-once, at-least-once, or exactly-once execution. Explain what it would take to get effectively exactly-once outcomes and what that costs.

Expected deliverable: the state machine, the data structures, the core code, the crash and race scenarios you would test, and a discussion of how this would become durable and multi-server.

Hints

Hint 1

Give each job an explicit state (blocked, ready, leased, succeeded, failed) and a monotonically increasing token that changes every time the job is leased. Every report must present the token, and a mismatch means the report is stale.

Hint 2

Keep a min-heap of (lease_expiry, job_id, token) entries and lazily discard entries whose token no longer matches. At the start of each claim, pop all expired entries and requeue or fail those jobs. Track an unmet-prerequisite counter per job and decrement it as prerequisites succeed.

Hint 3

For cycle detection with prerequisites that may already exist, run a DFS or a reachability check from the new job's prerequisites back to the new job before committing, and return the cycle path. For delivery semantics, reason about the window between a worker finishing its side effect and the dispatcher recording the result.