Quiz
Warm-up from lesson 1-1: a user uploads a video, and your request handler transcodes it before responding, taking 40 seconds. Judged by the life-of-one-request walkthrough, what is wrong?
The producer-worker pattern
A message queue is a service that stores small job descriptions until something is ready to process them, releasing them in arrival order. Its purpose is separation: the code that discovers work no longer has to be the code that does the work, or do it at the same moment. Think of it as a waiting line for jobs, sitting between the servers that discover work and the servers that do it:
- The app server (the producer) receives the upload, saves the raw file, and pushes a small message onto the queue:
{"job": "transcode", "video": 123}. This takes a millisecond - It immediately answers the user: got it, processing
- Separate worker processes pull messages off the queue, one at a time, and do the slow work at their own pace
Real queue systems include RabbitMQ, Amazon SQS, and Kafka.
What the queue buys you:
- Fast responses: users never wait on slow work
- A shock absorber: a traffic spike piles messages into the queue instead of crashing servers, and workers catch up afterward
- Independent scaling: too much work waiting? Add workers, nothing else changes
Code exercise · python
Run this queue simulation. Each second some jobs arrive (the arrivals list) and one worker processes up to 3 jobs. Watch the queue absorb the burst in second 1 and drain afterward.
Code exercise · python
Your turn: the queue depth peaked at 7 and jobs waited seconds. Scale the workers. Change the processing capacity to 6 per second (two workers) and run the same simulation. Print the same report lines.
Quiz
Your queue's depth (jobs waiting) has been growing steadily for an hour. What does that tell you?