Reliable Message Delivery with Queues
Learn how to use queues for message deliveries in an application.
An important abstraction in a messaging system is the message queue (MQ). With a message queue, the sender and the receiver(s) of the message don’t necessarily need to be active and connected at the same time to establish a communication, because the queuing system takes care of storing the messages until the destination is able to receive them. This behavior is opposed to the fire-and-forget paradigm, where a subscriber can receive messages only during the time it’s connected to the messaging system.
A subscriber that’s able to always reliably receive all the messages, even those sent when it’s not listening for them, is called a durable subscriber.
We can summarize the delivery semantic of a messaging system in three categories:
At most once: Also known as fire-and-forget, the message isn’t persisted, and the delivery isn’t acknowledged. This means that the message can be lost in cases of crashes or disconnections of the receiver.
At least once: The message is guaranteed to be received at least once, but duplicates might occur if, for example, the receiver crashes before notifying the sender of the reception. This implies that the message has to be persisted in the eventuality it has to be sent again.
Exactly once: This is the most reliable delivery semantic. It guarantees that the message is received once and only once. This comes at the expense of a slower and more data-intensive mechanism for acknowledging the delivery of messages.
We have a durable subscriber when our messaging system can achieve an “at least once” or an “exactly once” delivery semantic and to do that, the system has to use a message queue to accumulate the messages while the subscriber is disconnected. The queue can be stored in memory or persisted on disk to allow the recovery of its messages even if the queuing system restarts or crashes.
The following illustration shows a graphical representation of a durable subscriber backed by a message queue:
Get hands-on with 1400+ tech skills courses.