Publish-subscribe

1 message, many listeners Β· Part of Architecture

Key idea

Practice

7 questions Β· about 4 min

Explained step by step

Pub/sub

Pub/sub (publish-subscribe) is, like the queue, a way to connect services asynchronously. Every message published to a topic is pushed instantly to all of that topic's subscribers. Each subscriber can do something different with the message, in parallel. It's like a news channel: it posts 1 time and it reaches all 10,000 followers, and each does what they want with the news.

Pub/sub and queues look alike, but aren't the same. In a queue, each message is processed by 1 single consumer, and the sender usually knows where it goes. In pub/sub, the publisher doesn't know who uses the information and subscribers don't know where it comes from. Also, the queue batches messages until someone fetches them; the topic pushes them out instantly, with almost no waiting. It's like a letter (1 recipient you know) versus a street poster (whoever passes reads it).

Pub/sub brings more advantages. Subscribers can be added, changed or disappear and the system adjusts on its own, without the publisher keeping a list. Publishers and subscribers are decoupled, so each is developed and scaled on its own. And it simplifies communication: instead of connecting every service to every other, all connect 1 time to the topic. With 5 publishers and 5 subscribers, that's 10 connections to the topic instead of 25 direct ones.

Topic

A topic is a lightweight channel for broadcasting event notifications asynchronously. It has connection points (endpoints) so components can connect to send or receive. To broadcast, the publisher simply pushes the message to the topic. Every subscriber gets each message that was broadcast. It's like a group chat: you write 1 time in the group and all 30 members get it.

Push

Pub/sub delivers with push: the topic notifies the subscriber as soon as there's a message. This eliminates polling, which is asking every so often if there's anything new. It lowers delivery latency, key in systems where a delay can't be tolerated. It's like the difference between checking your phone every 5 minutes for a message and having the notification ring.

Fanout

Fanout is when a message reaches a topic and is copied and pushed to many destinations at once. That way each destination processes it in parallel. Destinations can be of different kinds: message queues, serverless functions or HTTP servers. It's like a hand fan: many ribs spread from 1 point. An order split into 4 copies is processed in the time of 1, not 4.

Message filtering

Pub/sub usually has other features. Filtering: each subscriber sets a rule to receive only the messages it cares about, not all of them. Durability: it keeps copies of the message on several servers and delivers at least once. Security: it authenticates whoever publishes and encrypts messages over the network. It's like subscribing to a newspaper but asking only for the sports section: 1 section of 10 arrives.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

1 message, many listeners