Pipes and Filter Pattern

⏱ 7 min read

Take a big processing job, cut it into small steps, connect the steps with queues. Each step deploys, scales and gets reused on its own. That's Pipes and Filters, and you've used it even if you never named it: every cat | grep | sort in a shell is one. In practice, you'll have small steps, connected by queues. The idea was written down in 2003, and it still holds.

The pattern, before the cloud got hold of it 🔗

Pipes and Filters is older than Azure Functions. Gregor Hohpe and Bobby Woolf call it a fundamental architectural style for messaging in Enterprise Integration Patterns (2003), and many of the book's routing and transformation patterns are built on it. Their definition: divide a larger processing task into a sequence of smaller, independent processing steps (filters) that are connected by channels (pipes).

Their example is an incoming order. It arrives encrypted, it carries a digital certificate to prove a trusted customer sent it, and it may arrive twice because someone clicked "Order Now" twice. One "incoming message massaging module" could handle all three, but it would be hard to test and hard to change. So the book splits it: Decrypt, then Authenticate, then De-Dup. Plain, unique orders come out the other end. you have 3 processing steps.

A Filter. One processing step. It receives a message on its inbound pipe, processes it, and publishes the result to its outbound pipe. That's the whole interface.

A Pipe. The channel between two filters. A filter sends into its pipe without knowing which component will read from it later. The obvious pipe is a message channel, like a queue, but the book recommends coding against an abstract pipe so you can swap in an in-memory queue when the filters share a machine.

Port. Where a filter plugs into a pipe. In the basic form, each filter has one input port and one output port. Because every filter has the same shape, you can add, drop or rearrange filters by rewiring the pipes, "all without having to change the filters themselves."

Pipeline processing. Each filter runs in its own thread or process. Once Decrypt hands message 1 to Authenticate, it starts on message 2 straight away. Messages move through the stages at the same time.

Parallel processing. The pipeline is only as fast as its slowest filter, so you run several copies of that one, competing for messages on the same channel. The book adds two catches. Messages can now finish out of order, and it works best with stateless filters. The de-duper remembers every message it has seen, so you can't just run three of it.

The book names the cost up front. Every pipe is another channel to run, and every hop converts the message into the messaging format and back. A long chain of filters pays "for the gain in flexibility with potentially lower performance." It also names the reasons to split: reuse (order status messages need decrypting but not de-duping) and steps that must run on different machines, like a decryptor whose private key lives on one locked-down server.

When Pipes and Filters pays on Azure 🔗

The example I'd keep is an image pipeline. An uploaded image goes through content moderation, resizing, watermarking, reorientation, Exif metadata removal, and finally CDN publication. Each step can be an Azure Function, the pipes are Azure Queue Storage queues, and the messages carry a claim check - a pointer to the image in Blob Storage - instead of the image itself.

Why does this one earn its queues? Because the steps are different animals.

  • Moderation calls an external or AI service - slow and rate-limited.
  • Resizing and reorientation are CPU-heavy and want bigger machines or more instances.
  • Exif removal is cheap.
  • CDN publication is I/O and depends on someone else's API.

These steps don't share hardware needs, scaling curves or failure modes. That's the book's "different machines" reason, in cloud clothes. If moderation slows to a crawl, you scale that filter alone, and the resize workers don't care.

The claim check matters too. Passing a pointer through five queues is cheap. Passing a 12 MB image is how you find your queue's message size limit.

Moderate, Resize, Watermark and Publish filters joined by queues. Each queue message carries only the image id and a blob link. Every filter reads the image from Blob Storage and writes its result back there.

Rules for a good filter 🔗

Idempoteny. Plan for this failure: a filter posts its result to the next pipe, then crashes before it marks its own input as done. The input comes back, another instance runs it, and the next filter receives the same message twice. That's at-least-once delivery doing exactly what it promises.

Service Bus can help, with a catch. Its duplicate detection drops messages with a MessageId it saw within a window (10 minutes by default, up to 7 days, and not on the Basic tier). It only works if the retried filter produces the same MessageId, so derive it from the input, like the image id plus the stage name. A fresh GUID per send defeats it. The image pipeline above runs on Queue Storage, which has no such feature, so the filter checks for itself.

Pass through what you don't use. A filter has to tolerate fields in the incoming message that it doesn't operate on, and pass them along unchanged. The watermark filter doesn't care about the moderation score, but the CDN filter might, so it forwards it untouched. A filter that deserializes into its own narrow type and serializes it back quietly deletes everyone else's data.

Carry enough context to run alone. A filter shouldn't assume anything about how it was invoked. If it needs the tenant id to pick a storage container, the tenant id travels in the message.

Decide what an error means. Every filter must choose whether a failure fails the pipeline or propagates. Moderation rejecting an image is a business outcome, so end the pipeline with a clear status. A CDN timeout is transient, so it can be retried. Write that decision down per filter. The queue won't make it for you.

Watch the slowest filter. A single request is only as fast as the slowest filter it passes through. Queue depth per stage tells you which one is slow. Scale that one, not all of them.

What this means in practice 🔗

  • Split a step out when it scales, fails, or runs on hardware differently from its neighbours.
  • Pass large payloads by claim check, not through the queue.
  • Make every filter idempotent, with deterministic message ids, and forward fields it doesn't read.
  • Alert on queue depth per stage, so you know which filter to scale.

The book promised you could add, drop or reorder filters by rewiring. On a pipeline like the image one, that mostly holds. The next post is about the pipelines where it doesn't: five queues, five functions, and still one program.

PS: Which step in your pipeline is the slow one? I'd bet it's the one calling someone else's API. Ping me, let's chat.

Get the next one by email

Every post here goes out by email too - one a week.
.NET, messaging, and distributed systems, with the trade-offs the docs leave out.