Quick revision
The short answer
Kafka keeps order only inside one partition of one topic, so OrderPlaced, OrderPaid and OrderShipped must share one topic keyed by order id if a consumer needs them in order; three topics give no order between them. With a schema registry, the default TopicNameStrategy allows one value schema per topic, so we set value.subject.name.strategy to TopicRecordNameStrategy (or RecordNameStrategy) to let each event type register its own subject. On the consumer, specific.avro.reader=true plus a class-level @KafkaListener with one @KafkaHandler per event class, and a default handler that logs and skips types it does not know.
What to remember
In this answer, we saw that ordering between OrderPlaced, OrderPaid and OrderShipped exists only if they share one topic and the order id key, while separate topics fit independent events, very different volumes or retention, or different access rights. With a schema registry, TopicRecordNameStrategy or RecordNameStrategy gives each event type its own subject, or a union with schema references keeps one subject. On the consumer, specific.avro.reader=true and a class-level @KafkaListener with one @KafkaHandler per type dispatch the events, and a default handler plus ErrorHandlingDeserializer make unknown types skipped and logged instead of fatal.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Kafka keeps order only inside one partition of one topic, so OrderPlaced, OrderPaid and OrderShipped must share one topic keyed by order id if a consumer needs them in order; three topics give no order between them. With a schema registry, the default TopicNameStrategy allows one value schema per topic, so we set value.subject.name.strategy to TopicRecordNameStrategy (or RecordNameStrategy) to let each event type register its own subject. On the consumer, specific.avro.reader=true plus a class-level @KafkaListener with one @KafkaHandler per event class, and a default handler that logs and skips types it does not know.
Why this question is asked
Teams often start with one topic per event type because it feels tidy, and then discover that a consumer sees OrderPaid before OrderPlaced. The fix, one topic for the order's events, raises a practical question: how do several Avro types live on one topic with a schema registry, and how does a Spring listener dispatch them? Interviewers ask this coding question to check both the ordering reasoning and the exact settings. In this answer, we will see why topic choice decides ordering, when separate topics still make sense, how subject naming strategies work, and then write the producer settings and the Spring Kafka listener, including the handling of unknown types.
1Why Separate Topics Cannot Keep Order
Kafka gives one ordering guarantee: records in one partition of one topic are read in the order they were appended. There is no order between partitions, and none between topics. If OrderPlaced goes to order-placed and OrderPaid goes to order-paid, a consumer subscribed to both fetches from each independently, and which record it sees first depends on fetch timing and lag on each topic.
So when a consumer needs the events of one order in sequence, they must share one topic and one key. Keying by order id makes the default partitioner send all events of order 1001 to the same partition, where they stay in the order the producer sent them.
| Choose | When |
|---|---|
| One topic for the aggregate, keyed by order id | Consumers need the events of one order in order |
| One topic per event type | Events are consumed independently |
| One topic per event type | Very different volume or retention |
| One topic per event type | Different access rights needed |
2When Separate Topics Still Make Sense
Separate topics are the better choice when events are consumed independently, for example a stream of page views that nobody correlates with orders. They also help when volumes or retention differ a lot: a high-volume telemetry event should not force a busy topic on consumers who only want rare order events, and audit events may need years of retention while operational events need days. Finally, ACLs work per topic, so events that only some services may read belong on their own topic. For our order lifecycle, none of these apply, and ordering decides it: one orders topic.
3How the Schema Registry Names Subjects
A schema registry stores schemas under subjects, and each subject has its own version history and compatibility rule. The serializer decides which subject a record's schema belongs to using a subject name strategy. The default, TopicNameStrategy, uses the topic name: every value written to orders is registered under the subject orders-value. That means one schema per topic, and registering OrderPaid after OrderPlaced is treated as an evolution of the same schema, which a compatibility check will usually reject.
TopicRecordNameStrategy uses the topic plus the record's full name, for example orders-com.shop.events.OrderPaid, so each event type gets its own subject and evolves on its own. RecordNameStrategy uses only the record name, which shares one subject across every topic that carries that type. The alternative is to keep TopicNameStrategy and register a top-level union of the three types using schema references, which keeps a single subject but lists the allowed types explicitly.
4Producer Settings
On the producer, we set the strategy as a serializer property. In Spring Boot it goes under the producer properties.
Notice that the consumer needs no strategy setting to read: each record carries its schema id, and the deserializer fetches the schema by id. The strategy matters where schemas are registered, which is on the producer, or in the CI pipeline if auto.register.schemas is turned off in production.
5The Spring Kafka Listener Shape
On the consumer, specific.avro.reader=true tells the Avro deserializer to build the generated classes, OrderPlaced, OrderPaid and OrderShipped, instead of GenericRecord. Then we put @KafkaListener on the class rather than on a method. Spring routes each record to the @KafkaHandler method whose parameter type matches the payload's class, and to the method marked isDefault = true when none matches.
Because all three handlers live in one listener and one partition carries each order's events, OrderPaid for order 1001 is always handled after OrderPlaced for 1001.
6Skipping Unknown Types Instead of Failing
Producers must be able to add a new event type, say OrderRefunded, without breaking every consumer. The default handler covers types the deserializer can build but the listener has no method for. There is a catch with the specific reader, though: if the generated class for a new type is not on the consumer's classpath, the Avro deserializer may fail to deserialize at all, rather than handing us an object (check your version's behaviour).
That is why the settings above wrap the Avro deserializer in ErrorHandlingDeserializer. A deserialization failure then reaches the container's error handler as a DeserializationException instead of failing the poll forever, and the DefaultErrorHandler does not retry DeserializationException by default. We configure its recoverer to log the record, or send it to a dead-letter topic, and move on. Either way, an unknown type is skipped and visible in logs, not a stuck partition.
7Follow-up questions
Why not use a header with the event type and a JSON payload instead?
That works too, and Spring Kafka's JsonDeserializer can map a type header to a class. With Avro and a registry we get schema validation and compatibility checks, which matter when many teams depend on a topic. Either way the listener shape is the same: one class-level listener with a handler per type and a default for the rest.
What happens to ordering if one handler throws an exception?
With the default blocking error handler, the record is retried on the same partition, so later events for that order wait behind it and order is kept. If non-blocking retry topics were used, the failed record would leave the partition's order and a later OrderShipped could be processed first, so for dependent events blocking retries are the safer default.
8Conclusion
In this answer, we saw that ordering between OrderPlaced, OrderPaid and OrderShipped exists only if they share one topic and the order id key, while separate topics fit independent events, very different volumes or retention, or different access rights. With a schema registry, TopicRecordNameStrategy or RecordNameStrategy gives each event type its own subject, or a union with schema references keeps one subject. On the consumer, specific.avro.reader=true and a class-level @KafkaListener with one @KafkaHandler per type dispatch the events, and a default handler plus ErrorHandlingDeserializer make unknown types skipped and logged instead of fatal.
Open with
Since the consumer needs OrderPlaced, OrderPaid and OrderShipped in order per order, I would put them on one orders topic keyed by order id, because Kafka only orders records within one partition of one topic; three topics give no order between them.
Then, if they want more
I would use separate topics only for events consumed independently, with very different volume or retention, or with different access rights. With a schema registry, the default TopicNameStrategy means one subject, orders-value, so one schema per topic.
I would set value.subject.name.strategy to io.confluent.kafka.serializers.subject.TopicRecordNameStrategy on the producer so each event type gets its own subject, or use RecordNameStrategy, or keep one subject with a top-level union using schema references.
On the consumer I would set specific.avro.reader to true so I get the generated classes, and write a class-level @KafkaListener on the orders topic with one @KafkaHandler method per event class, plus a @KafkaHandler with isDefault true that logs and skips unknown types.
I would also wrap the Avro deserializer in ErrorHandlingDeserializer, because an unknown class can fail deserialization, and DeserializationException is not retried by default, so the record is logged or dead-lettered rather than blocking the partition. That way producers can add new event types without breaking consumers.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Decide between one topic per event type and one topic for all order events, and show the...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/ba6e9b6bb5/