Skip to content
tezvyn:

Publish/Subscribe Pattern

MediumHow cards are made

Publish/subscribe decouples senders from receivers: publishers emit messages to a topic without knowing who consumes them, and subscribers receive messages from topics they care about.

Why it exists

Direct, point-to-point communication forces a sender to know every receiver and to wait on them, creating tight coupling and brittle systems. As applications grew into many services that all needed to react to the same events, teams needed a way to broadcast events without hard-wiring producers to consumers. Publish/subscribe exists to decouple who emits an event from who reacts to it.

The mental model

Think of a magazine subscription. The publisher prints an issue and sends it to a distribution service; it has no idea who the individual readers are. Readers subscribe to the magazines they care about and receive every new issue automatically. New readers can subscribe and old ones can cancel at any time, and the publisher's job never changes.

How it works

There are three roles: publishers, subscribers, and a broker or message bus. Publishers send messages tagged with a topic to the broker. Subscribers register interest in one or more topics. The broker maintains the subscription registry and, on each publish, routes a copy of the message to every matching subscriber, often asynchronously through queues. Messaging can be topic-based, where you subscribe to named channels, or content-based, where the broker filters by message attributes. Brokers may offer delivery guarantees, durable subscriptions, and ordering, with varying trade-offs.

When it matters

It matters for event-driven and microservice architectures where one event must trigger many independent reactions, such as an order placement updating inventory, billing, and notifications. It enables fan-out, asynchronous processing, and independent scaling of consumers. It is less suited when a strict request-response with an immediate reply is required, or when guaranteed exactly-once ordered delivery is hard to satisfy.

A concrete example

An e-commerce service publishes an OrderPlaced event to an orders topic. A billing service, an inventory service, and an email service each subscribe to that topic. When an order is placed, the broker delivers the event to all three, which charge the card, decrement stock, and send a confirmation, all without the order service knowing any of them exist. A new analytics consumer can subscribe later with zero changes to the publisher.

Interview question

What is the defining benefit of the publish/subscribe pattern over direct point-to-point messaging?

  • a.Publishers and subscribers are decoupled, so neither needs to know about the otherCorrect
  • b.It requires every subscriber to acknowledge the publisher directly
  • c.It guarantees exactly-once, strictly ordered delivery by default
  • d.It is always faster because it avoids using a broker
Why?

Pub/sub decouples producers from consumers via a broker, letting each side change independently and enabling fan-out. It relies on a broker rather than avoiding one, does not guarantee exactly-once ordering by default, and subscribers do not ack the publisher directly.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles