DEV Community

Cover image for When you need a message broker
Ilya R
Ilya R

Posted on

When you need a message broker

What is a message broker

A message broker is a software that enables communication and message passing between different components or applications in a distributed system. It plays the role of an intermediary, handling the sending, routing, and delivery of messages from the sender to the recipient. A message broker is typically used in asynchronous communication models where the sender and receiver do not need to interact directly in real time.

Some of the popular message brokers are Apache Kafka and RabbitMQ. They are widely used to process data streams and create streaming platforms. Message brokers play an important role in modern distributed systems by enabling reliable and scalable communication between components.

How do brokers work

Message brokers are based on a publisher-subscriber model. In this model, the sender of a message, called the publisher, does not directly contact the recipient, called the subscriber. Instead, the publisher sends the message to the broker, which then routes the message to the appropriate subscribers. The message broker ensures reliable delivery of messages, manages queues, and handles delivery errors.

When sending a message, the publisher specifies the topic or channel to which the message belongs. Subscribers who are interested in this topic are registered with the broker and expect to receive messages. The broker routes the message to appropriate subscribers based on their preferences and subscription filters.

The message broker can also handle situations where subscribers are temporarily unavailable or unable to process a message. It can store messages in a queue and deliver them to subscribers when they are available again.

When is a broker needed in architecture

A message broker is useful in the following cases:

  • When it is necessary to provide asynchronous communication between components or applications in a distributed system. It may be that a message from a queue can be processed by several services. And instead of sending a message or data to each service in turn, you can load the message into the broker’s queue, and the recipients who listen to this queue will themselves pick up the messages sent to them and process them.
  • When you need to process large volumes of messages and ensure their delivery with high reliability. This reduces the load on services and allows you to be sure that the message or data will definitely reach the desired service. The sender service places messages in a queue, and the receiver service takes each such message from the queue, processes it, and then takes the next one.
  • When the ability to scale and process data streams is needed.
  • When the sender and recipient do not have to interact in real time, but can work independently of each other. Also, brokers allow you to ignore how a particular service works. The sender service only needs to know which queue to send messages to.

Disadvantages of Message Brokers

Although message brokers provide many benefits, they also have some limitations and disadvantages:

  • Single point of failure: When a centralized message broker is used, there is a risk that if it fails, all communication between components will be disrupted. This can lead to data integrity problems and unavailability of services. In some cases, clustering or replication can be used to increase fault tolerance, but this adds complexity and cost.
  • Message Latency: Using a message broker may result in increased message delivery latency. Since messages are routed through a broker, this may add some delay to the overall delivery time. For some applications that require instant data transfer, this may not be acceptable.
  • Management Complexity: Managing and configuring a message broker can be challenging. This includes queue management, message routing, error handling, and scaling. A sufficient understanding of the broker's architecture and configuration is required to use it effectively.
  • Difficulty in Debugging: When messaging problems occur, debugging can be difficult. Due to the intermediary role of the broker and the interaction between various components, identifying and resolving errors may require additional effort and resources.
  • Added Security Complexity: Using a message broker adds complexity in securing data transfers. It is necessary to ensure secure connections, access control and encryption of messages to protect against unauthorized access and information leakage.

In conclusion, I would like to note that message brokers provide a reliable and flexible mechanism for exchanging messages between components in distributed systems. However, brokers also have disadvantages and these disadvantages must be taken into account when developing and implementing a system using message brokers.

Top comments (0)