Message queues
The conceptHow a website can say "uploaded" before your photo is processed
A message queue is a waiting line between two programs. A producer drops in messages (small notes that describe a job), and a consumer takes them out whenever it is ready. Neither one waits for the other, so each runs at its own speed.
Take a photo site. When you upload, the web server queues "make thumbnails for photo 123" and replies in about 50 ms. Workers pull jobs and spend 2 s on each. If 1,000 people upload at once, the jobs just pile up and get worked off.
What it’s forTracks how many jobs are waiting when work arrives faster or slower than it is handled.
B(t) = B₀ + (λ − μ) · t (never below 0)
- B(t)
- the backlog: messages waiting in the queue after t seconds
- B₀
- messages already waiting at the start
- λ
- arrival rate: messages added per second by producers
- μ
- service rate: messages finished per second by all consumers together
What it givesHow deep the queue gets during a rush, and how long it takes to drain afterwards.
- Rush: λ = 300/s, μ = 100/s, so the backlog grows by 200 each second
- After 10 s: B = 0 + 200 × 10 = 2,000 jobs waiting
- Then λ falls to 20/s: it drains at 80/s, so 2,000 ÷ 80 = 25 s to empty