Bookmarks

Message queue

An intermediary that accepts messages from producers and passes them on to consumers: it works asynchronously, stores messages until they are delivered and re-delivers whatever is not acknowledged.

Also: task queue, MQ

checked 8 Nov

All terms

A producer puts a message on the queue and immediately gets on with its work — the consumer will pick it up when it is free. The queue decouples the parts of a system in time: the producer does not wait for processing, and a failure of one side does not break the other — the messages will wait in the queue. Email delivery, image processing, video transcoding and online shop orders are all built on this.

How it works

A queue has three participants: the producer puts a message on it, the queue itself stores it until delivery, and the consumer takes it away and acknowledges processing.

  1. The producer publishes a message: a body, an operation type and attributes — priority, time to live.
  2. The broker stores the message until it has been delivered; the order is usually FIFO: first in, first out.
  3. The consumer takes messages when it is ready — the queue smooths out load peaks.
  4. After processing it acknowledges (acks) the message — only then does the broker delete it.
  5. If there is no acknowledgement, the message stays and will be delivered again — the at-least-once guarantee.

Delivery guarantees

Networks are unreliable, so queues are expected to provide guarantees. Three basic ones:

GuaranteeWhat it meansHow it is achieved
at-most-oncea message is delivered at most once and may be lostsending without acknowledgement: fast but unreliable
at-least-oncethe message will arrive but may be delivered more than onceacknowledgement after processing and retries; the consumer must be idempotent
exactly-onceexactly once — nothing lost and nothing duplicatedtransactions and deduplication; not supported by every broker: for example, Amazon SQS FIFO queues provide exactly-once processing

In practice at-least-once is the most common choice, and processing is made idempotent: a repeated delivery does not change the outcome. Every message can be given a time to live (TTL), and “poison” messages that fail several times in a row end up in a separate dead-letter queue, where they are dealt with manually.

Example: an order in an online shop

A customer places an order and the online shop puts order.created on the queue. Three consumers read it independently: one reserves the item in the warehouse, another takes the payment, the third emails the customer. If the payment fails, the order returns to the queue and is retried a minute later; if the mail server is down, the emails will go out once it is back up. Black Friday peak load is smoothed out: orders pile up in the queue and the consumers take them at their own pace.

Where the term comes from

The common open protocol for such systems is AMQP: a binary protocol for reliable message exchange between two parties, approved by OASIS as a standard on 29 October 2012. RabbitMQ supports both versions — the earlier 0-9-1 and AMQP 1.0; AMQP 1.0 is also supported by ActiveMQ and Azure Service Bus.

Where it is used

  • RabbitMQ — a broker with message routing and acknowledgements.
  • Amazon SQS — a managed queue service in the cloud: no servers to administer.
  • Kafka — a distributed log, a queue plus a message history, streaming of large flows.
  • Redis — simple queues via lists or the Stream stream for small systems.

Sources

the term is written from these pages

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • message broker

    A broker is a server that hosts queues, plus message routing and transformation; a queue is one of its mechanisms.

  • publish–subscribe (pub/sub)

    In pub/sub a message goes to all subscribers of the topic, while in a queue each consumer receives its own message from the shared queue.

In depth in the references

where the topic is covered in detail