DAG of tasks
A directed acyclic graph: tasks as nodes, "must finish before" as arrows, with no path leading back to where it started. It exists because a pipeline with branches (encode four renditions in parallel, then package) cannot be written as a list, and the graph tells the scheduler exactly what may run now.
What it costs
| Operation | Cost | Note |
|---|---|---|
| Topological order | O(V + E) | A valid run order: every task after all its dependencies. Kahn’s algorithm or depth-first search. |
| Ready set | O(1) per completion | Decrement a dependency counter on each child when a task finishes; a counter hitting zero means the child is ready. |
| Critical path | O(V + E) | The longest path by duration; it bounds wall-clock time no matter how many workers you add. |
| Retry a task | O(subtree) | Re-run the task; its descendants wait. Idempotent tasks make this safe. |
| Cycle check | O(V + E) | If topological sort cannot consume every node, there is a cycle and the graph is invalid. |
Memory: One record per task plus one per edge: for a 10-minute video split into 40 chunks × 4 renditions, about 170 task rows and 340 edges of a few hundred bytes each, under 200 KB of durable state per workflow.
Branches run in parallel; package waits for all of them
Arrows mean "needs the output of". After split, every encode and side task is ready at once. Package has 163 incoming edges and starts only when its dependency counter reaches zero.
How it works
- Define the tasks and their dependencies: probe → split → {encode chunk i at rendition r for every i, r} → package → publish, plus side branches (audio, thumbnails) that join at package. Each arrow means "the target needs the source’s output".
- Validate there is no cycle: run a topological sort, which repeatedly removes a node with no remaining incoming edges. If nodes remain with incoming edges, there is a loop and the pipeline can never finish.
- Store each task’s state (pending, running, done, failed) and a count of unfinished dependencies in a durable store; this is the orchestrator’s only memory, so a crash loses nothing.
- Start every task whose dependency count is zero (just probe). When a task completes, decrement the counter on each child; any child reaching zero is enqueued. The ready set at any moment is exactly the tasks whose inputs exist.
- Parallelism is the width of the graph: after split, 160 encode tasks are all ready at once and run on as many workers as you have. The critical path (probe → split → slowest encode → package → publish) is the floor on total time.
- A failed task is retried in place with backoff; its descendants simply stay pending. Because each task is idempotent and keyed by its inputs, a retry overwrites the same output and nothing downstream has to be undone.
Why it is shaped this way
- Explicit dependencies let the scheduler derive concurrency instead of the author guessing it: everything not on a dependency chain runs at the same time automatically.
- Acyclicity is what guarantees termination: with no path back, every task eventually has its inputs and the graph drains.
Worked example
The transcoding pipeline for a 10-minute upload
- Tasks: 1 probe, 1 split, 40 chunks × 4 renditions = 160 encodes, 3 side tasks (audio, thumbnails, fingerprint), 1 package, 1 publish: 167 nodes. Edges: split → each encode (160), each encode → package (160), side tasks → package (3), probe → split, package → publish, plus 3 from split to the side tasks: about 330 edges.
- Topological order: probe, split, then the 163 middle tasks in any order, then package, then publish. The ready set after split completes is 163 tasks at once.
- Durations: probe 5 s, split 20 s, a 1080p chunk encode 90 s (the slowest rendition), package 30 s, publish 2 s. Critical path: 5 + 20 + 90 + 30 + 2 = 147 s. With 200 workers the whole video is ready in about 2.5 minutes; with 20 workers the encode layer takes 160 / 20 × ~42 s average (90, 45, 22 and 11 s per rendition) ≈ 6 minutes and the critical path no longer dominates.
- Sequential alternative: one worker encoding the whole file at each rendition is 40 × 90 s = 60 minutes for 1080p alone, plus 30, 15 and 8 minutes for the smaller renditions, close to 2 hours, and one crash restarts the lot.
- Failure: chunk 17 at 720p fails on a bad frame. Only that node retries; the other 159 encodes and the side tasks finish and package waits with 1 dependency remaining. On the third retry it succeeds, the counter hits zero, package runs. Total delay: the retry time, not a rerun.
- State: 167 task rows plus counters in Postgres, updated on each completion, about 170 writes over the run. The orchestrator can crash and resume from the rows.
When it is the wrong choice
- A strictly linear job (fetch, transform, write with no branches): a plain queue of steps is the same thing with less machinery.
- Work that depends on its own earlier output (iterate until a model converges): that is a cycle. Model it as a loop around a DAG, or as repeated DAG runs, not as a graph with a back edge.
- Thousands of tiny tasks per second: the per-task scheduling overhead (a durable write per state change) dominates. Batch the tiny steps into one task.
Interviewer follow-ups
Answer out loud before you open each one.
How does the scheduler know which tasks can run right now?
Each task keeps a count of dependencies not yet finished. On every completion the scheduler decrements the count on each child; a child reaching zero is ready and goes on the queue. No scanning of the graph is needed, just one decrement per edge over the whole run.
What is the critical path and why does adding workers stop helping?
The longest chain of dependent tasks by duration, here probe → split → slowest encode → package → publish. Those tasks must run one after another regardless of parallelism, so their total is the minimum wall-clock time. Once you have enough workers for the widest layer, more workers do nothing.
What happens when one task in the middle fails?
It is retried in place with backoff; its descendants stay pending because their counters never reach zero. Siblings are unaffected and keep finishing. Because tasks are idempotent and keyed by their inputs, the retry overwrites the same output, so nothing has to be rolled back downstream.
Why must the graph be acyclic?
A cycle means some task is waiting on a task that is, directly or indirectly, waiting on it; its dependency counter can never reach zero and the run never ends. Topological sort doubles as the check: if it cannot consume every node, there is a cycle. Iteration is expressed as repeated runs of the DAG rather than an edge pointing backward.