Publish/Subscribe
Let’s name the parts of the design.
Publisher and subscriber
A service that sends messages to a topic is a publisher, and sending a message to a topic is called publishing it. Here, the order worker publishes to order-placed. In the previous chapter, we called the order worker a producer for the email queue.
A service that subscribes to a topic is a subscriber. Here, the notification service and the shipping service are subscribers to order-placed. In the previous chapter, we called the notification service a consumer.
graph TD
W[Publisher: order worker] -->|order 93| T
subgraph B[Broker]
T[order-placed topic] -->|copy| EQ[Email queue]
T -->|copy| SQ[Shipping queue]
end
EQ --> N[Subscriber: notification service]
SQ --> Sh[Subscriber: shipping service]
Publish/subscribe
Publish/subscribe, or pub/sub for short, is a broadcasting pattern. A publisher sends a message to a topic, and every subscriber to that topic receives a copy of the message. Underneath, it still uses a broker and message queues. Each subscriber takes its copies from its own queue. A consumer takes its messages from a queue in the same way.
Point-to-point and pub/sub
Here are the two side by side:
| Point-to-point | Pub/sub | |
|---|---|---|
| Sender | Producer | Publisher |
| Sends to | A queue | A topic |
| Receiver | Consumer | Subscriber |
| Each message goes to | One consumer | Every subscriber, a copy each |
| The sender knows | Only the queue | Only the topic |
The two differ in how many receivers get each message. Point-to-point is one-to-one delivery. Pub/sub is one-to-many delivery, also called fan-out. RabbitMQ’s fanout exchange gets its name from this term.
For the order worker, the difference is that it sends one message to one topic instead of one message to each service’s queue.
Message queues and pub/sub
Many people talk about “message queues” and “pub/sub” as if they were two different things. By “message queue”, they mean point-to-point. But in both, the messages wait in queues. The difference is where the sender sends a message:
- Point-to-point: straight to a queue. One consumer takes the message.
- Pub/sub: to a topic. The broker copies the message to every queue on the topic’s list.
RabbitMQ and AWS offer the two in different ways. RabbitMQ has queues and topics in one broker. AWS splits them into two services. Amazon SQS has only queues. Amazon SNS (Simple Notification Service) has the topics: a sender publishes to an SNS topic, and SNS copies the message to the SQS queues on the topic’s list.