Fan out
- Scenarios where the Producer has to produce messages to multiple topics (usually done in parallel).
- One to many
- Example scenarios:
- SMS notification for an order - use sms topic
- email notification for an order - use email topic
- Why don’t the Producer write to one topic and let the Consumers use different
consumer groups
.
- sms consumer application belongs to one consumer group.
- email consumer application belongs to another consumer group.
- Both of these applications can subscribe to the same topic and read messages from the same topic.
- It is possible to do it that way. But this is not a good approach.
- Each application has to consume all the messages from the topic and then use a filter to see if each message is applicable to it or not. It is not a very efficient approach.
- We cannot control features like message retention period for sms and email messages in the topic if both of them use the same topic.
Fan in
- One to many
- If multiple Producers are producing messages into multiple formats, in different formats, and if our application needs to consume all those messages, then we need to use this pattern.
- Example scenario: Ride Share service
- Messages about waiting passengers will be coming in from one topic.
- Messages about available drivers will be coming in from another topic.
- Our application has to pick up the first message from the “passengers-topic” and pair it with the first message from “available-drivers” topic and zip them and send notifications to both of them.