AMQP, RabbitMQ simple explanation and great resources
Motivation
The need of a way for communication between modules that one module can send messages/signals to declare something, and another module receive this messages/signals and perform some actions based on that.
Message broker, your postman!
A message broker Is an actor that responsible for delivering messages from source to destination.
Setting Standards for generalization
As we need something that works with any technology or source or destination, there must be some sort of standards to apply so any message broker provider should follow to be acceptable.
Welcome to AMQP : Advanced Message Queuing Protocol
AMQP includes a set of standards that control the entire messaging process in AMQP message brokers. It allows two parties to communicate by sending and receiving messages between them.
AMQP components
Exchange
Exchange is the first receiver of message in AMQP and responsible for delivering the message to right queue/destination and it has 4 types:
It deliveries the message to one and only one destination based on routing key.
This exchange works like broadcasting channel that deliver the message to all associated queues.
Topic exchanges route messages to queues based on wildcard matches between the routing key and the routing pattern, which is specified by the queue binding. Messages are routed to one or many queues based on a matching between a message routing key and this pattern.
This exchange deliver messages based on header values on messages.
Binding and Routing key
Binding is the connection that wires the exchange with the queue, As any queue must be bounded to exchange using a binding and routing key just a name to distinguishes between bindings.
领英推荐
Queue
Queue is the real holder of the messages that source sends and the messages still in queue till one consumer exists then the message broker push message to the consumer and uses FIFO mechanism in working.
Connection and Channel
Message broker is a server and any application want to use it should establish a connection between him and server, Channel is virtual connection to reuse the real connection instead of dropping and re-creating the connection.
AMQP component interaction
RabbitMQ , implementation of AMQP
RMQ is message broker server that implements AMQP standards so it works exactly same as explained previously.
As we know, Nothing is perfect !
pros
By adding message broker as a way communication your modules became independent from each other one module declare the message/event and any other module can consume this and perform the action and any part of this cycle can be replaceable any time
RMQ provides acknowledgment facility which that any module consume message should acknowledge that it processed successfully if not the message will return to the queue again to another processing.
Note: you have to pay attention to this as RMQ will try to push failed messages again and again till it processed , so you have you have to add try/catch block to consumer code or add dead-letter extension so that any failed message could be delivered to another queue for investigation and another way of handle
messages can be stored on disk for avoid any data loss during any server shutdown or failure
You can scale your message broker by adding resources or server instances.
In case of hot you can multi process messages by enable multi thread consuming feature
Cons
There is a lot components regarding to this protocol which makes it somehow complex to understand or use.
RMQ server pushes messages to consumers, Whenever there is a consumer send the message to it, in case of huge amount of message consumer could crash if it can not handle this number of messages.
Resources