5.2 Delivery, backpressure, and capacity
This part of Messaging and Distributed Queueing develops the mechanisms below. Work the numerical examples or trace the code before checking the answer.
Concepts from other sections (9)
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.
- 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
- Exactly-once: the guarantee that each record affects state exactly once. Read more: Chapter 3: Batch and Streaming Processing in Data Engineering
- At-least-once: work is never dropped but may be processed more than once. Read more: Chapter 13: Case Study: Exactly-Once Event API
- Idempotency key: an identifier stable across attempts (
<workflow id>/<activity id>) that lets an effect detect and skip or resume a duplicate. Read more: 1.2 Design review: the control plane - Transactional outbox: writing business state and the outgoing event in the same database transaction, with a separate relay publishing from the outbox table, so state and event commit atomically with no dual-write inconsistency. Read more: Chapter 7: Durable Orchestration and Workers
- Dead-letter queue: where a task goes after exhausting its budget; nothing retries it until a human redrives it. Read more: 12.4 A poison task lands in the dead-letter queue
- 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
- 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
- Consumer lag: log-end offset minus committed offset; the backlog a group still has to process. Read more: 5.3 Coordination and technology choice
Learning objectives
- Trace crash windows and apply backpressure when arrival rate exceeds service rate.
Delivery semantics and the crash window
Section titled “Delivery semantics and the crash window”Now consider what happens when a worker fails. Let the worker’s job be three steps: (1) poll a message at offset , (2) do durable work , and (3) commit offset . The delivery semantics depend entirely on the order of steps 2 and 3 and on where a crash lands.
At-most-once commits before the work: poll → commit → W. If the crash lands after the commit but before completes, the message is lost, because the offset has already advanced and the work was never durably done. You get no duplicates but possible loss, which is rarely acceptable.
At-least-once commits after the work: poll → W → commit. This is the default and the correct choice. Its crash window is the interval between finishing and the commit landing. A crash there leaves the offset un-advanced, so the message is redelivered, and you get a duplicate. You get no loss but possible duplicates.
“Exactly-once” is the most misunderstood of the three. Kafka transactions plus the idempotent producer do give exactly-once within Kafka-to-Kafka processing: consume, transform, and produce, atomically tied to the offset commit. But the instant your handler touches an external system, such as a payment gateway or an email send, that external effect is outside the transaction and you are back to at-least-once.
sequenceDiagram
participant K as Kafka
participant W as Worker
participant DB as External system
K->>W: poll(msg @ offset n)
W->>DB: durable work W
Note over W,K: CRASH HERE → offset n not committed →<br/>redelivery → at-least-once → DUPLICATE
W->>K: commit offset n
The real fix is idempotency: make applying the same message twice equal applying it once, by deduplicating on a natural key, using conditional writes, or doing a SETNX on an idempotency key. The operational mechanics (the idempotency key, the transactional outbox, dead-letter queues, retry topics) are covered in Durable Orchestration and Workers. Here you need only the conceptual frame.
Warning: Assume at-least-once delivery everywhere. The duplicate is not a bug; it is the contract. A handler that is not idempotent will eventually double-charge, double-send, or double-count.
The math: Little’s Law and backpressure as an integral
Section titled “The math: Little’s Law and backpressure as an integral”With the mechanics in place, two formulas explain almost all queueing intuition.
Little’s Law relates the three quantities you actually measure:
Here is the average number of in-flight (unacknowledged or uncommitted) messages, is the arrival rate, and is the average time a message spends in the system, its latency. The law holds for any stable system, regardless of arrival distribution or service discipline, and you can read it three ways. Fix the throughput and halve the latency , and you halve the in-flight count , so fewer messages sit parked in memory unacknowledged. Observe and , and you can infer without timing anything. And if a worker pool of consumers each holds up to in-flight messages, then , so the maximum sustainable arrival rate is .
Backpressure as an integral describes how the backlog evolves. Queue depth (consumer lag) is the running area between the arrival and service curves:
Here is the service rate, how fast the pool drains. When , lag accumulates; when , it drains. A spike is a temporary that integrates into a hump of lag and pays back down later. That is load-leveling made quantitative. A pull consumer guarantees that is set by the consumer, so lag can grow only as fast as the real shortfall, and nothing is forced on a worker beyond . code/backpressure_demo.py computes exactly this integral as its running backlog.
Summary
Section titled “Summary”Trace crash windows and apply backpressure when arrival rate exceeds service rate. State the assumptions before applying the result to a different workload.
Review questions
Section titled “Review questions”-
Can a larger queue fix sustained overload?
Answer
No. It delays failure and increases waiting time; reduce arrivals or increase sustainable service capacity.