Kafka - Stream Bridge
Table of Contents
Problem statement
- With spring-cloud-stream, we write Producers and specify the corresponding destinations using binder properties in application.yaml file.
- Using this, messages are routed to the corresponding topics based on configuration.
- But all of this is static. Meaning, we do the configuration well in advance before deploying the application. One function/bean is mapped to one destination. It is more of a one-to-one mapping.
Content-based routing
- What if this mapping needs to be dynamic?
- example scenario
- If a customer purchases a book on our website, we need to put a message in the
digital
topic if the order is for digital book and we need to put a message in thephysical
topic if the order is for physical book.
- If a customer purchases a book on our website, we need to put a message in the
- In other words, we need to route messages to topics based on additional conditions.
- We cannot solve this problem simply by creating two beans - one for
digital
and one forphysical
.- What if there are 100 different topics and the routing has to take all of them into account?
- Sometimes, we may not know what the destination topic is in advance.
- Routing needs to be done to dynamic destinations.
StreamBridge
- To send arbitrary messages to a topic
- To route messages based on condition
- odd number - odd-topic
- even number - even-topic
- Output destination is known at run-time
Sample use-case
- Incoming topic/event: order-event - it will have customer id, product id, order type
- Outgoing topic/event:
- Delivery topics
- home-address
- Delivery topics