Message Queues
Async broker that decouples producers from consumers in time, scale, and failure.
Your checkout calls the email service to send a receipt, and waits for it to finish before telling the customer the order went through.
One afternoon the email provider slows down. Not fails, slows: 30 seconds a response. Your checkout threads pile up waiting, the pool runs out, and now nobody can buy anything at all because a receipt is slow.
Nothing was wrong with your checkout code. It was tied to the slowest thing it touched.
A queue puts a buffer between the two. Checkout drops a message and moves on in a few milliseconds, and the email service picks it up whenever it can. That one move is why a queue sits in the middle of almost every system that has grown past a single process.
Lessons
4 in this chapter- Why Decouple With a QueueThe producer's job ends at enqueue, so slow or dead consumers stop being the producer's problem.2 min
- Delivery GuaranteesAt-most-once loses messages, at-least-once duplicates them, and exactly-once is mostly your code's job.3 min
- Retries and Dead Letter QueuesRetry transient failures with backoff, and give permanent failures somewhere to go that is not the front of the queue.3 min
- Kafka vs RabbitMQ Style BrokersA log you read at your own pace versus a broker that hands out jobs and deletes them.3 min