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.
- The producer publishes a message: a body, an operation type and attributes — priority, time to live.
- The broker stores the message until it has been delivered; the order is usually FIFO: first in, first out.
- The consumer takes messages when it is ready — the queue smooths out load peaks.
- After processing it acknowledges (acks) the message — only then does the broker delete it.
- 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:
| Guarantee | What it means | How it is achieved |
|---|---|---|
| at-most-once | a message is delivered at most once and may be lost | sending without acknowledgement: fast but unreliable |
| at-least-once | the message will arrive but may be delivered more than once | acknowledgement after processing and retries; the consumer must be idempotent |
| exactly-once | exactly once — nothing lost and nothing duplicated | transactions 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
Streamstream for small systems.
Sources
the term is written from these pages- OASIS — AMQP 1.0, Part 0: Overview (стандарт от 29.10.2012)docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-overview-v1.0-os.html(opens in a new tab)
- RabbitMQ — документация по очередям: FIFO, порядок доставки и подтвержденияrabbitmq.com/docs/queues(opens in a new tab)
- Amazon SQS — Developer Guide: надёжность и гарантии доставкиdocs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html(opens in a new tab)
Verified 8 Nov