Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Here's a Ruby poject that implements the middleware pattern. From the description, I have no idea what the pattern is, what it's useful for, and why other solutions wouldn't work as well.

What is the middleware pattern, and what are its advantages and drawbacks?

share|improve this question
Good question; the project seems to assume a definition that has little to do with the common usage of the term Middleware. –  Michael Borgwardt Jul 1 '13 at 12:04
...and that taking an existing term and slapping "pattern" on the end makes it something more than it actually is. –  Blrfl Jul 1 '13 at 12:11

2 Answers 2

The author of the project you linked describes middleware as a "state engine" for business logic, akin to Windows Communication Foundation. That's not the usual definition for middleware (software that glues two or more heterogenous applications together), and I don't think the "middleware pattern" is a thing.

I think the author is playing a bit fast and loose with his terminology.

share|improve this answer

Based on what I can tell from the example, it basically allows an arbitrary set of pre-action and post-action hooks that are chained in a defined way (hence, you build the stack it goes through) on a particular initial state of an input (in this case, nil, but there's no requirement for it to be such).

Their motivation appears to be in the context of HTTP requests where you might alter request/response properties in any given way. I don't think this pattern is overly useful in the general sense (and calling it "middleware" is certainly a misappropriation).

The advantage appears to be separation of concerns - e.g. something handles the generalized system for hooking and chaining operations to an input. Any subscription to this service could easily be deployed in other contexts and plugged right into a sequence of operations. It's also easy to guess the side effects of inserting any particular operation into the chain - as long as it's done statically.

The disadvantage to me would be that the context is completely removed - it's not clear what you're operating on without a strong naming convention or what the impact of any change to the subscribers is without understanding the context. You would have to put both the caller context and what it does into the naming convention in order to have it be understandable from both sides.

To me, it feels like it would be better to represent the operations as a whole concept; e.g. setting all properties in one spot. I can see the need for perhaps some portion of the application to inject properties and it's a much more interesting usage, for example, maybe in the context of an HTTP request you have something that changes the response type to XML while it's spitting out XML.

However, the way this is set up, you'd have to ensure that was the only spot setting the response type, the only one that will for a particular request, or just the last one which would overwrite any that came before it. Otherwise you'd run into issues.

I'm not sure that solving the problem this way is a compelling pattern, but I fully concede that might be because I looked at and thought about it for 30 minutes. I'm guessing somebody put more time than that into making it, so there could be things I'm missing.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.