BolEnglishLearn · Play · Win
Login

📨 System Design · System Building Blocks

📨

Message Queue

Building BlockIntermediateModule 3

Store work to be processed asynchronously.

The user does not always need to wait for slow background work.

💡 In practice

After an order is placed, a queue handles email and reporting jobs.

🖼️ CONCEPT PICTURE
A picture to help remember Message Queue
After an order is placed, a queue handles email and reporting jobs.

💡 Like dropping a letter in a postbox: you walk away immediately, and the postal workers deliver it in their own time.

💡 Learn🛠️ Apply🎯 Check

💡 Why it matters

Making a user wait for slow jobs like emails or reports makes the app feel sluggish and can fail under load. A queue lets you accept the work quickly and process it in the background at a steady pace. It also smooths out spikes, since the queue holds extra work instead of crushing the workers.

🛠️ How to achieve it

1
Queue the slow work
When a request triggers a slow task, put a message on the queue and respond to the user right away.
2
Process in the background
Have separate worker processes pull messages and do the heavy work out of the request path.
3
Make handlers safe to retry
Design each job so running it twice does no harm, since messages can be re-delivered.
4
Plan for failures
Send messages that keep failing to a dead-letter area so they do not block the rest.

⚠️ Signs of trouble

  • •Users wait for emails or reports to finish before a page loads.
  • •Traffic spikes overwhelm the app because everything runs inline.
  • •A failed background job silently loses the work.
  • •Retried jobs cause duplicate emails or double charges.
⚠️ Common mistake: Writing handlers that break if a message is delivered twice. Make each job idempotent so a retry produces the same safe result.

🎯 Quick check

Score: 0/3

Three questions to test the idea.

  1. 1. Why put slow work on a message queue?

    Answer: So the user does not wait for background jobs to finish

    The queue lets you respond to the user quickly and process the slow work in the background.

  2. 2. Why should queue job handlers be idempotent?

    Answer: Messages can be delivered more than once, so retries must be safe

    Re-delivery is normal, so a job that runs twice must not send duplicate emails or double charges.

  3. 3. What is a good use for a queue after an order is placed?

    Answer: Handling email and reporting jobs in the background

    Email and reporting are slow and not needed instantly, so they are ideal background queue jobs.

🔑 Key terms

Queue
A holding line for work waiting to be processed.
Producer
The code that puts messages on the queue.
Consumer
The worker that takes messages off and processes them.
Idempotent
Safe to run more than once with the same result.

✅ Review checklist

📊 Your progress

0%
Mark this topic learned to track it.
Saved in this browser.
🚀 Got it?
Mark it learned and move to the next topic.
Next topic →