Decoupling With Queues
Every system so far in this course has assumed a request gets an answer right away. Some work genuinely does not need an immediate answer, sending a confirmation email, generating a certificate, resizing an uploaded image, and forcing the original request to wait for it anyway just makes the user wait longer for no benefit. A message queue lets one part of the system hand off that work and move on, without the two parts ever needing to be available at the same instant.
Decoupling in Two Dimensions
A queue decouples a producer and a consumer in two separate ways. In time: the producer can place a message and move on immediately, without waiting for the consumer to be free or even online right now, the message simply waits in the queue until a consumer is ready. In capacity: if messages arrive faster than consumers can process them, they queue up rather than overwhelming the consumer directly, and you can add more consumer instances to drain the backlog faster, the same horizontal scaling idea from Module 2, applied to background work instead of web requests.
At-Least-Once Delivery
Most message queues guarantee at-least-once delivery: a message will be delivered, but if the consumer fails or crashes after receiving it but before acknowledging it finished, the queue will redeliver that same message to another consumer. This is a deliberate, sensible trade-off, the alternative (at-most-once, where a crash means the message is simply lost) is usually worse, but it has a direct, unavoidable consequence: your consumer code must expect to process the exact same message more than once, sometimes.
One Message, Many Consumers: Fan-Out
Everything above describes a point-to-point queue: one message, consumed once, by one consumer, then gone. A publish-subscribe (pub/sub) system changes the shape of that promise: a producer publishes a message to a topic, and every independently subscribed consumer group gets its own copy, unaware of the others. "A student completed a lesson" is a good example of an event with more than one legitimate audience: the certificate pipeline from this module's next lesson needs to know, so does an analytics pipeline counting completions, and so might a notifications service. A single point-to-point queue forces an awkward choice between those consumers. A pub/sub topic lets each one subscribe independently, process the same event in its own time, and fail or fall behind without affecting any of the others. The trade-off is real: a point-to-point queue's "exactly one consumer gets this" guarantee is simple to reason about, while pub/sub's fan-out means the same at-least-once delivery problem from this lesson now applies separately to every subscriber, each of which needs its own idempotency handling (the next lesson).
What Belongs on a Queue
Work that does not need to block the original request, and can tolerate being processed moments after the request that triggered it, belongs on a queue: sending a confirmation email, generating a PDF, updating a search index, running a report. Work the user is actively waiting on inline for a response, loading a page, checking a password, generally does not belong behind a queue, since that would turn an interactive wait into an indefinite one.