Pipes & Filters
Pipeline
Escrito por Darjus Kazakevic · revisado el
Each stage transforms data and passes it on, knowing nothing of its neighbours.
Pipes and Filters splits processing into a chain of small steps. Each filter reads input, transforms it and writes output. A pipe connects one filter's output to the next filter's input. The Unix shell is the classic example: sort names.txt | uniq -c | sort -rn counts each name, most common first.
The rule that makes it work: a filter knows only its input and output format, never which filter comes before or after it. So you can test each filter alone, reorder them and run them in parallel. The cost: every step must use a shared format, and an error in the middle of a stream needs a plan.
En esencia — Join small filters with pipes, so each stage can be tested alone, reordered in a code review or replaced, without touching the others.
Cuándo recurrir a él
Recurre a él cuando
- Data flows through ordered steps: ETL, log processing, compilers
- The input is too big for memory and must be streamed
- Steps should be reusable in other pipelines or reordered later
Reconsidéralo cuando
- Steps must share mutable state or send results back upstream
- The logic branches on state instead of flowing in one direction
- Interactive request and response, where a user waits for one answer
The parts
- Filter
- One processing step with one job: parse, drop bad rows, convert units. It reads its input, writes its output, and shares no state with other filters.
- Pipe
- The connection between two filters: a Unix pipe, an in-memory stream or a queue. It buffers data, so the two sides can run at their own speed.
- Source and sink
- The two ends of the pipeline. A source produces data (a file, a socket, a Kafka topic). A sink consumes it (a database, a file, an HTTP call).
- Backpressure
- A slow filter makes the faster one before it wait, instead of letting data pile up in memory. A Linux pipe does this with a 64 KB buffer; Node.js streams do it with highWaterMark.
Reglas de diseño
- A filter knows its input and output, nothing more
- It reads the agreed format and writes the agreed format. It never calls another stage or shares a variable with one, so you can test it with literal rows and move it without surprises.
- Stream between stages; do not collect
- Pass streams or async iterables, not arrays. An array chain holds the whole dataset at every step; a stream holds one chunk, so a 10 GB file runs in a few megabytes.
- Agree on one format between filters
- Lines of text in Unix, typed records in code. If each pair of filters uses its own format, you need converters everywhere and reordering stops working.
- Decide what a bad record does
- Choose per pipeline: skip and count it, send it to a dead-letter file, or stop. A filter that throws on row 4 million should not throw away the rows before it.
Errores habituales
- Collecting into arrays between steps. Memory grows with the input, and the job dies on the first big file.
- A filter that depends on the one before it, for example on a field only that one sets. Reordering then breaks without an error.
- No plan for bad records. One malformed row kills a two-hour job at 95%.