Message queue
A message queue is a buffer that holds messages from one program until another is ready to process them, so the two can work, fail and scale independently.
Photo: Stephen Dawson / Unsplash
A message queue is a buffer that holds messages sent by one program, the producer, until another program, the consumer, is ready to process them. The two never talk to each other directly: the producer drops a message into the queue and carries on with its work, and the consumer takes messages out at its own pace, usually in the order they arrived. Think of the order rail in a restaurant kitchen: waiters keep pinning up orders, each cook takes the next one when free, and a sudden rush makes the rail longer instead of overwhelming the cooks. In IoT the producers are often devices, or the gateways and brokers that collect their telemetry, and the consumers are the services that store, analyze and act on it.
How does a message queue work?
A producer sends a message, the queue stores it, and a consumer fetches it, processes it and then confirms. Only after that confirmation, called an acknowledgement, does the queue delete the message. If the consumer crashes before confirming, the message is delivered again, so nothing is lost when a service fails halfway through.
Amazon’s queue service, SQS, shows the mechanism clearly. A message that has been received stays in the queue but is hidden from other consumers for a visibility timeout, 30 seconds by default; if the consumer does not delete it in time, it becomes visible again for another attempt. A message that keeps failing can be moved to a dead-letter queue, a separate queue where people can examine it without blocking the others. Queues also keep messages only for a while: SQS keeps them for 4 days by default, adjustable from 1 minute to 14 days.
Order is usually first in, first out, with exceptions. The RabbitMQ documentation describes a queue as “an ordered collection of messages” but notes that message priorities and redelivery can change the order.
What is the difference between a message queue and publish-subscribe?
In a queue, each message is handled by one consumer; in publish-subscribe, each message is copied to every subscriber. A queue divides work: start three consumers on the same queue and they share the load, each taking different messages. Publish-subscribe spreads news: a temperature reading published to a topic reaches the dashboard, the alarm service and the archive at the same time.
MQTT is publish-subscribe, but its broker also queues. The MQTT 5.0 standard, approved by OASIS on 7 March 2019, has the broker keep, for a client with a stored session, the QoS 1 and QoS 2 messages still waiting to be sent to it, so a device that was offline receives them when it reconnects. Apache Kafka is a third model, a log: events are not deleted after consumption, each topic keeps them for a period set in its configuration, and every consumer can read the stream from its own position or replay it from the start. MQTT vs Kafka compares the two in detail.
What delivery guarantees does a message queue offer?
Usually one of three: at most once, at least once or exactly once. “At most once” means a message may be lost but is never repeated; “at least once” means it is never lost but may arrive twice; “exactly once” means each message is processed one time only, which costs extra work to achieve. MQTT uses the same three names for its Quality of Service levels 0, 1 and 2.
At least once is a common choice. Amazon SQS standard queues offer at-least-once delivery, warning that “a message may be delivered more than once”, and order on a best-effort basis; its FIFO queues add strict order within a group of messages and exactly-once processing. The practical rule is to make consumers tolerate repeats: if the same meter reading arrives twice, the energy total must not count it twice.
Why do IoT systems use message queues?
Because devices produce data in bursts and the systems behind them fail, restart and grow at different speeds. When a mobile network comes back after an outage, a whole fleet of devices may reconnect at about the same time and send the readings they stored while offline. A queue between data ingestion and the database absorbs that peak: the backlog grows, then drains as consumers catch up. If the database is down for maintenance, messages wait instead of being lost. If the fleet doubles, more consumers can be added without touching the devices.
Queues also make the parts independent. The service that receives device data does not need to know which applications use it, and a new analytics job can start reading without changing anything upstream. This is the core idea of event-driven architecture, and it is why the IoT data pipeline usually has a queue or a log right after ingestion.
What are examples of message queues?
Open-source brokers such as RabbitMQ, log-based systems such as Apache Kafka, and the queue services of the big cloud providers, such as Amazon SQS. The AMQP protocol lets programs and brokers from different vendors exchange queued messages: the AMQP 1.0 specification, an OASIS Standard since 29 October 2012, describes it as a binary protocol “for the reliable exchange of business messages between two parties”. The choice usually comes down to three questions: does every message need one consumer or many, must old messages be replayable, and who will run the system. For stream processing and replay a log fits better; for handing out tasks to workers a classic queue is simpler.
Part of the IoT Glossary. Spotted an error or a missing term? Tell us.