TL;DR
- Message-oriented middleware (MOM) puts a broker between processes. Applications call a simple send/receive API and never touch sockets.
- Messages carry a destination, not an address, so senders don’t need to know where receivers live. That’s location decoupling.
- Guaranteed messages are persisted by the middleware until a receiver is back. That’s availability decoupling.
- Queues absorb bursts and let you add competing consumers to drain them. That’s pace decoupling, and it gives you elasticity almost for free.
- The price: the middleware becomes critical infrastructure, and you have to think asynchronously about ordering, duplicates and eventual consistency.
In the last part we scored files, databases, sockets and RPC against seven objectives. Nothing hit all seven. Files decoupled nicely but couldn’t signal or reach far. Sockets and RPC were immediate and far-reaching but coupled in every way. We put the lot into a basket of requirements and gave it a shake.
What fell out was a product category: middleware, or integration products. This is the space a whole industry grew up to serve, and it’s where my own employer, Solace, has its roots. The specific answer to our basket is message-oriented middleware, or MOM if you’ve met it in academic papers.
IBM’s MQSeries in the early 1990s is the best-known early example, and the Java Message Service (JMS) API standardised how Java applications talk to this kind of product later that decade. You don’t need the history to follow along, though. Let’s build it up one feature at a time using an online store.
Everything is a message
Start with a black box in the middle of the system. Every process connects to it using an API the middleware provides, and all communication is packaged as a discrete message. A message might be a simple signal (“something happened”) or a payload of state. Either way, it’s one self-contained thing to send.
The API handles the connection to the middleware: the TCP, the framing, the reconnects. The middleware then works out which processes are interested in each message, copies it as needed and delivers it.
What we get: abstraction from socket programming. As a developer, you send and receive messages. What happens underneath is someone else’s problem.
Messages have a destination
Messages aren’t dumb blobs. You can annotate them, and the most important annotation is a destination.
Say our store’s web front end takes customer requests. New orders should go to an order processor. Cancellations should go to a separate cancellation processor. We split those functions on purpose, so the order processor isn’t cluttered with cancellations.
The front end sends new orders to orders/new and cancellations to orders/cancel. It never says where the processors are. The middleware routes each message to whoever is consuming that destination, whether that’s one process, five copies of it, or something in another data centre.
What we get: location decoupling. The sender addresses what it’s sending, not where the receiver lives. Move the receiver, scale it out or replace it, and the sender doesn’t change.
Destinations usually come in two flavours. A topic delivers a copy to every interested subscriber, which is publish/subscribe. A queue delivers each message to one consumer, which is point-to-point. Hold on to that distinction. It comes back in the patterns series.
Messages can be guaranteed
Not all messages matter equally. A heartbeat saying “I’m alive” is useful if someone’s listening right now and worthless a minute later. If nobody’s there, dropping it is fine.
Orders are different. An order is precious, and losing one means losing money and a customer. So when the front end sends a new order, it marks it as guaranteed (also called persistent or durable).
Now suppose the order processor crashes before the order reaches it. The message doesn’t disappear. The middleware persists it and holds on to it. When the order processor comes back, whether it’s the same instance restarting or a new copy started elsewhere, the middleware delivers the backlog.
The broader point is that you can choose a quality of service per message: fire-and-forget for things that go stale, guaranteed for things you can’t afford to lose.
What we get: availability decoupling. The front end isn’t held up because something downstream is unavailable. Responsibility for eventual delivery moves to the middleware.
Messages can be queued
A close cousin of guaranteed delivery is buffering. Imagine you’ve just launched a hot new product and the front end is flooded with orders. The order processor can’t keep up.
With a queue in the middle, the burst lands in the queue and the order processor works through it at its own pace. The queue acts as a shock absorber.
It gets better. If each order can be processed independently, in any order, you can start a second order processor reading from the same queue. The two share the load as competing consumers. When the spike passes and the queue drains, you shut the extra one down. That’s elasticity, provided by the transport rather than built into every application.
What we get: pace decoupling. Fast publishers and slow consumers no longer matter to each other.
The scorecard
For the first time, a single row is green across the board. Messaging gives you the decoupling of a file drop with the immediacy and reach of a network connection, without writing socket code.
Nothing is free
I’d be doing you a disservice if I stopped there. Messaging changes the shape of your problems:
- The middleware is now critical infrastructure. Everything flows through it, so it needs high availability, monitoring and someone who owns it.
- You have to think asynchronously. You send work off and get told later. That’s harder to reason about than a function call.
- New edge cases appear. Guaranteed delivery usually means at least once, so consumers need to cope with duplicates. Competing consumers can process messages out of order. Data across services becomes eventually consistent.
These are all solvable, and most of the patterns in the upcoming EDA Design Patterns series exist precisely to solve them. But they’re real, and you should know about them going in.
Wrapping up
Message-oriented middleware delivers what the earlier approaches couldn’t: messages instead of sockets, destinations instead of addresses, persistence instead of “hope they’re up”, and queues instead of “slow down for me”. Location, availability and pace, all decoupled.
Next up, the question that always comes up at this point: “Isn’t this what REST is for?” We’ll run the same store over HTTP and look at the real dividing line, which is synchronous versus asynchronous communication.