KStream is the Kafka Streams abstraction for an unbounded stream of key-value records, where every record is an independent event. You build it from a topic with StreamsBuilder, transform it with operations such as filter, mapValues, flatMap, groupByKey and join, and write results back to Kafka with to.
Spring Boot does not replace Kafka Streams. It supplies a StreamsBuilder and a lifecycle-managed KafkaStreams instance through @EnableKafkaStreams, so you only declare the topology.
| Abstraction | Meaning | Typical use |
|---|---|---|
KStream | Every record is a new event | Clicks, payments, sensor readings |
KTable | Latest value per key, updates replace older ones | User profile, current stock |
GlobalKTable | Full table replicated to every instance | Small reference data for joins |
A stream can be turned into a table by aggregating, and a table into a stream with toStream(). Choosing between them depends on whether you care about each event or about the current state per key.
Add spring-kafka and kafka-streams, then configure the application id and broker:
The application-id is also the consumer group id and the prefix of internal topics, so it must stay stable and unique per application. exactly_once_v2 needs brokers that support it and is optional.
StreamsBuilder lives in org.apache.kafka.streams, not in a Spring package. With @EnableKafkaStreams, Spring provides it as a bean.
The bean method runs once at startup and only describes the topology. Records are processed afterwards by stream threads started by Spring's StreamsBuilderFactoryBean. Passing explicit Consumed and Produced serdes avoids surprises when the defaults do not match your types.
Stateless operations such as filter, map, mapValues, flatMapValues and peek look at one record at a time. Stateful operations keep data in a local state store backed by a changelog topic, so state survives restarts.
For windowed counts, use TimeWindows.ofSizeWithNoGrace(Duration.ofMinutes(5)) or ofSizeAndGrace when late events matter. Joins between streams need the same partition count and key on both sides, or a repartition step.
Kafka Streams ships TopologyTestDriver, which runs a topology without a broker. Build the Topology from your builder and pipe records through test topics:
default.deserialization.exception.handler) so one bad record does not stop the application.StreamsBuilderFactoryBean state through actuator health or listener hooks to detect ERROR or PENDING_SHUTDOWN states.application-id; partitions are distributed among them.No. A consumer polls records into your code. KStream is a declarative processing API that manages consumers, producers, state and fault tolerance for you.
No. spring-kafka with @EnableKafkaStreams is enough. Spring Cloud Stream offers a binder model on top, which is optional.
It defines the consumer group and internal topic names. Two different applications sharing an id would compete for the same partitions and state.
Yes. Provide a Serde for your type, such as JsonSerde from Spring Kafka or a schema-registry Avro serde, in Consumed, Produced and Materialized.
KStream models an unbounded stream of events in Kafka Streams. In Spring Boot you enable Kafka Streams, set the application id and serdes, then declare transformations on a StreamsBuilder. Use KTable for state, explicit serdes to avoid type errors, TopologyTestDriver for tests, and error handlers for production resilience.