Skip to content
Theory · Why message brokers · lesson 3 of 5

What a broker decouples

When the order service calls the email service directly, the two depend on each other more than it seems. An order goes through only if both are running right now and the email service keeps up with the flow of orders. A broker removes these dependencies, but you pay for that.

Recall the phone call and the courier service. To agree on something by phone, your friend must be reachable in the same minute, pick up and talk at your pace. The courier service needs none of this. It takes a parcel at night, keeps it while your friend is away, and delivers a hundred parcels one by one if you hand them in at once.

Coupling is the dependence of one service on another: how much its work suffers when the other is down, slow or changed. A synchronous call creates it in three respects at once.

Three kinds of coupling

In time

With a call, both services must run at the same moment. If the email service restarts after a new version is deployed, no orders are placed during those seconds.

With a broker, the producer and the consumer run at different times. The producer writes messages while the consumer is down, and the consumer reads them after it starts.

In availability

In a chain of calls, a failure of any link breaks the whole request. If each of three services is available 99.9% of the time, a chain of them is available about 99.7% of the time:

  • one service is down about 9 hours a year;
  • a chain of three, about 26 hours a year.

With a broker, a crashed consumer does not bring down the producer. The producer needs only the broker itself, so the broker runs on several servers at once to survive the loss of one of them.

In load

With a call, the receiver must handle the load at the sender's pace. During a sale the shop takes 1000 orders a minute, while the email service sends only 200. Without a broker, 800 calls a minute end in errors or time-outs.

With a broker, messages pile up in it as in a buffer, and the consumer works through them at its own pace. Ten minutes of the sale pile up 8000 emails. If there are almost no orders afterwards, the email service sends them in about 40 minutes. The emails arrive later, but none of them is lost, and the shop never answers with an error.

The producer does not know its consumers

The event "order 42 created" matters not only to the email service. The warehouse reserves the goods, analytics counts the revenue, and the loyalty program adds points.

With calls, the order service knows the address of each of them and calls them one after another. To connect the loyalty program, you have to change and deploy the order service.

With a broker, the order service publishes one event and does not know who will read it. A new service connects to the broker on its own, and the producer's code does not change.

Here the courier analogy stops working: you address a parcel to a specific person, while the producer writes no receiver's name on a message. Consumers tell the broker themselves which messages they want to read.

Event-driven architecture is an architecture in which services talk through events sent via a broker instead of calling each other directly. An event is a message about something that has already happened, so it is named in the past tense: "order created", "payment succeeded".

Interviewers like to ask this: "What is event-driven architecture and what problem does it solve?" A good answer names both the gain and the price:

  • the gain is loose coupling and asynchrony: the producer neither waits for its consumers nor knows about them;
  • the price is eventual consistency and harder debugging.

What you pay

Eventual consistency

Eventual consistency is a property of a system in which the data of different services agree not at once but with a delay. The order is already stored in the order service, but the warehouse learns about it only when it reads the event. In between, the two services see the world differently.

Repeated deliveries

A broker can deliver one message twice, for example if a consumer crashed before it told the broker it had processed the message. The consumer must be ready for a repeat, or the customer gets two emails or two charges. What exactly a broker promises on a failure is covered later in the course.

Harder debugging

The error of a synchronous call shows at once: the request failed, and its trace tells you at which call. With a broker, the chain is smeared across services and across time. The email never arrived, yet the order service answered "done". To find where the event got lost, you put the request id into the message and look for traces of the event in every service by that id.

On top of this comes the broker itself: one more server to watch.

Common mistake

Showing the user data from a service that has not read the event yet. The customer placed an order and opened "My orders" right away, but that list is built by a service that learns about orders from events. The event has not arrived yet, the list is empty, and the customer places the order a second time.

So right after an action, show the result from the reply of the service that performed it, or say plainly that the order is being processed.

Key points

  • A broker removes coupling in time, in availability and in load; a load peak piles up in the broker, and the consumer works through it at its own pace.
  • The producer does not know its consumers, so a new receiver connects without changes to the producer's code.
  • Event-driven architecture: services talk through events via a broker, not through direct calls.
  • The price is eventual consistency, repeated deliveries and harder debugging.