Topic 106 / Core
Message queue fundamentals
Chapter 7.1 · Asynchronous Foundations
What and why
Asynchronous communication (→ 105) needs a durable hand-off point so the producer doesn't need the consumer to be up at the same instant. A message queue is a broker-managed buffer that stores messages until a consumer acknowledges having processed them, decoupling producer uptime and speed from consumer uptime and speed. Builds on: → 105.
Diagram
producer ──send──► [ broker: durable queue ] ──deliver──► consumer
▲ │
└──────── redeliver on no ack ◄────┘
How it works
- The producer sends a message; the broker stores it durably before acknowledging the send, so a crash right after send doesn't lose it.
- The message sits on the queue until delivered to a consumer.
- The consumer processes it, then sends an acknowledgement; the broker marks it delivered.
- If the consumer crashes or times out before acknowledging, the broker redelivers — which is exactly why consumers must be built to tolerate redelivery (→ 116).
- Multiple consumers reading the same queue compete for messages — each message is handled by one of them, scaling throughput horizontally without changing the producer.
Practical example
(illustrative). Order-confirmation emails arrive at 200/sec at peak. One consumer processing 50/sec cannot keep up; the queue absorbs the 150/sec surplus (→ 110) until four competing consumers, at 50/sec each (4 × 50 = 200/sec), clear the queue as fast as it fills.
When to use it
- Use when producer and consumer need independent uptime, speed, and scaling.
- Avoid when the caller needs immediate confirmation of completion (→ 105 stays synchronous), or a single, simple, strictly-ordered line of work is all that's needed.
- Failure scenarios: a down broker stalls the whole path if there's no fallback; an unbounded queue hides a struggling consumer until disk or latency becomes a crisis.
- Simpler alternative: a plain function call, or a database jobs table polled by a scheduled task, when the volume doesn't yet justify running a broker.
Practice
(mechanism — observe the behavior directly; 15–20 min, lab Kafka). Produce 20 messages to a topic with no consumer running and confirm they sit there; start one consumer and watch the drain rate; kill the consumer mid-processing and confirm the unacknowledged message is redelivered. Done when you've seen storage, redelivery, and drain happen, not just read about them.
Shared lab setupKey takeaway
A queue decouples producer speed from consumer speed and survives either side being briefly down. Done when you can: explain what happens to a message during a consumer crash before it acknowledges. Used next in → 107 (producers and consumers), → 111 (Kafka as a log-structured queue).
References
None — this topic makes no external claims; Kafka and RabbitMQ specifics are 111 and 112.
Source directory