Advanced Message Queuing Protocol
Ritvik Shukla
Department of Computer Science Engineering
School of Engineering and Technology
Central University Of Rajasthan March 2, 2023
What are Distributed Systems?
Distributed systems are software systems that run on multiple computers or nodes
and work together to achieve a common goal. In a distributed system, each node
communicates with other nodes by exchanging messages over a network. The
nodes can be located in the same physical location or in different locations across
the globe.
Distributed systems are designed to provide high availability, scalability, and fault
tolerance. They are used to solve problems that cannot be easily solved by a single
computer or centralized system. Some examples of distributed systems include web
applications, cloud computing platforms, online gaming systems, and social
networking platforms.
CURAJ Advanced Message Queuing Protocol 3/47
What is Message Queuing?
A message queue is a software component that allows applications to exchange
messages or data with each other asynchronously. It acts as an intermediary
between the sender and the receiver, ensuring reliable delivery and decoupling the
sender and receiver so that they can operate independently.
Message queues are often used in distributed systems where applications may be
running on different machines or in different processes. They provide a way for
applications to communicate without needing to know the exact details of each
other’s implementation or availability. Common implementations of message queues
include RabbitMQ, Apache Kafka.
CURAJ Advanced Message Queuing Protocol 4/47
How it work?
The working of a message queue involves several components, including the sender,
the receiver, and the message queue itself.
•Sender sends a message: The sender application creates a message and sends
it to the message queue.
•Message is stored in the queue: The message is stored in the message queue
until a receiver retrieves it.
•Receiver retrieves the message: The receiver application polls or subscribes to
the message queue and retrieves the next available message.
•Message is processed: The receiver processes the message according to its
requirements.
CURAJ Advanced Message Queuing Protocol 6/47
How it work?
•Message is acknowledged: Once the receiver has processed the message, it
sends an acknowledgment to the message queue to confirm that the message
has been successfully received and processed.
•Message is removed from the queue: The message queue removes the
message from the queue and updates its internal state accordingly.
CURAJ Advanced Message Queuing Protocol 7/47
What is Message Broker?
A message broker is an intermediary software component that facilitates
communication between different applications or services by managing the
exchange of messages.
In a distributed system, applications or services may need to communicate with
each other asynchronously, but direct communication between them may not be
possible or efficient. In such cases, a message broker acts as a middleman,
receiving messages from one application or service and forwarding them to one or
more other applications or services.
Message brokers provide several functions, including message routing, message
filtering, message transformation, and message persistence.
CURAJ Advanced Message Queuing Protocol 8/47
Common Brokers Used
Examples
Some of the common examples of message brokers used are RabbitMQ, Apache
Kafka, Apache ActiveMQ etc.
CURAJ Advanced Message Queuing Protocol 9/47
What is AMQP?
AMQP (Advanced Message Queuing Protocol) is an open standard messaging
protocol that enables reliable and efficient communication between applications or
services in a distributed system.
AMQP defines a messaging format and a set of rules for message routing and
delivery, allowing applications to communicate asynchronously and reliably over a
network. It provides features such as message queuing, message acknowledgment,
and message routing to ensure that messages are delivered to their intended
destination in a timely and secure manner.
CURAJ Advanced Message Queuing Protocol 10/47
What is AMQP?
AMQP is designed to be interoperable, meaning that it can be used with different
programming languages, operating systems, and messaging systems. It is widely
used in various industries, including finance, healthcare, e-commerce, and
telecommunications, to name a few.
Note
Popular messaging systems that implement AMQP include RabbitMQ and Apache
ActiveMQ.
CURAJ Advanced Message Queuing Protocol 11/47
Working of AMQP
CURAJ Advanced Message Queuing Protocol 12/47
Working of AMQP
AMQP works on a client-server model, where the messaging system consists of one
or more message brokers (servers) and multiple client applications that interact
with the brokers to send and receive messages.
The basic workflow of AMQP is as follows:
•A client application sends a message to a message broker using a specified
queue or exchange.
•The message broker receives the message and performs routing and delivery
based on the message’s destination, using AMQP routing rules.
•If the message is sent to a queue, it is stored in the queue until a consuming
client retrieves it.
CURAJ Advanced Message Queuing Protocol 13/47
Working of AMQP
•If the message is sent to an exchange, the message broker routes it to the
correct queue or multiple queues based on a set of predefined routing rules.
•A client application consumes the message from the queue and sends an
acknowledgment to the broker indicating that the message has been received
and processed.
CURAJ Advanced Message Queuing Protocol 14/47
Important Points Related to AMQP
Note
AMQP uses channels to multiplex multiple independent conversations over a single
TCP connection, reducing the overhead of opening and closing multiple connections.
Note
AMQP supports different messaging patterns such as publish/subscribe,
point-to-point, and request/response, allowing developers to choose the best
pattern for their use case.
CURAJ Advanced Message Queuing Protocol 15/47
Messaging Patterns in AMQP
There are different messaging patterns that AMQP supports like:
•Publish/Subscribe
•Point-To-Point
•Request/Response
CURAJ Advanced Message Queuing Protocol 16/47
Publish/Subscribe Pattern
The publish-subscribe (pub/sub) messaging pattern is a communication pattern
used in distributed systems where publishers send messages to a topic or channel,
and subscribers receive those messages based on their interest in that topic. In this
pattern, the publishers and subscribers are decoupled from each other, meaning
that publishers do not need to know who the subscribers are, and vice versa.
When a publisher sends a message to a topic, the message is distributed to all
subscribers that have subscribed to that topic. Subscribers can receive messages
in a variety of ways, such as through a message queue or by using a WebSocket.
CURAJ Advanced Message Queuing Protocol 17/47
Point-To-Point Pattern
The point-to-point messaging pattern is a communication pattern used in
distributed systems where messages are sent from one sender to one specific
receiver. In this pattern, a message is sent to a specific queue, and only one
consumer can receive and process that message from that queue.
When a sender sends a message to a queue, the message is stored in the queue
until a receiver processes it. When a receiver processes a message, it is removed
from the queue and cannot be received by any other receiver. This ensures that
each message is processed only once by one receiver.
CURAJ Advanced Message Queuing Protocol 18/47
Request/Response Pattern
The request-response messaging pattern is a communication pattern used in
distributed systems where one application sends a request message to another
application and expects a response message in return. In this pattern, the sender
sends a request message and waits for a response from the receiver before
continuing with its work.
When a requester sends a request message to a receiver, it waits for a response
message before continuing with its work. The receiver processes the request
message and sends a response message back to the requester. The response
message contains the result of the request or an error message if the request could
not be processed.
CURAJ Advanced Message Queuing Protocol 19/47
Exchanges
Exchanges take a message and route it into zero or more queues. The routing
algorithm used depends on the exchange type and rules called bindings. AMQP
brokers provide four exchange types:
•Direct exchange
•Fanout exchange
•Topic exchange
•Headers exchange
CURAJ Advanced Message Queuing Protocol 21/47
CURAJ Advanced Message Queuing Protocol 22/47
Direct exchange
A direct exchange delivers messages to queues based on the message routing key.
A direct exchange is ideal for the unicast routing of messages (although they can be
used for multicast routing as well).
A queue binds to the exchange with a routing key K When a new message with routing
key R arrives at the direct exchange, the exchange routes it to the queue if K = R
CURAJ Advanced Message Queuing Protocol 23/47
CURAJ Advanced Message Queuing Protocol 24/47
Fanout exchange
When a message is published to a fanout exchange, the exchange sends a copy of
the message to every queue that is bound to it. This means that every message sent
to a fanout exchange is delivered to all the queues that are bound to it, regardless of
the routing key or any other message attribute.
CURAJ Advanced Message Queuing Protocol 25/47
CURAJ Advanced Message Queuing Protocol 26/47
Topic exchange
The topic exchange is a type of exchange where messages are routed to queues
based on a pattern match between the routing key and the binding key.
When a message is published to a topic exchange, the exchange compares the
routing key of the message with the routing key of each queue that it is bound to.
The routing key can contain one or more words separated by dots, and can also
contain wildcard characters:
•”*” (star) can substitute for exactly one word.
•”#” (hash) can substitute for zero or more words.
CURAJ Advanced Message Queuing Protocol 27/47
CURAJ Advanced Message Queuing Protocol 28/47
Headers exchange
Header exchange is a type of exchange where messages are routed to queues based
on message attributes (headers) that match the values specified in the binding.
When a message is published to a header exchange, the exchange examines the
message headers and compares them to the headers specified in each binding. The
exchange then delivers the message to all the queues that have bindings with
header values that match the message headers.
CURAJ Advanced Message Queuing Protocol 29/47
Ecommerce Microservice
What I built?
Implementing a Microservice using RabbitMQ as a message broker.
•Auth Service
•Product Service
•Order Service
CURAJ Advanced Message Queuing Protocol 31/47