Back to Kafka Streams
Q25Question 25 of 27

How do you integrate Kafka Streams with Spring Boot

Answer: Spring Boot integrates Kafka Streams through the spring-kafka library, which provides auto-configuration via @EnableKafkaStreams, a managed StreamsBuilderFactoryBean for topology lifecycle, and application.yml-based configuration.

Key Concepts
  • Spring Kafka provides auto-configuration for Kafka Streams through the @EnableKafkaStreams annotation and StreamsBuilderFactoryBean.
  • application.yml configures Kafka Streams properties under spring.kafka.streams with the same property names as native Kafka.
  • StreamsBuilderFactoryBean manages the lifecycle of the KafkaStreams instance, starting and stopping it with the Spring application context.
  • You define topology beans using @Bean methods that accept StreamsBuilder as a parameter.
  • State stores can be queried through interactive queries by injecting StreamsBuilderFactoryBean and calling getKafkaStreams().
  • Spring Boot Actuator exposes Kafka Streams health indicators automatically when configured.
  • The spring-kafka dependency includes both the Kafka client and Kafka Streams libraries.

Definition

Spring Boot integrates Kafka Streams through the spring-kafka library, which provides auto-configuration via @EnableKafkaStreams, a managed StreamsBuilderFactoryBean for topology lifecycle, and application.yml-based configuration.
  • @EnableKafkaStreams is the Spring annotation that triggers auto-configuration of Kafka Streams, creating a StreamsBuilderFactoryBean and a managed KafkaStreams instance.
  • StreamsBuilderFactoryBean is the Spring-managed factory that creates the StreamsBuilder, builds the topology, and manages the KafkaStreams lifecycle (start/stop with context).
  • application.yml configures Kafka Streams properties under spring.kafka.streams, including application-id, bootstrap-servers, and default serdes.
  • @Bean with StreamsBuilder is the pattern for defining topology: a @Bean method receives the auto-configured StreamsBuilder and returns a KStream to register processing logic.
  • Interactive queries work by injecting StreamsBuilderFactoryBean, calling getKafkaStreams(), and using store() to query state stores from a REST controller.

Think of National Bank of India building a new payment processor. Without Spring Boot, Rajesh the developer writes all the boilerplate: create Properties, build Topology, create KafkaStreams, add shutdown hook, manage lifecycle. With Spring Boot, Rajesh writes only the topology logic in a @Bean method, and Spring handles everything else: configuration from YAML, lifecycle management, health checks, and clean shutdown. It is like the difference between running a standalone cheque-processing machine versus plugging it into the bank's automated infrastructure.

Understanding the Concepts

What does Spring Kafka provide for Kafka Streams?

Spring Kafka wraps the native Kafka Streams API with Spring lifecycle management. You do not create Properties objects or KafkaStreams instances manually. Instead, you write YAML configuration and @Bean methods.

  • The main components are:
  • Auto-configured StreamsBuilderFactoryBean that manages the KafkaStreams lifecycle.
  • YAML-based configuration for all Kafka Streams properties.
  • Health indicators for monitoring.
  • Integration with Spring dependency injection for services, repositories, and REST controllers.
JAVA
package com.bank.streams;

public class SpringKafkaOverview {
    public static void main(String[] args) {
        System.out.println("Spring Kafka Streams integration:");
        System.out.println();
        System.out.println("Without Spring:");
        System.out.println("  Properties props = new Properties();");
        System.out.println("  props.put(...); // 10+ lines of config");
        System.out.println("  KafkaStreams streams = new KafkaStreams(topology, props);");
        System.out.println("  streams.start();");
        System.out.println("  Runtime.getRuntime().addShutdownHook(...);");
        System.out.println();
        System.out.println("With Spring Boot:");
        System.out.println("  @EnableKafkaStreams on config class");
        System.out.println("  spring.kafka.streams.* in application.yml");
        System.out.println("  @Bean KStream<> topology(StreamsBuilder builder) { ... }");
        System.out.println("  Spring handles lifecycle, shutdown, health check");
    }
}
TEXT
Expected output:
Spring Kafka Streams integration:

Without Spring:
  Properties props = new Properties();
  props.put(...); // 10+ lines of config
  KafkaStreams streams = new KafkaStreams(topology, props);
  streams.start();
  Runtime.getRuntime().addShutdownHook(...);

With Spring Boot:
  @EnableKafkaStreams on config class
  spring.kafka.streams.* in application.yml
  @Bean KStream<> topology(StreamsBuilder builder) { ... }
  Spring handles lifecycle, shutdown, health check

How do you set up the Maven dependencies?

You need spring-boot-starter, spring-kafka, and optionally spring-boot-starter-web for REST endpoints.

XML
<!-- pom.xml dependencies for Spring Boot + Kafka Streams --syn-kw">>
syn-kw"><dependenciessyn-kw">>
    syn-kw"><dependencysyn-kw">>
        syn-kw"><groupIdsyn-kw">>org.springframework.bootsyn-kw"></groupIdsyn-kw">>
        syn-kw"><artifactIdsyn-kw">>spring-boot-startersyn-kw"></artifactIdsyn-kw">>
    syn-kw"></dependencysyn-kw">>
    syn-kw"><dependencysyn-kw">>
        syn-kw"><groupIdsyn-kw">>org.springframework.bootsyn-kw"></groupIdsyn-kw">>
        syn-kw"><artifactIdsyn-kw">>spring-boot-starter-websyn-kw"></artifactIdsyn-kw">>
    syn-kw"></dependencysyn-kw">>
    syn-kw"><dependencysyn-kw">>
        syn-kw"><groupIdsyn-kw">>org.springframework.kafkasyn-kw"></groupIdsyn-kw">>
        syn-kw"><artifactIdsyn-kw">>spring-kafkasyn-kw"></artifactIdsyn-kw">>
    syn-kw"></dependencysyn-kw">>
    syn-kw"><dependencysyn-kw">>
        syn-kw"><groupIdsyn-kw">>org.springframework.bootsyn-kw"></groupIdsyn-kw">>
        syn-kw"><artifactIdsyn-kw">>spring-boot-starter-actuatorsyn-kw"></artifactIdsyn-kw">>
    syn-kw"></dependencysyn-kw">>
    syn-kw"><dependencysyn-kw">>
        syn-kw"><groupIdsyn-kw">>org.springframework.kafkasyn-kw"></groupIdsyn-kw">>
        syn-kw"><artifactIdsyn-kw">>spring-kafka-testsyn-kw"></artifactIdsyn-kw">>
        syn-kw"><scopesyn-kw">>testsyn-kw"></scopesyn-kw">>
    syn-kw"></dependencysyn-kw">>
syn-kw"></dependenciessyn-kw">>
JAVA
package com.bank.streams;

public class MavenDependencies {
    public static void main(String[] args) {
        System.out.println("Required Maven dependencies:");
        System.out.println("  spring-boot-starter:          core Spring Boot");
        System.out.println("  spring-boot-starter-web:      REST endpoints");
        System.out.println("  spring-kafka:                 Kafka client + Streams");
        System.out.println("  spring-boot-starter-actuator: health checks");
        System.out.println("  spring-kafka-test:            test utilities");
        System.out.println();
        System.out.println("spring-kafka includes kafka-streams transitively.");
        System.out.println("No separate kafka-streams dependency needed.");
    }
}
TEXT
Expected output:
Required Maven dependencies:
  spring-boot-starter:          core Spring Boot
  spring-boot-starter-web:      REST endpoints
  spring-kafka:                 Kafka client + Streams
  spring-boot-starter-actuator: health checks
  spring-kafka-test:            test utilities

spring-kafka includes kafka-streams transitively.
No separate kafka-streams dependency needed.

How do you configure Kafka Streams in application.yml?

All Kafka Streams properties go under spring.kafka.streams in application.yml. The property names match the native Kafka Streams config keys.

YAML
# application.yml
spring:
  kafka:
    bootstrap-servers: localhost:9092
    streams:
      application-id: payment-processor
      properties:
        default.key.serde: org.apache.kafka.common.serialization.Serdes$StringSerde
        default.value.serde: org.apache.kafka.common.serialization.Serdes$StringSerde
        processing.guarantee: exactly_once_v2
        num.standby.replicas: 1
        state.dir: /var/kafka-streams/payment-processor

server:
  port: 8080

management:
  endpoints:
    web:
      exposure:
        include: health,info
  health:
    kafka:
      enabled: true
JAVA
package com.bank.streams;

public class YamlConfigExplained {
    public static void main(String[] args) {
        System.out.println("application.yml key mappings:");
        System.out.println("  spring.kafka.bootstrap-servers     -> bootstrap.servers");
        System.out.println("  spring.kafka.streams.application-id -> application.id");
        System.out.println("  spring.kafka.streams.properties.*  -> native Kafka properties");
        System.out.println();
        System.out.println("Spring Boot resolves these to a Properties object internally.");
        System.out.println("No manual Properties creation needed.");
    }
}
TEXT
Expected output:
application.yml key mappings:
  spring.kafka.bootstrap-servers     -> bootstrap.servers
  spring.kafka.streams.application-id -> application.id
  spring.kafka.streams.properties.*  -> native Kafka properties

Spring Boot resolves these to a Properties object internally.
No manual Properties creation needed.

How do you define a topology as a Spring Bean?

Create a @Configuration class with @EnableKafkaStreams. Define a @Bean method that takes a StreamsBuilder parameter, builds the topology, and returns a KStream. Spring auto-configures the StreamsBuilder and manages its lifecycle.

JAVA
package com.bank.streams;

import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.Materialized;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafkaStreams;

import java.math.BigDecimal;

@Configuration
@EnableKafkaStreams
public class PaymentTopologyConfig {

    @Bean
    public KStream<String, String> paymentTopology(StreamsBuilder builder) {
        KStream<String, String> payments = builder.stream("payments");

        // Filter high-value payments
        KStream<String, String> highValue = payments
            .filter((key, value) -> {
                String[] parts = value.split("\\|");
                BigDecimal amount = new BigDecimal(parts[2]);
                return amount.compareTo(new BigDecimal("100000")) > 0;
            });

        highValue.to("high-value-payments");

        // Maintain payment count per account
        payments.groupByKey()
            .count(Materialized.as("payment-count-store"));

        return payments;
    }
}
JAVA
package com.bank.streams;

public class TopologyBeanExplained {
    public static void main(String[] args) {
        System.out.println("Spring Kafka Streams topology as a Bean:");
        System.out.println();
        System.out.println("1. @EnableKafkaStreams on @Configuration class");
        System.out.println("2. @Bean method receives StreamsBuilder (auto-configured)");
        System.out.println("3. Build topology inside the method");
        System.out.println("4. Return KStream (Spring registers it)");
        System.out.println("5. Spring starts KafkaStreams on context refresh");
        System.out.println("6. Spring stops KafkaStreams on context close");
        System.out.println();
        System.out.println("No manual KafkaStreams.start() or shutdown hook needed.");
    }
}
TEXT
Expected output:
Spring Kafka Streams topology as a Bean:

1. @EnableKafkaStreams on @Configuration class
2. @Bean method receives StreamsBuilder (auto-configured)
3. Build topology inside the method
4. Return KStream (Spring registers it)
5. Spring starts KafkaStreams on context refresh
6. Spring stops KafkaStreams on context close

No manual KafkaStreams.start() or shutdown hook needed.

How do you query state stores from a REST endpoint?

Inject StreamsBuilderFactoryBean into a REST controller, call getKafkaStreams() to get the KafkaStreams instance, and use store() to access the state store.

JAVA
package com.bank.streams;

import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StoreQueryParameters;
import org.apache.kafka.streams.state.QueryableStoreTypes;
import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
import org.springframework.kafka.config.StreamsBuilderFactoryBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PaymentCountController {

    private final StreamsBuilderFactoryBean factoryBean;

    public PaymentCountController(StreamsBuilderFactoryBean factoryBean) {
        this.factoryBean = factoryBean;
    }

    @GetMapping("/api/payments/count/{accountId}")
    public String getPaymentCount(@PathVariable String accountId) {
        KafkaStreams streams = factoryBean.getKafkaStreams();
        ReadOnlyKeyValueStore<String, Long> store = streams.store(
            StoreQueryParameters.fromNameAndType(
                "payment-count-store",
                QueryableStoreTypes.keyValueStore()));

        Long count = store.get(accountId);
        if (count == null) {
            return "{\"account\": \"" + accountId + "\", \"count\": 0}";
        }
        return "{\"account\": \"" + accountId + "\", \"count\": " + count + "}";
    }
}
JAVA
package com.bank.streams;

public class StateStoreQueryExplained {
    public static void main(String[] args) {
        System.out.println("Querying state store from REST endpoint:");
        System.out.println();
        System.out.println("  GET /api/payments/count/SAV2001");
        System.out.println("  Response: {\"account\": \"SAV2001\", \"count\": 42}");
        System.out.println();
        System.out.println("Steps:");
        System.out.println("  1. Inject StreamsBuilderFactoryBean");
        System.out.println("  2. Call factoryBean.getKafkaStreams()");
        System.out.println("  3. Call streams.store() with store name and type");
        System.out.println("  4. Call store.get(key) to read the value");
    }
}
TEXT
Expected output:
Querying state store from REST endpoint:

  GET /api/payments/count/SAV2001
  Response: {"account": "SAV2001", "count": 42}

Steps:
  1. Inject StreamsBuilderFactoryBean
  2. Call factoryBean.getKafkaStreams()
  3. Call streams.store() with store name and type
  4. Call store.get(key) to read the value

What is a complete Spring Boot banking project?

Project structure

DIAGRAM
payment-processor-spring/
├── pom.xml
├── src/
│   └── main/
│       ├── java/
│       │   └── com/
│       │       └── bank/
│       │           └── streams/
│       │               ├── PaymentProcessorApplication.java
│       │               ├── PaymentTopologyConfig.java
│       │               └── PaymentCountController.java
│       └── resources/
│           └── application.yml
└── src/
    └── test/
        └── java/
            └── com/
                └── bank/
                    └── streams/
                        └── PaymentTopologyTest.java

pom.xml

XML
<?xml version="1.0" encoding="UTF-8"?syn-kw">>
syn-kw"><project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd"syn-kw">>
    syn-kw"><modelVersionsyn-kw">>4.0.0syn-kw"></modelVersionsyn-kw">>
    syn-kw"><parentsyn-kw">>
        syn-kw"><groupIdsyn-kw">>org.springframework.bootsyn-kw"></groupIdsyn-kw">>
        syn-kw"><artifactIdsyn-kw">>spring-boot-starter-parentsyn-kw"></artifactIdsyn-kw">>
        syn-kw"><versionsyn-kw">>3.2.4syn-kw"></versionsyn-kw">>
    syn-kw"></parentsyn-kw">>
    syn-kw"><groupIdsyn-kw">>com.banksyn-kw"></groupIdsyn-kw">>
    syn-kw"><artifactIdsyn-kw">>payment-processor-springsyn-kw"></artifactIdsyn-kw">>
    syn-kw"><versionsyn-kw">>1.0.0syn-kw"></versionsyn-kw">>
    syn-kw"><propertiessyn-kw">>
        syn-kw"><java.versionsyn-kw">>25syn-kw"></java.versionsyn-kw">>
    syn-kw"></propertiessyn-kw">>
    syn-kw"><dependenciessyn-kw">>
        syn-kw"><dependencysyn-kw">>
            syn-kw"><groupIdsyn-kw">>org.springframework.bootsyn-kw"></groupIdsyn-kw">>
            syn-kw"><artifactIdsyn-kw">>spring-boot-starter-websyn-kw"></artifactIdsyn-kw">>
        syn-kw"></dependencysyn-kw">>
        syn-kw"><dependencysyn-kw">>
            syn-kw"><groupIdsyn-kw">>org.springframework.kafkasyn-kw"></groupIdsyn-kw">>
            syn-kw"><artifactIdsyn-kw">>spring-kafkasyn-kw"></artifactIdsyn-kw">>
        syn-kw"></dependencysyn-kw">>
        syn-kw"><dependencysyn-kw">>
            syn-kw"><groupIdsyn-kw">>org.springframework.bootsyn-kw"></groupIdsyn-kw">>
            syn-kw"><artifactIdsyn-kw">>spring-boot-starter-actuatorsyn-kw"></artifactIdsyn-kw">>
        syn-kw"></dependencysyn-kw">>
        syn-kw"><dependencysyn-kw">>
            syn-kw"><groupIdsyn-kw">>org.apache.kafkasyn-kw"></groupIdsyn-kw">>
            syn-kw"><artifactIdsyn-kw">>kafka-streams-test-utilssyn-kw"></artifactIdsyn-kw">>
            syn-kw"><scopesyn-kw">>testsyn-kw"></scopesyn-kw">>
        syn-kw"></dependencysyn-kw">>
        syn-kw"><dependencysyn-kw">>
            syn-kw"><groupIdsyn-kw">>org.springframework.bootsyn-kw"></groupIdsyn-kw">>
            syn-kw"><artifactIdsyn-kw">>spring-boot-starter-testsyn-kw"></artifactIdsyn-kw">>
            syn-kw"><scopesyn-kw">>testsyn-kw"></scopesyn-kw">>
        syn-kw"></dependencysyn-kw">>
    syn-kw"></dependenciessyn-kw">>
syn-kw"></projectsyn-kw">>

PaymentProcessorApplication.java

JAVA
package com.bank.streams;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class PaymentProcessorApplication {
    public static void main(String[] args) {
        SpringApplication.run(PaymentProcessorApplication.class, args);
    }
}

PaymentTopologyTest.java

JAVA
package com.bank.streams;

import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.TopologyTestDriver;
import org.apache.kafka.streams.TestInputTopic;
import org.apache.kafka.streams.TestOutputTopic;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.util.Properties;

public class PaymentTopologyTest {
    public static void main(String[] args) {
        // Build the same topology used in Spring config
        StreamsBuilder builder = new StreamsBuilder();
        new PaymentTopologyConfig().paymentTopology(builder);

        Properties props = new Properties();
        props.put(StreamsConfig.APPLICATION_ID_CONFIG, "test");
        props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "dummy:1234");
        props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());

        try (TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
            TestInputTopic<String, String> input = driver.createInputTopic(
                "payments", new StringSerializer(), new StringSerializer());
            TestOutputTopic<String, String> output = driver.createOutputTopic(
                "high-value-payments", new StringDeserializer(), new StringDeserializer());

            input.pipeInput("SAV2001", "SAV2001|NEFT|250000|SUCCESS");
            input.pipeInput("SAV2002", "SAV2002|IMPS|5000|SUCCESS");
            input.pipeInput("CUR3001", "CUR3001|RTGS|500000|SUCCESS");

            System.out.println("High-value payments:");
            while (!output.isEmpty()) {
                System.out.println("  " + output.readValue());
            }
            System.out.println();
            System.out.println("SAV2001 (250K): passed filter");
            System.out.println("SAV2002 (5K): filtered out");
            System.out.println("CUR3001 (500K): passed filter");
        }
    }
}
TEXT
Expected output:
High-value payments:
  SAV2001|NEFT|250000|SUCCESS
  CUR3001|RTGS|500000|SUCCESS

SAV2001 (250K): passed filter
SAV2002 (5K): filtered out
CUR3001 (500K): passed filter

How to run the project

BASH
# Step 1: Start Kafka
docker-compose up -d kafka zookeeper

# Step 2: Create topics
kafka-topics.sh --create --topic payments --partitions 3 --replication-factor 1 --bootstrap-server localhost:9092
kafka-topics.sh --create --topic high-value-payments --partitions 3 --replication-factor 1 --bootstrap-server localhost:9092

# Step 3: Build and run
cd payment-processor-spring
mvn clean package
java -jar target/payment-processor-spring-1.0.0.jar

# Step 4: Produce payments
kafka-console-producer.sh --topic payments --property "parse.key=true" --property "key.separator=:" --bootstrap-server localhost:9092
# SAV2001:SAV2001|NEFT|250000|SUCCESS

# Step 5: Query state store via REST
curl http://localhost:8080/api/payments/count/SAV2001

# Step 6: Check health
curl http://localhost:8080/actuator/health

What are common mistakes with Spring Boot Kafka Streams?

The most common mistake is forgetting @EnableKafkaStreams.

Wrong: missing @EnableKafkaStreams

JAVA
package com.bank.streams;

public class WrongMissingAnnotation {
    public static void main(String[] args) {
        System.out.println("WRONG: @Configuration without @EnableKafkaStreams");
        System.out.println();
        System.out.println("  @Configuration");
        System.out.println("  public class TopologyConfig {");
        System.out.println("    @Bean");
        System.out.println("    public KStream<> topology(StreamsBuilder builder) {...}");
        System.out.println("  }");
        System.out.println();
        System.out.println("Error at startup:");
        System.out.println("  No qualifying bean of type StreamsBuilder");
        System.out.println("  Spring does not create StreamsBuilderFactoryBean");
        System.out.println("  without @EnableKafkaStreams.");
    }
}
TEXT
Expected output:
WRONG: @Configuration without @EnableKafkaStreams

  @Configuration
  public class TopologyConfig {
    @Bean
    public KStream<> topology(StreamsBuilder builder) {...}
  }

Error at startup:
  No qualifying bean of type StreamsBuilder
  Spring does not create StreamsBuilderFactoryBean
  without @EnableKafkaStreams.

Correct: add @EnableKafkaStreams

JAVA
package com.bank.streams;

public class CorrectWithAnnotation {
    public static void main(String[] args) {
        System.out.println("CORRECT: @Configuration with @EnableKafkaStreams");
        System.out.println();
        System.out.println("  @Configuration");
        System.out.println("  @EnableKafkaStreams");
        System.out.println("  public class TopologyConfig {");
        System.out.println("    @Bean");
        System.out.println("    public KStream<> topology(StreamsBuilder builder) {...}");
        System.out.println("  }");
        System.out.println();
        System.out.println("Spring creates StreamsBuilderFactoryBean automatically.");
        System.out.println("StreamsBuilder is injected into the @Bean method.");
        System.out.println("KafkaStreams starts on application context refresh.");
    }
}
TEXT
Expected output:
CORRECT: @Configuration with @EnableKafkaStreams

  @Configuration
  @EnableKafkaStreams
  public class TopologyConfig {
    @Bean
    public KStream<> topology(StreamsBuilder builder) {...}
  }

Spring creates StreamsBuilderFactoryBean automatically.
StreamsBuilder is injected into the @Bean method.
KafkaStreams starts on application context refresh.

What should a senior or principal engineer know?

At the senior level, you need to understand the differences between the native Kafka Streams API and the Spring wrapper.

  • StreamsBuilderFactoryBean is a SmartLifecycle bean. It starts the KafkaStreams instance in the STARTED phase of the Spring context. If the topology bean throws an exception during construction, the application fails to start.
  • Multiple topologies: you can have multiple @Bean methods that build different parts of the topology on the same StreamsBuilder. All beans are added to a single topology.
  • Custom configuration: override the default StreamsBuilderFactoryBean by defining your own @Bean of type StreamsBuilderFactoryBean with custom KafkaStreams.StateListener or StreamsUncaughtExceptionHandler.
  • Testing: use TopologyTestDriver for unit tests of the topology bean. For integration tests, use @EmbeddedKafka with Spring Boot Test.
  • Profile-based config: use Spring profiles (dev, staging, prod) to set different Kafka Streams properties (e.g., exactly_once_v2 only in prod).
JAVA
package com.bank.streams;

public class SeniorSpringKafkaKnowledge {
    public static void main(String[] args) {
        System.out.println("Senior/Principal Spring Kafka Streams checklist:");
        System.out.println("  1. StreamsBuilderFactoryBean manages lifecycle");
        System.out.println("  2. Multiple @Bean methods share one StreamsBuilder");
        System.out.println("  3. Override factory bean for custom state listeners");
        System.out.println("  4. Use TopologyTestDriver for unit tests");
        System.out.println("  5. Use @EmbeddedKafka for integration tests");
        System.out.println("  6. Profile-based config: exactly_once_v2 in prod only");
        System.out.println("  7. Actuator health endpoint for Kafka Streams");
        System.out.println("  8. state.dir per environment for isolation");
    }
}
TEXT
Expected output:
Senior/Principal Spring Kafka Streams checklist:
  1. StreamsBuilderFactoryBean manages lifecycle
  2. Multiple @Bean methods share one StreamsBuilder
  3. Override factory bean for custom state listeners
  4. Use TopologyTestDriver for unit tests
  5. Use @EmbeddedKafka for integration tests
  6. Profile-based config: exactly_once_v2 in prod only
  7. Actuator health endpoint for Kafka Streams
  8. state.dir per environment for isolation

What are common interview follow-up questions?

  • Can you run multiple Kafka Streams applications in one Spring Boot app? (Yes, but you need multiple StreamsBuilderFactoryBean instances with different application IDs. This is complex and rarely needed.)
  • How does Spring Boot handle Kafka Streams errors? (Set the uncaught exception handler on the factory bean. Spring does not override Kafka Streams error handling by default.)
  • Can you use Spring profiles to toggle topologies? (Yes. Use @Profile("fraud-detection") on a topology @Bean to activate it only in the fraud-detection profile.)
  • How do you test a Spring Boot Kafka Streams app end-to-end? (Use @SpringBootTest with @EmbeddedKafka. Produce test records, wait, and consume output records.)
  • What happens if the Kafka broker is down when Spring Boot starts? (StreamsBuilderFactoryBean retries the connection according to Kafka client retry settings. The health endpoint reports DOWN until the connection succeeds.)

Visual Overview

DIAGRAM
 Spring Boot Kafka Streams Architecture
 ========================================

 ┌──────────────────────────────────┐
 │ Spring Boot Application          │
 │                                  │
 │ ┌────────────────────────────┐   │
 │ │ @EnableKafkaStreams        │   │
 │ │ StreamsBuilderFactoryBean │   │
 │ │  ├── StreamsBuilder       │   │
 │ │  ├── Topology             │   │
 │ │  └── KafkaStreams         │   │
 │ │      (managed lifecycle)  │   │
 │ └────────────────────────────┘   │
 │                                  │
 │ ┌────────────────────────────┐   │
 │ │ @Bean topology()          │   │
 │ │  stream -> filter -> to   │   │
 │ │  stream -> groupBy ->     │   │
 │ │     count (state store)   │   │
 │ └────────────────────────────┘   │
 │                                  │
 │ ┌────────────────────────────┐   │
 │ │ @RestController            │   │
 │ │ GET /api/count/{account}   │   │
 │ │  -> factoryBean            │   │
 │ │  -> getKafkaStreams()      │   │
 │ │  -> store().get(key)       │   │
 │ └────────────────────────────┘   │
 │                                  │
 │ application.yml                  │
 │  spring.kafka.streams.*          │
 └──────────────────────────────────┘

Key Takeaway

Spring Boot simplifies Kafka Streams development by handling configuration, lifecycle, and health monitoring. You write topology logic in @Bean methods, configure properties in application.yml, and Spring manages the KafkaStreams instance lifecycle. No manual Properties objects, no shutdown hooks, no boilerplate.

The key annotation is @EnableKafkaStreams, which creates a StreamsBuilderFactoryBean. This factory bean creates the StreamsBuilder, builds the topology, and starts the KafkaStreams instance when the Spring context refreshes. State stores can be queried from REST controllers by injecting the factory bean.

In our payment processor project at National Bank of India, we built a Spring Boot application that filters high-value payments and maintains a count per account in a state store. A REST endpoint at /api/payments/count/{accountId} queries the store directly. The TopologyTestDriver tests the topology without Spring or Kafka running.

At the senior level, understand that multiple @Bean methods share one StreamsBuilder (one topology), override the factory bean for custom error handlers and state listeners, and use Spring profiles to control environment-specific settings like exactly_once_v2 in production.

Remember one sentence: @EnableKafkaStreams plus application.yml plus a @Bean method that takes StreamsBuilder gives you a fully managed Kafka Streams application inside Spring Boot with zero boilerplate.

---

Share
Use ← → arrow keys to navigate