SkillSprout
Chapter 29 · Lesson 1
Intermediate
3 min read

Message Queues and Pub/Sub

Overview

When one service calls another directly, it has to wait, and it breaks if the other is slow or down. A message broker sits in the middle and holds messages so the two sides do not need to be online at the same time. Queues and pub/sub are the two basic patterns.

Detailed Notes

What is a messaging system

A message is a small piece of data describing something that happened or work to do, such as "order 42 placed". A producer creates messages. A consumer reads and processes them. A broker is the middleman that receives messages from producers, stores them safely, and hands them to consumers. Because nobody waits for anyone, this is asynchronous communication.

The problem it solves

When a customer checks out, the system must charge the card, send an email, update stock and record analytics. If the checkout service calls all of these directly:

  • Checkout becomes slow, since it waits for each one.
  • If the email service is down, checkout fails even though payment worked.
  • A traffic spike can overwhelm a downstream service.

With a broker, checkout saves the order, publishes one message and returns "success". The other work happens in the background. The broker also acts as a buffer: a spike just makes the line longer while consumers work at their own pace. This independence is called decoupling.

How it works

Message queue (point-to-point): messages wait in a line. Each message is delivered to one consumer. If you run several consumers on the same queue, they share the work, so adding consumers increases throughput. This is perfect for jobs: resize an image, send an email.

Publish/subscribe (pub/sub): the producer publishes to a topic. Every subscriber gets its own copy of each message. Use it when several different services care about the same event: billing, email and analytics all react to "order placed", and checkout does not even know they exist. Adding a new subscriber needs no change to the producer.

A simple example

A restaurant. The waiter pins an order slip on a rail (the queue) and goes back to customers. Whichever cook is free takes the slip. A pub/sub topic is like a loudspeaker announcement "table 5 paid": the cashier, the manager and the kitchen each hear it.

System design example

Photo upload: the API stores the file and puts "resize photo 981" on a queue, replying instantly. A pool of workers pulls jobs and resizes. During a burst the queue grows and workers scale out; the user never waits. Example products: Amazon SQS (queue), Amazon SNS (pub/sub), RabbitMQ, Kafka.

Summary

  • A broker stores messages between producers and consumers, making communication asynchronous.
  • It decouples services, absorbs traffic spikes and isolates failures.
  • Queue: each message goes to one consumer; good for sharing work.
  • Pub/sub: each subscriber gets a copy; good for broadcasting events.
  • Trade-off: more moving parts and eventual results.

Architecture Diagram

Queue and pub/sub messaging
Architecture
Producercheckout
Brokerstores messages

Consumers

Billing
Email
Analytics

A broker decouples the producer from several independent consumers.

  1. 1The producer publishes a message and carries on.
  2. 2The broker durably stores it.
  3. 3Consumers fetch and process it independently.

Common Interview Questions

Finished this lesson?

0 of 91 lessons done · 0% of the course