Handling multiple event types in a topic


How can you have multiple event types in a topic and maintain topic-name subject constraints?

Edit this page

Example use case:

You have distinct but related event types and you want to produce them to the same topic, but you also want to maintain topic-name subject constraints. Why produce different events to the different same topic? One reason would be you have low-traffic topics and you'd like to consolodate them to reduce overhead for the brokers. Or you need to get the exact order of different events and by producing them to the same topic you are guaranteed correct ordering per-partition.
To do multiple events with topic-name constraints you'll need to use schema references, which is a schema that contains a field representing an object which is reference to another another schema. In this tutorial you'll learn about using schema references with both Protobuf and Avro.
For more information on multiple event topics you can read Put several event types in a Kafka Topic by Martin Kleppmann and Putting Several Event Types in the Same Topic – Revisited by Robert Yokota.

Hands-on code example:

Run it