Sub topic 1
Why Use a Message Queue
Prerequisites
This chapter assumes you can read basic Python and know how HTTP request/response works. You should also know that processes and threads run independently, and that networked services can fail or respond slowly.
The problem with synchronous calls
In a synchronous call, the caller sends a request and waits for the reply. This is simple, but it ties the caller to the callee in three ways:
- Time: the caller is blocked while the callee works. A slow image resize makes the user's upload request slow.
- Availability: if the callee is down, the call fails, and so does the caller's own work.
- Load: a traffic spike on the caller goes straight to the callee, which may not be able to keep up.
A message queue breaks these ties. The caller (the producer) puts a message describing the work on a queue and moves on. A separate worker (the consumer) takes messages off the queue and handles them when it can.
The core shape
A message is a small piece of data that describes a job or an event. A queue is an ordered buffer that holds messages until a consumer takes them. A broker is the service that hosts queues, such as RabbitMQ. This chapter uses a queue inside one Python process to show the shape. Real brokers add a network between the parts.
import queue
import threading
q = queue.Queue()
def consumer():
while True:
item = q.get()
if item is None:
q.task_done()
break
print("processing", item)
q.task_done()
t = threading.Thread(target=consumer)
t.start()
for i in range(3):
q.put(f"job-{i}")
q.put(None)
q.join()
t.join()The producer loop calls q.put and does not wait for the work to finish. The consumer thread handles each item on its own schedule. The None value is a sentinel, a marker that tells the consumer to stop.
What you gain
| Benefit | How the queue provides it |
|---|---|
| Faster responses | The producer returns after enqueueing, not after the work is done. |
| Fault isolation | Messages wait in the queue while the consumer is down or restarting. |
| Load smoothing | A spike fills the queue. Consumers drain it at their own pace. |
| Independent scaling | You can add consumers without changing the producer. |
What it costs
Queues are not free. Be clear about these costs before adopting one:
- Eventual results: the producer does not get the result right away. If the user needs an answer immediately, use a synchronous call.
- Another system to run: the broker must be deployed, monitored, and kept available.
- Harder failure handling: a message can be lost, delivered twice, or arrive out of order. Later chapters cover acknowledgements, delivery guarantees, and idempotent consumers.
Common mistakes
- Treating the queue as a database. It holds work in transit. It is not a place to query data.
- Assuming one message is processed exactly once. Plan for duplicates.
- Using a queue to hide a consumer that is permanently slower than the producer. The queue then grows without limit.
Exercise
A checkout service must charge a card and show the user whether the payment succeeded. It also sends a confirmation email. Which of these two steps suits a message queue, and why?
Show answer
The confirmation email. The user does not need its result to continue, and it can be delayed or retried if the mail service is down. The card charge must be answered immediately, so it should stay synchronous.
Exercise
In the Python example, what would happen to the producer's timing if the consumer took ten seconds per item, and why?
Show answer
The producer would finish almost immediately. It only calls q.put and does not wait for processing. Only q.join() at the end waits for the consumer to finish the queued work.