Design a Pipeline Runner
⚙️ What is a pipeline runner? It is the engine behind a CI system. A config file lists jobs such as lint, build, test and deploy, and says which jobs must finish before others can start. The runner checks that config, starts every job as soon as the jobs it waits for have passed, runs independent jobs side by side, and stops the parts of the pipeline that depend on a job that failed.
This problem shows up as "design a CI pipeline", "design a workflow engine", "run a YAML-configured job pipeline" or "execute a DAG of tasks". They are the same design. The YAML file is just how the jobs arrive, the dependencies form a directed acyclic graph (DAG), and "parallel stages" is the runner starting more than one job at a time. We cover the YAML loading as its own small class, and the older "stages" style of config as a follow-up, because it turns out to be a special case of the DAG.
Requirements
"Design a runner for a build pipeline. Jobs are defined in a YAML file and can depend on other jobs. Run them in the right order, in parallel where possible."
The words that hide the most design are "right order" and "in parallel". The questions below pin both down.
Clarifying Questions
You: What does a job look like in the config?
Interviewer: A name, the command to run, and an optional needs list naming the jobs that must succeed first. Something like test: {needs: [build]}.
You: Do I write the YAML parser?
Interviewer: No, use a library. You own turning the parsed data into jobs and checking that it makes sense.
You: What counts as a config that does not make sense?
Interviewer: A job that needs a job that does not exist, and dependencies that loop back on themselves. Reject both before anything runs. Half a pipeline that hangs forever is worse than an error up front.
You: How many jobs can run at the same time?
Interviewer: We have a fixed number of workers. Assume the number is given when the runner is created.
You: If several jobs are ready and only one worker is free, which one goes first?
Interviewer: The one that appears first in the config file. Keep it predictable.
You: What happens when a job fails?
Interviewer: Anything that depends on it, directly or through other jobs, is skipped. Jobs on other branches keep going, so the developer sees every failure in one run.
You: Do I actually execute shell commands?
Interviewer: Not in the interview. Pretend each job takes a known amount of time and that we know up front which ones will fail. Then we can talk about real execution.
That last answer is a gift. It turns the runner into a simulation: a clock, a set of busy workers and a list of jobs waiting. The scheduling logic is the same one a real runner uses, but every run is deterministic, so you can trace it by hand at the end.
💡 Interview tip: When the prompt involves "running" things (jobs, requests, elevators), ask whether you can simulate time instead of sleeping or spawning threads. Most interviewers say yes, and it moves the conversation to the part they want to grade: the scheduling rules.
Final Requirements
- A pipeline is a list of jobs loaded from a YAML config. Each job has a unique name, a duration, and a list of jobs it needs.
- Before running, the runner rejects the config if a need names an unknown job or if the needs form a cycle. Nothing runs in that case.
- A job is ready when every job it needs has succeeded. Jobs with no needs are ready at the start.
- At most
workersjobs run at the same time. Whenever a worker is free, the ready job that appears first in the config starts. - When a job fails, every job that depends on it, directly or indirectly, is skipped. Unrelated jobs keep running.
- After a run, anyone can ask for a job's status (succeeded, failed, skipped, or pending if it never ran) and when it started. The run reports when the last job ended.
- A run can be repeated with different failures or after new jobs are added.
Out of scope
- Parsing YAML ourselves, and the shell or containers a job runs in.
- Logs, artifacts and caches passed between jobs.
- Triggers (on push, on a schedule) and showing results in a UI.
- Retries, cleanup jobs and real threads (we come back to these in Extensibility).