Message Queues and Asynchronous Communication

Intermediate · 13 min

Why decoupling producers from consumers matters, and how message queues implement point-to-point and publish-subscribe messaging.

Before thisCaching Layers and CDNs

Hussein Nasser · 13 min

Message Queues and Asynchronous Communication

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Hussein NasserReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Synchronous communication forces the caller to wait for the callee to be available and to respond before moving on; asynchronous communication breaks that dependency by putting a buffer between them.
  • A message queue is that buffer — producers write messages into it without needing a consumer to be listening at that exact moment, and consumers read messages whenever they're ready to process them.
  • Decoupling producers from consumers means either side can be down, slow, or scaled independently without the other side failing or blocking.
  • Point-to-point queues deliver each message to exactly one consumer, which is the right model for distributing units of work across a pool of workers.
  • Publish-subscribe delivers each message to every subscriber, which is the right model for broadcasting an event to multiple independent systems that each need to react to it.

The problem with talking directly

When one service calls another directly — an HTTP request, a direct function call over the network — it’s a synchronous interaction: the caller sends the request and then waits, doing nothing else, until a response comes back. This is the simplest way to build communication between two systems, and it works fine as long as both sides are reliably available and reasonably fast.

The trouble starts when that stops being true. If the receiving service is temporarily overloaded, every caller waiting on it is now also stalled. If it’s down entirely, every caller either fails outright or has to implement its own retry logic. And if the receiving service processes requests more slowly than the sender produces them, a sudden burst of traffic has nowhere to go but straight into the receiver, which is exactly the situation that tips an overloaded service into an outage.

Asynchronous communication breaks this coupling. Instead of calling the receiver directly and waiting, the sender hands its message to an intermediary and moves on immediately. The receiver picks the message up and processes it whenever it’s ready. Neither side needs the other to be available, responsive, or even running, at the same moment — and that intermediary is a message queue.

How a message queue works

A message queue is, structurally, simple: producers write messages into it, and consumers read messages out of it. The queue itself persists messages (usually to disk, so a crash doesn’t lose anything already queued) until a consumer is ready to take them.

This buys two things directly:

  • Decoupling. A producer doesn’t need to know how many consumers exist, where they’re running, or whether they’re currently healthy. It just writes to the queue. A consumer doesn’t need to know who produced a message or when. This separation means either side can be deployed, scaled, or restarted independently of the other.
  • Load smoothing. If producers suddenly generate messages faster than consumers can process them, the excess simply waits in the queue rather than failing or timing out. Consumers keep working at whatever steady rate they can sustain, and the queue’s backlog drains as capacity allows. This is the core idea behind Little’s Law from queueing theory: the number of items waiting in a queue is a direct consequence of the arrival rate and the time each item spends there, which is exactly the relationship a message queue is designed to absorb gracefully instead of letting it turn into dropped requests.

Two delivery models

Not all queues behave the same way once a message is read. There are two dominant models, and picking the right one depends entirely on what you’re trying to accomplish:

Point-to-point queues deliver each message to exactly one consumer, even if several consumers are listening. This is the natural fit for distributing work: if you have a pool of workers resizing uploaded images, you want each image processed once, by whichever worker happens to be free — not by every worker redundantly. RabbitMQ is a well-known example of a message broker built primarily around this point-to-point, work-distribution model, using queues that a pool of competing consumers pulls from.

Publish-subscribe delivers each message to every subscriber that’s listening, not just one. This is the natural fit for broadcasting an event: when a new order is placed, the inventory service, the billing service, and the notification service might all need to know about it independently, and each should get its own copy of that event rather than racing to claim it. Kafka is a widely-used example built around this model, organizing messages into topics that any number of independent consumers can subscribe to and read at their own pace, often retaining the full history of messages rather than deleting them once read.

Choosing between them

The question to ask isn’t “which technology is better” — it’s “does this message represent one job to be done once, or one event that multiple independent parties need to know about?” A job queue for background work (send this email, resize this image, charge this card) wants point-to-point delivery, so the job happens exactly once. An event describing something that already happened (a user signed up, an order shipped) wants publish-subscribe, so every system that cares can react independently, without the event producer needing to know in advance who’s listening or how many of them there are.

Check yourself

  1. 01What specific problem does putting a message queue between two services solve that a direct API call doesn't?

    A direct API call requires both services to be up and responsive at the same instant, so if the receiving service is slow or down the caller is blocked or fails; a queue lets the sender hand off the message and move on, and the receiver processes it whenever it's able to, so neither side's availability or speed is coupled to the other's.

  2. 02When would you reach for a point-to-point queue instead of publish-subscribe?

    When you want each unit of work handled exactly once by exactly one consumer, such as distributing image-processing jobs across a pool of workers — point-to-point ensures no two workers pick up the same job, while publish-subscribe would deliver the same job to every subscriber, which is the wrong behavior for distributing work rather than broadcasting an event.

  3. 03How does a message queue help a system handle a sudden burst of traffic?

    Incoming messages pile up in the queue instead of overwhelming the consumers directly, so consumers can keep processing at a steady, sustainable rate while the queue absorbs the spike — the backlog drains over time instead of causing timeouts or dropped requests during the burst itself.

Go deeper

The maths underneath

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark