A message queue comprises of two terms message and queues. Let’s understand them before understanding the message queue.
Message – A message is a data passed between a sender and receiver applications. It is anything like a plain message or a status code or a command to execute some task, etc.
Queue – A queue is a series of programs waiting for execution in a first-come-first-serve manner.
A message queue allows applications or services to asynchronously communicate with each other by sending messages and storing the message in a temporary buffer (called queue) if the destination program is busy or not active.
Asynchronous behaviour
The message queue provides an asynchronous communication protocol which means the producer puts the message/task into the queue and continues with its processing instead of waiting for an immediate response. The consumer can process the message/task anytime and notify the consumer.
Message Queue Architecture
The message queue architecture consists of client applications called producer and server or destination services called a consumer. The producer creates messages and sends them to the message queue. The consumer then retrieves the message from the queue and process them.
The message remains in the queue until a component called the consumer doesn’t retrieve the message and does something with it.
Decoupled and Scalable
Since most of the applications today are cloud-based and are decoupled into smaller distributed and independent blocks (because it is easier to develop, deploy and manage), the message queue helps to keep up coordination and communication between these independent and distributed services/application.
How?
Imagine an architecture where a process after performing its task needs to pass the execution result to the next independent second process to continue. For this, it needs to invoke other processes, and if the second process is down the first process will also go into the waiting state.
But with the help of messaging queue, the first process needs not to invoke other processes every time instead it can complete its task and push the message/result of execution to the message queue. The second process can fetch and execute the message from the message queue when it’s ready.
This ways of handling make the system scalable and easy to maintain.
Types of Messaging Queues
Point-to-point
In point-to-point messaging, many producers and consumers can use the queue same time but each message can only be processed, by a single consumer. The producer must know the information about the receiving application(ex: queue name, etc) before sending a message.
Publish/Subscribe
Publish/Subscribe messaging, allows a (single) message to be processed by more than one consumer. It is an asynchronous service-to-service communication mostly used in a serverless and microservices architecture.
Here we use something known as message topic (sibling of message queue). To broadcast a message publisher pushes the message to the topic. All the components subscribed to that topic can receive the broadcast. Read “What is Pub/Sub messaging?” for more details.
Did, we miss something or do you want to add some other key points?🤔
Please comment.
Do you know System Design is a very important topic in product based company interviews and almost every tech giant asks it? At Nlogn we have a dedicated section for system design to help you prepare. Read here.