Choosing Between Queue and Pub-Sub
What you'll learn: A simple decision framework to pick the right messaging pattern for your use case.
The Core Question
When designing asynchronous communication, you need to ask: How many independent systems need to react to this message?
This single question drives your choice between queues and pub-sub.
Decision Framework
Use a Queue when:
- Exactly one consumer should process each message
- You're distributing work that needs to be done once
- Messages represent tasks or commands ("process this payment," "resize this image")
- You want competing consumers to share the load
Think of a queue like a to-do list for a team: each task gets assigned to one person, and once completed, it's checked off.
Use Pub-Sub when:
- Multiple independent systems need to react to the same event
- You're broadcasting notifications or state changes ("order placed," "user registered")
- Subscribers have different purposes (analytics, email, inventory, etc.)
- Adding new consumers shouldn't affect existing ones
Think of pub-sub like a news broadcast: multiple people listen to the same announcement and each reacts independently—one takes notes, another calls a friend, another just nods.
The "Single Processor" Test
Ask yourself: "If two systems processed this message, would that cause problems?"
- Yes, it would cause problems → Use a queue (e.g., charging a credit card twice)
- No, independent reactions are fine → Use pub-sub (e.g., multiple teams reacting to a new user signup)
Practical Example
Payment processing? Queue—you need exactly one processor to charge the card.
User signup event? Pub-sub—email service, analytics, CRM, and welcome workflow all need to know independently.
Key Takeaway: Choose queues for single-processor semantics where work must be done exactly once; choose pub-sub when multiple independent systems need to react to the same event.