Chapter 5: Messaging and Distributed Queueing
Most services start life synchronous: a request arrives, the handler does the work, and the response goes back on the same connection. That works until the work is slow, bursty, or unreliable, and then the caller is stuck waiting on whatever is slowest downstream. Asynchronous messaging breaks that coupling. It separates receiving a request from doing it: a producer drops a message into a broker and walks away, and a pool of workers consumes at its own pace. That one indirection is behind most of the event-driven architectures you will meet in practice.
Concepts from other sections (11)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Event-driven architecture: components that communicate asynchronously through events, highly decoupled; it suits complex, real-time, independently evolving systems but not simple CRUD or strictly ordered work. Read more: Chapter 2: Software Architecture
- Worker pool: stateless processes that long-poll a durable queue, execute tasks, and report results; because all state lives in the queue or engine, workers can crash, deploy, and autoscale freely. Read more: Chapter 7: Durable Orchestration and Workers
- Exactly-once: the guarantee that each record affects state exactly once. Read more: Chapter 3: Batch and Streaming Processing in Data Engineering
- Little’s law: , requests in the system equal arrival rate times time in the system, with no distributional assumptions. Read more: 1.1 Design review: the tracer and the serving platform
- Backpressure: the signal that tells a fast producer the consumers are behind; free under pull, built from prefetch limits under push. Read more: 5.3 Coordination and technology choice
- KRaft: Kafka’s Raft-based controller quorum that stores cluster metadata in an internal log and replaced ZooKeeper. Read more: 5.3 Coordination and technology choice
- Consumer group: a set of consumers that divide a topic’s partitions among themselves; different groups each see every message. Read more: 5.3 Coordination and technology choice
- At-least-once delivery: committing after the work, so a crash between work and commit causes a redelivered duplicate but never a loss. Read more: 5.3 Coordination and technology choice
- Idempotency: the property that applying the same message twice has the same effect as applying it once. Read more: 5.3 Coordination and technology choice
- Quorum: requiring write and read acknowledgements out of replicas, with for overlapping reads and writes. Read more: Chapter 6: Distributed Data and Caching
- Fan-in / fan-out: the number of inputs feeding one output, and the number of outputs fed by one input. Read more: 13.4 Variance propagation and initialization in Mathematics
This chapter is about the dynamics of that indirection. It begins with the deepest dividing line in the space, the difference between a queue that deletes messages on acknowledgement and a log that retains and replays them, and then works outward: who controls the pace of delivery (push, pull, or long-poll), how one stream can feed both a worker pool and many independent subscribers, where the crash window sits that makes “exactly-once” so slippery, and the two formulas (Little’s Law and backpressure as an integral) that explain most queueing intuition. It closes with the question every broker cluster must answer, who owns the metadata, and the three answers the industry has given: ZooKeeper, KRaft, and Redpanda.
The running example is Streamflow, the event-driven order platform that threads through this book. Its API produces to an orders topic; worker pools and analytics jobs consume from it. The containers you scheduled in Containers, Kubernetes, and Workloads are the consumers you will reason about here.
Learning objectives
- Explain the difference between a delete-on-ack queue and a retained, replayable log, and choose between them for a given workload.
- Explain why pull-based consumption gives backpressure for free, and what push brokers must add to match it.
- Show how Kafka consumer groups provide both competing consumers and publish-subscribe at once.
- Locate the crash window behind at-least-once delivery and design handlers that tolerate duplicates through idempotency.
- Use Little’s Law and the lag integral to reason about in-flight messages, latency, and sustainable throughput.
- Compare ZooKeeper (ZAB), Kafka KRaft (Raft), and Redpanda (Raft per partition) as answers to cluster coordination.
Before you begin
Section titled “Before you begin”You should be comfortable with the replication, quorum, and consensus vocabulary from System Design in this book. Knowing how a workload is deployed and scaled on Kubernetes, from Containers, Kubernetes, and Workloads, helps when lag-based autoscaling comes up.
The chapter ships three small programs you can run while you read. code/backpressure_demo.py (plain python3, no dependencies) and its Go twin code/backpressure-demo.go (go run) simulate a push and a pull consumer side by side: watch the push model drop messages while the pull model converts the surplus into lag, with nothing lost. code/raft_election.go (go run) runs a Raft leader election; trace the terms and votes and confirm that a minority partition can never elect a leader. That is the KRaft controller quorum in miniature. If you want hands-on time with a real broker, stand up a single-broker Redpanda (no ZooKeeper, Kafka-API compatible) and a vanilla Kafka in KRaft mode, produce 1000 messages to orders, then start a second consumer group and replay from offset 0. You will have proved fan-out and retention for yourself.
In this chapter
Section titled “In this chapter”- Queues, logs, and consumers: Choose queue or log semantics from ownership and replay needs.
- Delivery, backpressure, and capacity: Trace crash windows and apply backpressure when arrival rate exceeds service rate.
- Coordination and technology choice: Separate metadata coordination from data delivery and compare mechanisms.