Scrivio Interview library
CodingFoundations6 min read · updated 13 November 2026

Spring Boot can build and start a Kafka Streams topology for you; this exercise writes a small stateful topology end to end and looks at what it creates in Kafka.

Using Spring Boot with @EnableKafkaStreams, write the topology that reads the topic orders (key orderId, value Order with customerId and status), keeps only orders with status PAID, counts paid orders per customer and writes the running count to paid-orders-per-customer. Which Spring Boot properties are required, and which extra topics does the topology create?

With @EnableKafkaStreams, we declare a @Bean that takes the auto-configured StreamsBuilder and chains stream("orders"), filter for PAID, selectKey to the customer id, groupByKey with a named Grouped, count with a named Materialized store, and toStream().to("paid-orders-per-customer"). Spring Boot needs spring.kafka.streams.application-id and the bootstrap servers. Because the key changes, Streams creates a repartition topic, and the count creates a compacted changelog topic, both prefixed with the application id.

Why this question is asked

Spring Boot makes Kafka Streams easy to start, and that is exactly why people are surprised by what it creates in the cluster. A five-line topology can create two extra topics, and their names depend on choices we make in the code. Interviewers ask this exercise to see whether we can write a correct stateful topology and also explain what it does to Kafka. In this answer, we will set up the configuration, write the topology step by step, look at the internal topics it creates and why, see why naming matters, and understand what the output topic contains.

1The Spring Boot Setup

@EnableKafkaStreams tells Spring Kafka to create a StreamsBuilderFactoryBean, which builds a StreamsBuilder, collects the topology our beans define, and starts KafkaStreams when the application context starts. Spring Boot's auto-configuration fills in the settings from properties.

YAML
spring:
kafka:
streams:
application-id: paid-orders-app # required: group id and prefix of internal topics
bootstrap-servers: localhost:9092 # or spring.kafka.bootstrap-servers
properties:
processing.guarantee: exactly_once_v2 # any other StreamsConfig setting goes here
default.key.serde: org.apache.kafka.common.serialization.Serdes$StringSerde

The application id is the one setting we must think about. It becomes the consumer group id, the prefix of every internal topic and the name of the local state directory, so changing it later starts the application from scratch. If it is missing, Spring Boot falls back to spring.application.name, and fails at startup if that is not set either.

2Writing the Topology

Here is the whole topology. The Order serde is a JSON serde for our record type; in Spring Kafka 3.x that is JsonSerde, and Spring Kafka 4.0 moves to Jackson 3 based classes, so check your version for the exact name.

Java
@Configuration
@EnableKafkaStreams
class PaidOrdersTopology {
@Bean
KStream<String, Order> paidOrders(StreamsBuilder builder) {
Serde<Order> orderSerde = new JsonSerde<>(Order.class);
KStream<String, Order> orders = builder.stream("orders", Consumed.with(Serdes.String(), orderSerde));
orders.filter((orderId, o) -> o.status() == Status.PAID) // drop unpaid orders
.selectKey((orderId, o) -> o.customerId()) // key changes: orderId -> customerId
.groupByKey(Grouped.with("paid-by-customer", Serdes.String(), orderSerde))
.count(Materialized.<String, Long, KeyValueStore<Bytes, byte[]>>as("paid-count-store"))
.toStream()
.to("paid-orders-per-customer", Produced.with(Serdes.String(), Serdes.Long()));
return orders;
}
}
// input: o-1 {customerId c-7, PAID}, o-2 {c-7, NEW}, o-3 {c-7, PAID}
// output: c-7 -> 1, then c-7 -> 2

Notice the order of the steps. We filter first, so unpaid orders never travel through the repartition topic. selectKey changes the key from order id to customer id, because counting per customer requires all of one customer's orders to be in the same place. count() returns a KTable of customer id to count, and toStream() turns its updates back into records we can write to a topic.

3The Topics the Topology Creates

A count needs every order of customer c-7 in one task, but the input is partitioned by order id, so c-7's orders are spread over many partitions. Because selectKey changed the key, the next groupByKey forces a repartition: Streams writes the records to an internal topic keyed by customer id and reads them back, so equal customer ids land on the same partition. With our names, that topic is paid-orders-app-paid-by-customer-repartition.

The count keeps its state in a local store, RocksDB by default, and every update is also written to a compacted changelog topic so the state can be rebuilt on another instance: paid-orders-app-paid-count-store-changelog. Streams creates both topics itself, so the application's credentials need permission to create topics with that prefix.

Step throughWhere each record goes
  1. orders (key orderId)Source topic
  2. filter status == PAIDUnpaid orders stop here
  3. selectKey to customerIdMarks the stream for repartitioning
  4. paid-orders-app-paid-by-customer-repartitionInternal topic, now keyed by customer
  5. count into paid-count-storeRocksDB plus ...-paid-count-store-changelog
  6. paid-orders-per-customerOutput: latest count per customer

6 steps in order. Play them, or step through them one at a time.

4Why We Name Grouped and Materialized

If we leave out the names, Streams generates them from an operator counter, giving topics such as paid-orders-app-KSTREAM-AGGREGATE-STATE-STORE-0000000004-changelog. Adding a step earlier in the topology later, even a harmless filter, shifts the counter, renames the store and its changelog, and the application silently starts counting from zero. With Grouped.with("paid-by-customer", ...) and Materialized.as("paid-count-store"), the names stay fixed however the topology evolves, and the store name is also what we use for interactive queries.

5What the Output Topic Contains

The output is a stream of updates, not a stream of orders. Every paid order produces a new count for its customer, so the topic receives c-7 -> 1 and later c-7 -> 2. Consumers should treat it as the latest value per key, and the topic should ideally be compacted so it keeps the current count for every customer. With the default record cache, several updates to the same key within a commit interval may be merged into one output record, which is fine for a latest-value topic.

6Follow-up questions

How would you test this topology without a broker, using TopologyTestDriver?

We build the topology from a plain StreamsBuilder, create a TopologyTestDriver with test properties, then createInputTopic("orders", ...) and createOutputTopic("paid-orders-per-customer", ...) with the serdes. We pipe o-1 PAID, o-2 NEW and o-3 PAID for c-7 and assert that readKeyValuesToList() returns c-7 -> 1 and c-7 -> 2. The driver runs the repartition in memory and flushes after each input, so every update is visible, which is ideal for testing the logic but, as we noted, differs from production caching. We can also read the store directly with getKeyValueStore("paid-count-store").

Why return the KStream from the @Bean method?

The bean method's job is to add steps to the shared StreamsBuilder; returning the stream is a convention that makes it a bean, but the topology would be built even if we returned something else. What matters is that the method runs before the factory bean starts KafkaStreams.

Could we use groupBy instead of selectKey and groupByKey?

Yes, groupBy((id, o) -> o.customerId(), Grouped.with(...)) does the same in one step and also creates a repartition topic.

7Conclusion

In this answer, we wrote a Spring Boot Kafka Streams topology that filters paid orders, re-keys them by customer, counts them in a named store and writes the running count to an output topic. Spring Boot requires the application id and bootstrap servers, with other settings under spring.kafka.streams.properties. The key change creates a repartition topic, the count creates a compacted changelog, both prefixed with the application id, naming them keeps them stable, and the output is a latest-value-per-key stream of updates.

What to say in the interviewAbout 70 seconds aloud

Open with

With @EnableKafkaStreams, I write a @Bean method that receives the auto-configured StreamsBuilder. It does builder.stream on orders with a String serde and a JSON serde for Order, filters status PAID, selectKey to the customer id, groupByKey with Grouped.with a name like paid-by-customer and the serdes, count with Materialized.as paid-count-store, then toStream and to paid-orders-per-customer with String and Long serdes.

Then, if they want more

In Spring Boot the required properties are spring.kafka.streams.application-id, which is also the consumer group id and the prefix of every internal topic, and the bootstrap servers; anything else from StreamsConfig goes under spring.kafka.streams.properties. Because selectKey changes the key, the groupByKey forces a repartition topic, paid-orders-app-paid-by-customer-repartition, so all of one customer's orders meet in one task.

The count keeps RocksDB state backed by a compacted changelog, paid-orders-app-paid-count-store-changelog. I name both explicitly because generated names contain operator numbers that shift when the topology changes, which would reset the state.

The output is a stream of updates, the new count for that customer on every paid order, so consumers should read it as latest value per key, ideally on a compacted topic.

About this answer

Last updated
13 November 2026
Versions it names
Kafka 4.0
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 "Using Spring Boot with @EnableKafkaStreams, write the topology that reads the topic...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-streams-connect/4fc7752a66/