Design a Multi-Stage Pipeline
🏭 What is a multi-stage pipeline? It is an assembly line for data. Work is split into steps, such as parse, enrich and save, and each step runs on its own threads at the same time as the others. Items flow from one step to the next through queues, so while one item is being saved, the next is being enriched and a third is being parsed.
This problem looks like a pure producer-consumer exercise, and it is one, repeated. What makes it a good interview question is what happens at the edges. What if one step is much slower than the others? What if a step needs four threads? How do you stop the whole line without losing the items still on it? Those three questions are the three things the title promises: bounded queues, backpressure and a poison-pill shutdown.
Requirements
"We process log lines in three steps: parse them, enrich them with user data, and write them out. The steps run at different speeds. Build an in-process pipeline that runs the steps concurrently."
Clarifying Questions
You: Are the steps fixed, or does the pipeline take any list of steps?
Interviewer: Any list. Each step is a function that takes one item and returns one item. A step may also drop an item, for example a filter that throws away debug lines.
You: Can one step have more than one thread?
Interviewer: Yes. Enrich calls another service and is the slowest step, so we want to give it several workers. Each step should have its own worker count.
You: If parse is fast and enrich is slow, items pile up in between. Is memory a concern?
Interviewer: Yes. The pipeline must use bounded memory. If a step falls behind, the steps before it, and in the end whoever feeds the pipeline, should slow down instead of filling memory.
You: Does the output have to come out in the input order?
Interviewer: For a step with one worker, keep the order. With several workers, order can change. That is fine for now.
You: How do we stop it?
Interviewer: A close() call. Every item that was already accepted must still go all the way through. Then all threads stop. New items after close() are turned away.
You: Can a step fail?
Interviewer: Assume the step functions do not throw for now.
You: Where does the output go?
Interviewer: Collect it in a list we can read after close(). In production it would be one more step that writes somewhere.
The third answer is the one that shapes the design. "Bounded memory" plus "the producer should slow down" is the definition of backpressure: pressure from a slow consumer travels back up the line until it reaches the source. We will get it almost for free from the right kind of queue.
💡 Interview tip: Whenever you put a queue between two threads, ask "what happens when the consumer is slower than the producer?" An unbounded queue answers "we run out of memory, eventually". A bounded queue answers "the producer waits". Interviewers want to hear that you know the difference.
Final Requirements
- A pipeline is built with a queue capacity and a list of stages. Each stage has a function (item in, item out, or "drop") and a number of worker threads.
- Items go through the stages in order. A stage with one worker keeps the order of its items.
- Every queue between stages holds at most the capacity. When a queue is full, whoever feeds it waits.
put(item)feeds the first stage, blocking while its queue is full. It returnsfalseafterclose().close()finishes every accepted item, stops every worker, and only then returns. Calling it twice is safe.- Every accepted item is either dropped by a stage or appears exactly once in the output.
results()returns the output of the last stage in the order it came out.
Out of scope
- Branching (one step feeding two others) and joining. That is a DAG runner, see /low-level-design/workflow-pipeline.
- Steps that fail (we come back to this in Extensibility).
- Surviving a crash: items in memory are lost if the process dies.
- Changing the stages while the pipeline runs.