%%
date:: [[2022-09-09]]
%%
# [[Message Broker]]
A message broker is a component solely focused on managing the traffic of messages sent between other components. Message brokers are [[An external monitor that oversees the flow of work improves general performance|external monitors]] and exist to improve performance and functionality. Here are some of the things a message broker might be concerned with:
- Messages are processed in a timely manner.
- Only messages that a component needs to process go to each component.
- Messages are processed sequentially.
Message brokers essentially act as intermediaries between applications, effectively decoupling data streams from the components. [^maarek]
<iframe width="560" height="315" src="https://www.youtube.com/embed/57Qr9tk6Uxc" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
## Why use message brokers?
Why do we need an intermediary? Why can't one application just talk directly to another? Well, message brokers are useful in the following situations: [^odonnell]
- **Outages**. When an application that would normally be sent messages is down, a message broker stores the messages instead, and forwards them on whenever the application is up again.
- **Poor performance**. Similar to what happens when there are outages, message brokers provide storage for messages that can't yet be forwarded by an application that is performing poorly or is struggling to keep up with processing.
- **Firewalls**. If there are firewalls between applications, or they're not on the same network, the firewalls can be reconfigured to send messages to a single message broker instead of having to individually whitelist all other applications.
- **Translation**. Applications may not always speak the same language. A message broker can be useful in translating message from one language to another.
## Message brokering approaches
There are three main ways that a message broker handles messages.
### [[Message queuing]]
In a message queue, the broker enforces an order for the messages that are sent to it, processing older messages first and later messages last. This is the most traditional form of message brokering.
[[RabbitMQ]] is an example of a message broker that employs message queues.
### [[Publish-subscribe|Pub/Sub]]
Pub/Sub stands for *publish/subscribe* and is both a model and a product: [[Google Pub-Sub]] is an implementation of the model, as is [[Azure Service Bus]].
In pub/sub, components *publish* information in categories and consumers can then *subscribe* to categories relevant to them.
### [[Event streaming]]
Event streaming is a hybrid of the previous message queuing and pub/sub models. In event streaming, messages are treated as "events" (and for this reason work best within [[Event-driven architecture|Event-based architecture]]s).
Tools that use event streaming are:
- [[Apache Kafka|Kafka]]
- [[Azure Event Hub]]
[^maarek]: Maarek, S. (2019). _Apache Kafka in 5 minutes_. Retrieved from https://www.youtube.com/watch?v=PzPXRmVHMxI
[^odonnell]: O'Donnell, C. (2021). _Introduction to message brokers_. Retrieved from https://www.youtube.com/watch?v=57Qr9tk6Uxc .