Skip to content

Queues, pub/sub and consumer lag

Without a queue, a service that needs work done calls the service that does it and waits for the answer. If the second service is slow or down, the first one is slow or down with it. A message queue puts a buffer in between: the producer writes a message describing the work and carries on, and a consumer takes the message when it is ready. The thing in the middle that stores the messages is the broker.

RabbitMQ’s tutorial on work queues gives the main idea: avoid doing a resource-intensive task immediately and having to wait for it to complete. The task is put in a queue, and a worker running in the background takes it later.

A queue does not make the work smaller. It moves the waiting from the caller to the queue, and it lets the producer and the consumers run at different speeds for a while. That is useful, and it has a limit, which this lesson shows.

There are two shapes, and they answer a different question: who gets each message?

  • Work queue. Several consumers take from the same queue, and each message is delivered to exactly one of them. RabbitMQ’s tutorial describes it that way: each task is delivered to exactly one worker, and by default the broker sends each message to the next consumer in turn, so that on average every consumer gets the same number. Adding consumers adds capacity. This is the shape of a job queue.
  • Publish/subscribe. The producer publishes to a place that is not a single queue, and every subscriber receives its own copy. In RabbitMQ’s tutorial on publish/subscribe a fanout exchange broadcasts every message it receives to all the queues bound to it, and each consumer instance has a queue of its own. Adding a subscriber adds a new reader of the same messages, not capacity for the same ones. This is the shape of an event: the order service says that an order was placed, and billing, email and analytics each react.

Mental model. A work queue shares one stream of work among workers, so each message is handled once. Pub/sub copies one stream to many readers, so each message is handled once per reader. In both, the producer does not know who is listening, and the consumers can fall behind.

Challenge

Work queue or publish/subscribe?

A work queue shares messages among consumers, so each message is handled once. Publish/subscribe copies each message to every subscriber. Put each statement under the shape it describes.

Sorting needs JavaScript.

Categories

Work queue
Competing consumers: each message goes to one of them.
Publish/subscribe
Every subscriber receives its own copy of each message.
  1. Line 1 of 6Thumbnails must be made for each uploaded photo, and any free worker may do it.
  2. Line 2 of 6When an order is placed, billing, email and analytics must each hear about it.
  3. Line 3 of 6Adding a consumer makes the backlog drain faster.
  4. Line 4 of 6Adding a subscriber does not slow down the others, because it gets a copy of its own.
  5. Line 5 of 6By default the broker sends each message to the next consumer in turn, so on average each gets the same number.
  6. Line 6 of 6A message published to the exchange reaches every consumer that is listening, not just one.

Check your answers at least once before looking at the answers.

The gap between what has been sent and what has been taken is the consumer lag, or the backlog. The lab models a work queue: producers send at a fixed rate, the consumers take at theirs, and a message that nobody has received yet counts as waiting. It starts at 10 s into the run. Read Producer rate, Rate per consumer and Consumers, and the status sentence below them.

Producers send faster than the consumers can take, so the queue grows. Add consumers or slow the producers and watch the backlog.

Traffic
30 per second

With backpressure on, the queue holds at most 100 messages and producers are slowed to what it accepts. Without it, the queue just grows.

Consumers
Consumers
4
5 per second
Failures and delivery
0%
0%
0%
10 s
3

Share of receives where the consumer crashes before it finishes. The message comes back after the visibility timeout, and a retry may succeed.

Share of messages that fail every time they are tried. Only the dead-letter queue gets rid of them.

Share of finished messages whose acknowledgement never reaches the queue: the work was done, and the message comes back anyway.

Time controls
Time: 10 s

One second of model time passes each half second while the lab runs. Step and Skip move time on by hand.

What is happening

Producers send 30 a second and the consumers can take 20. The queue grows by about 10 a second.

Waiting in the queue
1000 in flight, invisible until their timeout
Work done
200200 different messages, 0 runs repeated
Dead-letter queue
0gave up after 3 attempts
Accepted from producers
300backpressure is off, nothing is refused
The queue: one square per message, waiting first, then in flight.
Messages waiting over time
050100110 seconds

Waiting in the queue Dead-letter queue

Where every message is, after 10 s
StateMessagesWhat it means
Waiting in the queue100Visible in the queue, ready to be received.
In flight0Received and invisible until the visibility timeout, waiting for an acknowledgement.
Acknowledged200Done and deleted from the queue.
Work repeated0Times a message already processed was processed again, because its acknowledgement was lost.
Dead-letter queue0Gave up after the allowed number of attempts, kept apart to be looked at.
Refused0Not accepted because the queue was full, with backpressure on. The producer is slowed down.

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • The lab advances one second at a time. A consumer receives up to its rate each second and finishes each message in the same second; processing time, the network and the broker itself are not modelled.
  • A message is hidden for the visibility timeout after each receive. A crash or a lost acknowledgement makes it reappear, and after the allowed number of attempts it moves to the dead-letter queue. A random draw, seeded so the same settings give the same run, decides each outcome.

Simplifications

  • The work queue does not track the order of messages: retries are received first, which stands for the oldest. Order is shown in the stream, where a partition keeps its messages in the order they were appended.
  • Exactly-once processing, transactions, replicated brokers, retention, message size and a consumer that is slow rather than dead are not modelled. The numbers are floors and illustrations, not measurements of a product.

Parameter values are illustrative. They are not benchmarks of any real product.

Producers send 30 a second. Four consumers can take 5 each, which is 20 a second, so the queue grows by about 10 a second, as the status says. At the start 100 messages are waiting in Waiting in the queue: 10 s at 10 a second. Press Skip 30 s twice, to 70 s, and there are 700. The growth is constant, because nothing changes the rate in or the rate out, and the backlog is the difference accumulated over time.

Now change the Consumers. With 5, the capacity is 25 and the backlog still grows, by 5 a second. With 6, the capacity is 30, exactly what is sent: the backlog stops growing, but it does not shrink either, so the 100 messages already waiting stay at 100. The queue only drains when the consumers can take more than is sent. With 7, the capacity is 35, the backlog falls by 5 a second, and the 100 waiting messages are gone after 100 / (35 - 30), which is 20 s.

A backlog is also a delay. Messages are taken oldest first, so a message that arrives at 70 s, behind 700 others, waits for 700 / 20, which is 35 s before a consumer receives it. The lab does not draw the waiting time, but the same arithmetic as in the lesson on Little’s Law gives it: the queue length divided by the rate at which it is served.

What happens if the consumers are always slower?

Section titled “What happens if the consumers are always slower?”

The backlog grows for as long as producers outpace consumers. At 10 a second of excess, that is 36,000 more messages an hour. Every real queue has some limit, whether memory, disk or a configured maximum, and the lab does not model it. What happens when the limit is reached depends on the system, and the next lesson is about making that a choice: refuse messages, and slow the producers down.

The other lesson from the numbers is that average rates are enough to tell if a queue will grow. A burst that is shorter than the time the consumers need to catch up is harmless: the queue absorbs it, which is the use of a queue. A producer rate that is above the consumers’ capacity for long enough is not a burst, it is a capacity problem, and only more consumers or fewer messages fix it.

  • A queue between a producer and a consumer lets the producer hand work over without waiting. It moves the waiting, it does not remove it.
  • In a work queue each message goes to one consumer, so adding consumers adds capacity. In publish/subscribe each subscriber gets a copy, so adding a subscriber adds a reader.
  • The backlog grows by the producer rate minus the consumers’ combined rate: 30 minus 4 × 5, which is 10 a second, and 700 messages at 70 s.
  • With 6 consumers the backlog stops growing but does not shrink. With 7 it drains the 100 waiting messages in 20 s.
  • A backlog is a delay: 700 messages at 20 a second is 35 s for the newest message.

Next: backpressure, which keeps a queue from growing without limit.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.