AMQP · Study deck

AMQP Reliability: Testing and Pitfalls

Some messages are casual status updates, and some are smoke alarms.

Broker Bex is your guide for this deck.

fundreliabilitypatterns
Broker Bex, the module guide, in a scene from this chapter.
iotclass.org

After studying this chapter

Learning objectives

You will be able to:

  • Explain: Raising prefetch to ceil((20+5)/5) = 5 lets five messages overlap the round trip, so the consumer stays busy and approaches 200/s — a 5x gain with no change to durability or acks.
  • Explain: If the consumer's channel or connection drops, the broker requeues everything still unacked for that consumer and marks it redelivered=true so the next consumer knows it may be a repeat.
  • Explain: A handler that is normally 5 ms but becomes 100 ms during database compaction should not be given a window that lets it reserve minutes of work.
iotclass.org

Major section

Common Pitfall: Prefetch Starvation

The mistake: Using high prefetch counts (or unlimited prefetch) with multiple consumers of varying processing speeds, causing fast consumers to starve while slow consumers hoard messages they cannot process quickly.

  • With prefetch=1000, a slow consumer receives 1000 messages immediately.
  • While it processes them one by one, those messages are unavailable to faster consumers.
  • The queue looks full, but messages are stuck in slow consumer buffers.
iotclass.org

Major section

AMQP Prefetch Reliability Contracts

It sets how many unacknowledged messages a consumer may hold before the broker gives other workers a chance.

  • A large waiting set may keep the link busy while old work fills memory and delays the alarm that matters now.
  • A protocol is a shared set of rules for exchanging data.
  • AMQP means Advanced Message Queuing Protocol.

Key terms

Latency
Latency means the elapsed time from a stated start event to a stated finish event.

Why it matters

If that consumer then blocks on a database call, those deliveries are invisible to the other workers because they are already assigned and unacknowledged.

Bounded prefetch keeps unacknowledged work distributable: a stalled consumer can hold only its window, not the whole backlog.
Bounded prefetch keeps unacknowledged work distributable: a stalled consumer can hold only its window, not the whole backlog.
iotclass.org

Major section

AMQP Prefetch Reliability Contracts (continued)

A broker is the service that accepts and routes messages.

  • Latency means the elapsed time from a stated start event to a stated finish event.
  • This runway does not select one prefetch value for every workload.
  • AMQP Reliability Patterns teaches acknowledgments, persistence, dead-lettering, queue limits, and competing consumers.
  • Second, unacked messages are the recovery unit.
iotclass.org

Major section

AMQP Prefetch Reliability Contracts (continued)

The trap is treating it as "bigger is faster".

  • This page tightens one operational contract inside that reliability model: manual acknowledgments need a deliberate prefetch window, or one stalled worker can hoard unacked messages and cause large redelivery bursts.
  • Durable queues and persistent messages protect the broker side, but the consumer side has its own decisive knob: prefetch, set with basic.qos(prefetch_count=N).
  • A large prefetch therefore means a large redelivery burst on failure.
iotclass.org

Major section

AMQP Prefetch Reliability Contracts (continued)

If a handler writes to an idempotent database table, a redelivery window of 20 may be harmless.

  • Prefetch is the number of messages the broker will hand a consumer before it must acknowledge any of them.
  • Reliability tuning is finding the prefetch that keeps a consumer busy without hoarding.
  • The monitoring threshold should match the prefetch policy.
iotclass.org

Major section

AMQP Prefetch Reliability Contracts (continued)

It quietly controls three things at once — throughput, fair distribution across workers, and how much work is at risk if a consumer crashes.

  • Concrete example: a queue has 3,000 telemetry enrichment jobs and three consumers.
  • With unlimited prefetch, the first fast TCP connection can receive hundreds or thousands of deliveries before the broker notices other consumers are available.
  • It defines the largest batch that can be delayed or redelivered by one consumer failure.
iotclass.org

Major section

AMQP Prefetch Reliability Contracts (continued)

With prefetch_count=20, the stalled worker can hold only 20 at a time, leaving the broker free to keep feeding the other two consumers.

  • A good first setting is usually based on the worst acceptable redelivery burst.
  • If each message triggers a physical device action, even five unacked commands may be too many to replay at once.
  • The broker cannot infer that risk from the queue name; the application owner has to set the window deliberately.
iotclass.org

Major section

AMQP Prefetch Reliability Contracts (continued)

With prefetch=1, a consumer processes a message, sends basic.ack, and only then receives the next one.

  • During the network round trip it is idle, so throughput is capped at roughly one message per (processing time + round trip).
  • Worked example.: Handlers take 5 ms each; the broker round trip is 20 ms.
  • At prefetch 1, each consumer does about one message per 25 ms (40/s).
iotclass.org

Major section

AMQP Prefetch Reliability Contracts (continued)

Raising prefetch to ceil((20+5)/5) = 5 lets five messages overlap the round trip, so the consumer stays busy and approaches 200/s — a 5x gain with no change to durability or acks.

  • If image analysis takes 500 ms and the broker round trip is 20 ms, ceil((20+500)/500) = 2 is already enough to hide network latency.
  • A handler that is normally 5 ms but becomes 100 ms during database compaction should not be given a window that lets it reserve minutes of work.
  • The picture connects fair throughput to monitoring both ready and unacked counts, not simply increasing concurrency.
iotclass.org

Major section

AMQP Prefetch Reliability Contracts (continued)

Its share of the queue is frozen even though the queue is full and other consumers may be idle.

  • If the consumer's channel or connection drops, the broker requeues everything still unacked for that consumer and marks it redelivered=true so the next consumer knows it may be a repeat.
  • Operationally, a prefetch problem shows up as a mismatch between queue depth and worker activity.
  • The queue may appear to drain because messages were delivered, but useful throughput stalls because they are sitting unacked inside one process.
iotclass.org

Major section

AMQP Prefetch Reliability Contracts (continued)

A healthy bounded setup has ready messages falling, unacked messages spread across consumers, and acknowledgements continuing at roughly the handler rate.

  • With three consumers and prefetch 20, more than 60 unacked messages means the configuration is not what you think, or consumers are using multiple channels.
  • With unlimited prefetch, the same count tells you very little until a stall has already trapped a large batch.
  • If that consumer then blocks on a database call, those deliveries are invisible to the other workers because they are already assigned and unacknowledged.
iotclass.org

Deck summary

Key takeaways

The mistake: Using high prefetch counts (or unlimited prefetch) with multiple consumers of varying processing speeds, causing fast consumers to starve while slow consumers hoard messages they cannot process quickly.

  • It sets how many unacknowledged messages a consumer may hold before the broker gives other workers a chance.
  • A broker is the service that accepts and routes messages.
  • The trap is treating it as "bigger is faster".
  • If a handler writes to an idempotent database table, a redelivery window of 20 may be harmless.
iotclass.org

Retrieval practice

Recall check 1 of 5

Broker Bex says: answer from memory, then check your reasoning.

Q1Place each AMQP reliability mechanism where it lives so you can tell broker receipt, durable storage, application completion, and failed-message handling apart.

APublisher Confirm
BPersistent Message
CConsumer Acknowledgement
DDead-Letter Routing
Show answer

Answer: A Place each AMQP reliability mechanism where it lives so you can tell broker receipt, durable storage, application completion, and failed-message handling apart.

Q2Complete the AMQP publisher with persistent message delivery:

Aproperties = pika.BasicProperties(delivery_mode=2)
Bproperties = pika.BasicProperties(persistent=True)
Cproperties = pika.MessageProperties(durable=True)
Dproperties = pika.Properties(delivery='persistent')
Show answer

Answer: A Message persistence requires both a durable queue and delivery_mode=2 in BasicProperties.

iotclass.org

Retrieval practice

Recall check 2 of 5

Broker Bex says: answer from memory, then check your reasoning.

Q3Per this chapter's Overview section, in the 3,000-job / three-consumer example, why does setting prefetch_count=20 keep the broker able to feed the other two consumers even if one worker stalls on a database call?

ABecause the stalled worker can hold at most 20 unacknowledged deliveries at a time, leaving the rest of the queue available for the other consumers
BBecause the broker moves the stalled worker's 20 deliveries to a healthy worker once its prefetch window fills, keeping pending jobs circulating between consumers
CBecause the worker acknowledges its deliveries as soon as its window reaches 20, releasing the broker to assign another batch while the database call finishes
DBecause unlimited prefetch and prefetch_count=20 behave identically once more than 20 messages are queued
Show answer

Answer: A Bounding prefetch caps how much of the queue one consumer can hold unacknowledged at once.

iotclass.org

Retrieval practice

Recall check 3 of 5

Broker Bex says: answer from memory, then check your reasoning.

Q4Three competing consumers share a busy queue with prefetch set to unlimited. One consumer stalls on a slow external call while holding thousands of unacked messages. What is the observed effect and the fix?

AThe stalled consumer hoards unacked messages; set bounded prefetch.
BThe broker automatically reassigns the stalled consumer's unacked messages after a few seconds.
CNothing is wrong because unlimited prefetch maximizes throughput.
DSwitching to auto-ack would distribute the load more fairly.
Show answer

Answer: A Unacked messages are pinned to a consumer and counted against prefetch; unbounded prefetch causes starvation and large crash redeliveries, so use a modest prefetch with manual acks.

iotclass.org

Retrieval practice

Recall check 4 of 5

Broker Bex says: answer from memory, then check your reasoning.

Q5A warehouse management system uses AMQP with auto-acknowledgment (auto_ack=True) for order processing. During a peak sales event, a consumer crashes while processing an order worth $5,000. What happens to that order message?

AThe message is automatically redelivered to another consumer after a timeout
BThe message stays in the queue until the crashed consumer reconnects
CThe message is permanently lost; it was acked before processing
DThe message is sent to the dead-letter exchange for manual recovery
Show answer

Answer: C With auto-acknowledgment, the broker considers the message delivered and removes it from the queue as soon as it is sent to the consumer -- before processing completes.

iotclass.org

Retrieval practice

Recall check 5 of 5

Broker Bex says: answer from memory, then check your reasoning.

Q6A developer configures a durable AMQP queue but sets delivery_mode=1 (transient) on messages. After a broker restart, what happens to the messages that were in the queue?

AAll messages are lost because transient messages are stored only in memory, even in durable queues
BThe messages survive because the queue's durable declaration includes the stored message bodies in its restart recovery
COnly messages published in the last 60 seconds are lost
DThe queue definition is removed on restart together with its transient message contents
Show answer

Answer: A Durability in AMQP requires BOTH a durable queue AND persistent messages (delivery_mode=2).

iotclass.org

Print reference

Answers 1 of 2

Answer key.

  1. A · Place each AMQP reliability mechanism where it lives so you can tell broker receipt, durable storage, application completion, and failed-message handling apart.
  2. A · Message persistence requires both a durable queue and delivery_mode=2 in BasicProperties.
  3. A · Bounding prefetch caps how much of the queue one consumer can hold unacknowledged at once.
  4. A · Unacked messages are pinned to a consumer and counted against prefetch; unbounded prefetch causes starvation and large crash redeliveries, so use a modest prefetch with manual acks.
iotclass.org

Print reference

Answers 2 of 2

Answer key.

  1. C · With auto-acknowledgment, the broker considers the message delivered and removes it from the queue as soon as it is sent to the consumer -- before processing completes.
  2. A · Durability in AMQP requires BOTH a durable queue AND persistent messages (delivery_mode=2).
iotclass.org