Yeva Byzek explains how you can use Kafka Streams to perform dynamic routing of messages:
A cleaner way is to provide the service with a separate stream that contains only the relevant subset of events that the microservice cares about. To achieve this, a streaming application can branch the original event stream into different substreams using the method
KStream#branch()
. This results in new Kafka topics, so then the microservice can subscribe to one of the branched streams directly.For example, in the finance domain, consider a fraud remediation microservice that should process only the subset of events suspected of being fraudulent. As shown below, the original stream of events is branched into two new streams: one for suspicious events and one for validated events. This enables the fraud remediation microservice to process just the stream of suspicious events, without ever seeing the validated events.
Read on to learn more.