Sub topic 3
Backpressure, Ordering, and Broker Choices
Assume you know basic Python, HTTP request/response, threads, and common network failures. This chapter focuses on the design choices that keep message systems stable.
Backpressure and prefetch limits
Backpressure slows producers or limits delivery when consumers cannot keep up. A broker's prefetch limit sets how many unacknowledged messages a consumer may receive. A small limit prevents one slow consumer from holding too much work.
channel.basic_qos(prefetch_count=10)With competing consumers, the broker distributes work among consumers. Scaling consumers increases throughput until another resource, such as a database, becomes the bottleneck.
Ordering and durability
A queue may preserve order only within one queue, partition, or consumer path. Multiple consumers, retries, and redelivery can change the order observed by a handler. Preserve a key's order by routing that key consistently to one partition or serial consumer.
Durability means messages survive specified failures. Use a durable queue and persistent messages, but remember that durability does not guarantee delivery. Acknowledgements, replication, and broker configuration also matter.
Routing and log-based brokers
A work queue sends each message to one consumer. Publish/subscribe sends an event to multiple subscribers. Exchanges, topics, or routing keys select the subscribers.
A log-based broker stores an ordered, append-only log. Consumers track offsets and can replay records. Partitions provide parallelism, but ordering normally applies only inside one partition. Choose a partition key that balances load without breaking the order your application needs.
Transactional outbox
A transactional outbox solves the problem of updating a database and publishing an event together. The application writes both the business change and an outbox row in one database transaction. A relay later publishes the row and records its progress. Consumers still need idempotency because publication or acknowledgement can be repeated.
Monitoring and broker choice
Monitor queue depth, oldest message age, processing time, acknowledgement failures, retry counts, dead-letter volume, consumer count, and partition lag. Queue depth alone can hide a growing delay.
| Need | Useful choice |
|---|---|
| Task distribution and acknowledgements | Work-queue broker |
| Fan-out events | Topic or exchange-based broker |
| Replay and high throughput | Log-based broker |
Choose based on ordering scope, durability and replication, throughput, replay needs, operational skill, and failure behavior.
Exercise
Why can increasing prefetch make a slow consumer harm other consumers?
Show answer
It can reserve many unacknowledged messages for the slow consumer, reducing fair distribution and increasing queue delay.