Modern applications are becoming increasingly intelligent and complex. To handle the increasing complexity and scalability challenges, developers split applications into a set of microservice instances. However, this approach can create challenges, as developers often encounter communication issues related to software components while processing data. because those components are often built using different programming languages. This is where the message broker comes in. A message broker is like a middleman that helps different services communicate with each other. In this article, we will explore the concept of a message broker in detail and learn how it is crucial to enabling communication within a distributed system.
What is the purpose of using a message broker?
Message broker is a software that acts as a middleware between different services. It is a standardized medium that handles the flow of data between the services and allows them to communicate internally within an application. It supports asynchronous messaging. Now you will ask what asynchronous communication is. Asynchronous messaging is a method where the sender and receiver don't need to rely on each other to exchange messages. Both participants are free to start, pause, or resume producing or consuming messages without waiting for others. This improves fault tolerance and resiliency in the system and makes it easier to scale the software. Message brokers ensure that messages are properly delivered between different components. if a component is not available at the time a message is sent. The message is stored in a queue and delivered when the component becomes available again. This ensures that no messages are lost, even if components are temporarily unavailable due to connectivity or any other issue.
Protocols used in Message Broker
Message brokers use different protocols for communication. Here are some popular protocols commonly used by message brokers:
AMQP (Advanced Message Queueing Protocol)
MQTT (Message Queueing Transport)
SMQP (Simple Message Queue Protocol)
STOMP (Simple Text-Oriented Messaging Protocol)
REST (Representational State Transfer)
Different message brokers may support different protocols, so it's important to choose a message broker that supports the protocols required by your application.
Message brokers generally use two messaging patterns:
In this messaging approach, there is a direct relationship between the sender and receiver. The sender creates a message and directly delivers it to a specific destination, which is typically the address of the receiver. For example, it can be used in financial systems for transferring funds between accounts.
Publish/Subscribe or pub/sub messaging:
This messaging pattern is like the broadcasting approach, where the producer publishes messages for a specific station or topic, and multiple consumers connected to or subscribed to the station can consume them. For example, a news agency publishes new articles about cricket. These articles are sent to 'sports' topic. and subscribers who have subscribed to the topic can receive the articles and display them in their components.
Here are some real-life use cases for message brokers:
In an e-commerce system, a message broker can be used to process the orders.
In financial services, a message broker can be used to process transactions.
used to handle notifications and messages on social media.
In a streaming service, a message broker can be used to handle video encoding and delivery.
Examples of Message Brokers
Memphis: the next-generation open-source message broker. A simple, robust, durable, cloud-native project written in Go
RabbitMQ: Released in 2007. lightweight, easy-to-deploy message brokers written in Erlang.
Apache Kafka: free and distributed event streaming platform that operates on a pull-based model. This means that the consumer of the messages must actively request messages from a particular offset.
Apache ActiveMQ: open-source message broker based on the Java Message Service (JMS) specification, which provides a standard API for sending and receiving messages between services.
A message broker can be valuable software for developing distributed systems, but it may not always be the best solution. Simple applications with few services, tightly coupled services, limited developers, and low latency requirements are some scenarios where we shouldn't use a message broker. Ultimately, the decision should be based on the requirements, complexity, and scalability of the application.