Skip to main content
Communicationchapter 3 of 4 · 3 lessons

Pub/Sub Pattern

One-to-many broadcast. Publishers emit events, any number of subscribers receive them.

An order gets placed. Your checkout code calls the notification service, then the loyalty service, then analytics, then the fraud check.

A new team needs to know about orders too, so somebody edits checkout and adds a fifth call. Then a sixth. Checkout is now the file every team has to touch, and it is on the critical path of a purchase.

Publish it instead. Checkout says an order was placed, once, and stops. Every team subscribes on their own, without checkout knowing they exist.

Six months later there are seven subscribers and checkout has not changed a line. That decoupling is the actual product, and the broadcasting is only how you get it.

Lessons

3 in this chapter
  1. Queues vs TopicsA queue delivers each message to one worker; a topic delivers a copy to every subscriber.2 min
  2. Fan-Out MechanicsOne event in, N durable deliveries out, with each subscriber's failures kept private.3 min
  3. Ordering and Delivery CaveatsGlobal order does not exist, duplicates do, and your subscribers have to be built for both.2 min