Skip to content
Kafka Is Fun

SpringSpring Kafka Advanced

What is spring.kafka.streams and how do you use Kafka Streams in Spring Boot?

Reference answer

Kafka Streams support is enabled with @EnableKafkaStreams on a @Configuration class and kafka-streams on the classpath; Spring Boot then auto-configures the KafkaStreamsConfiguration bean. The application id and bootstrap servers must be set: spring.kafka.streams.application-id (defaults to spring.application.name) and the global bootstrap servers, which can be overridden for streams only. Other Streams settings go under spring.kafka.streams.properties, for example the default serdes (there are no dedicated default-key-serde properties), or you set serdes in code. Spring for Apache Kafka declares a StreamsBuilderFactoryBean (named defaultKafkaStreamsBuilder) that exposes a StreamsBuilder; you inject it in a @Bean method, define the topology with the DSL and return a KStream. StreamsBuilderFactoryBean implements SmartLifecycle and manages the internal KafkaStreams instance, starting it automatically (spring.kafka.streams.auto-startup) and calling close() when stopped. You customize it with StreamsBuilderFactoryBeanConfigurer beans, for example to set a state listener. The reference does not document a Streams health indicator; getKafkaStreams() on the factory bean gives access to the KafkaStreams instance (for example to build your own check), and a KafkaStreamsMicrometerListener registers Micrometer metrics.

Expected key concepts: spring.kafka.streams, application-id, @EnableKafkaStreams, StreamsBuilderFactoryBean, StreamsBuilder, KStream, SmartLifecycle, StreamsBuilderFactoryBeanConfigurer, auto-startup, getKafkaStreams, KafkaStreamsMicrometerListener

How did that feel?

Your own verdict, not the score. It shows up next to this question in the list, so you can come back to the shaky ones.