Message Queues vs Message Brokers
If you like the free content I put out, consider subscribing to my newsletter on substack to get a well-researched article every week delivered straight to your inbox.
As is tradition with my newsletter topics, I selected this topic out of curiosity to study the difference between Message Queues and Message Brokers. Without further ado, let’s get started.
Problem Statement
Before deep diving in, let us understand why they even exist in our architecture.
Imagine you have two services: Service A and Service B. Now, Service A performs any operation(let’s say processed a payment, or completed a booking) and should update this information in a way that all other services (whoever needs it) should be able to consume this information almost immediately.
One way to solve this problem is for Service A to make a blocking call to Service B and update it in real time. But, with an increase in the number of services, Service A would have to call N number of services and it would have to consider all the edge cases: what if any downstream service is down, or the request gets timed out, etc. It’s simply not scalable.
That’s why we introduce asynchronous behavior in our architecture which works on a simple principle: decouple the producer and the consumer. Producer services produce the data in the form of a message in a queue-like structure and consumer services consume the data from these queues.
Message Queues
Message Queues are a single-queue data structure that stores messages temporarily in memory or on disk depending upon the configuration. These messages are produced by producers on one side and consumed by consumers on the other side. The key things to note here are:
For reasons listed above, this type of messaging pattern is also called as one to one or point to point communications.
Message Queues usage: When you send an email, it goes into the recipient's inbox, where it waits until they choose to open and read it. This acts like a message queue, as the email is stored until the recipient is ready to access it.
Message Brokers
As per Wikipedia, “message broker is an intermediary computer program module that translates a message from the formal messaging protocol of the sender to the formal messaging protocol of the receiver. It is used for storing and delivering the message to the required destination.”
Message Brokers or Message Queues are both essentially used for asynchronous communication where the sender sends the message without caring about the fact if the receiver is ready to receive the message or not.
The main difference is Message Brokers consist of multiple Message Queues as highlighted in the above diagram. Each message queue could represent one type or category of data that consumer services can subscribe to and process.
For example: In a payments company, the message broker could consist of multiple message queues. One message queue could contain data about all the successful/failed transactions. Another message queue could contain data about promotions or offers. The consumers can listen to whatever data they are interested in. This is called the traditional pub/sub model.
Do note that a message queue doesn’t inherently handle routing, filtering, or broadcasting to multiple consumers – these are functions managed by the broker around the queue. Thus, can you imagine, that managing a broker that consists of multiple queues is a tedious task and has operational complexity involved in it.
Examples
All Message Brokers are Message Queues but all Message Queues are not Message Brokers.
The full-fledged Message Brokers provide more richer feature set compared to Message Queues like routing, load balancing, persistence of messages, scalability, delivering in a fan-out fashion, etc. That’s why nearly all of the popular technologies out there that are used for large-scale distributed systems can be used as a Message Broker with some configuration because they use Multiple Queues.
If you want to use anyone of them as a Message Queue, then that’s fine as well.
When to use each?
Use a message queue when you need straightforward, point-to-point communication for task distribution or workload balancing, where messages are only consumed by one worker and are removed after processing.
This is ideal for simple, background job processing with basic reliability needs, as queues keep the system responsive by allowing consumers to handle tasks at their own pace.
In contrast, a message broker is best for complex, multi-pattern communication in systems that need flexible routing, like event-driven or microservices architectures.
Brokers allow for publish-subscribe models, making it easy to send the same message to multiple consumers and support real-time data processing and analytics. They often come with advanced features like replication, fault tolerance, and clustering, making them suitable for large-scale, highly reliable, and scalable systems
Hope you liked this edition.
Please consider liking ?? and sharing with your friends as it motivates me to bring you good content. If you think I am doing a decent job, share this article in a nice summary with your network. Connect with me on Linkedin or Twitter for more technical posts in the future!
Curious Engineer is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.
Software Engineering Leader | Follow for Software Development Insights
2 周Appreciate the content, gave me a small laugh to see the person writing on that whiteboard descending into madness... "Massage Qeeeuees" ??
Engineering @ Oracle || x-Microsoft, VMware || Database Internals || Distributed systems || Build and scale data systems || Real-time Analytics || Big Data || ML and Deep learning |
2 周Thanks for sharing. KIP-932 (https://cwiki.apache.org/confluence/display/KAFKA/KIP-932%3A+Queues+for+Kafka), which supports queues for Kafka. It introduces Share Groups, Many-to-many mapping between partitions and consumers, and entire topics as a single queue. These queues support semantics at least once without any order. Brokers act as Share-Group Coordinators to manage share groups and keep a sliding window of records per partition. Once a record is fetched, it will be marked as acquired by the consumer, and a lock will be held for some configurable duration. The message will be re-sent if the broker does not receive ACK before that duration.
Curious? Senior Software Engineer ? 18k+? Python ? Django ? Flask ? AWS ? Sql ? Support-turned-Dev
2 周Thanks for sharing Vivek Bansal .