Blog

Spring Boot Kafka Interview Questions

Key Takeaways

  • Spring Boot simplifies Kafka topic creation with the NewTopic @Bean configuration.
  • KafkaAdmin beans are automatically registered, but their behavior can be configured.
  • The KafkaTemplate class helps send messages to Kafka topics efficiently.
  • Spring Boot provides various containers and annotations to facilitate message handling.

Creating Topics in Spring Boot

To create a new Kafka topic in Spring Boot, you define a NewTopic @Bean in your application's configuration:

@Bean
public NewTopic myTopic() {
    return TopicBuilder.name("myTopic")
        .partitions(10)
        .replicas(3)
        .build();
}

This code snippet shows an easy way to create a topic named "myTopic" with 10 partitions and 3 replicas. This approach remains relevant and leverages the TopicBuilder API for clarity and configurability.

Registration of KafkaAdmin Beans

Spring Boot automatically registers a KafkaAdmin bean, which manages topics and configurations in Kafka.

However, be aware that depending on your Spring Boot version and setup, you might need to configure properties like fatalIfBrokerNotAvailable to control startup behavior when brokers are unavailable.

Handling Broker Availability

By default, Spring Boot continues to load even if brokers are unavailable at startup. Should you want this condition to be fatal, set the fatalIfBrokerNotAvailable property to true in your application's properties:

spring.kafka.admin.fail-fast=true

This ensures the application fails fast if no Kafka broker is available, allowing for quicker troubleshooting.

Using the KafkaTemplate Class

The KafkaTemplate class makes Kafka integration straightforward by abstracting low-level producer details and enabling you to focus on sending messages efficiently:

public class KafkaProducerService {
    private final KafkaTemplate<String, String> kafkaTemplate;

    public KafkaProducerService(KafkaTemplate<String, String> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

    public void sendMessage(String topic, String message) {
        kafkaTemplate.send(topic, message);
    }
}

This simple service class demonstrates sending messages using the KafkaTemplate.

Message Listeners and Containers

Spring Boot offers powerful options for consuming messages through listeners and containers:

  • KafkaMessageListenerContainer operates on a single thread, suitable for processing messages from all partitions and topics.
  • ConcurrentMessageListenerContainer utilizes multiple KafkaMessageListenerContainer instances, allowing parallel processing across multiple threads for greater efficiency.

The annotations and interfaces offered, such as @KafkaListener, make it easy to define message listeners:

@KafkaListener(topics = "test")
public void processMessage(String content) {
    System.out.println("Message received: " + content);
}

In this example, messages from the "test" topic invoke the processMessage method.

FAQ

What is the purpose of the KafkaAdmin bean?

The KafkaAdmin bean manages topics and their configurations in a Kafka cluster. It allows you to automatically create and delete topics based on your application context settings.

Can you configure multiple listener containers in Spring Boot?

Yes, using the ConcurrentMessageListenerContainer, you can configure multiple listener containers to allow parallel processing of Kafka messages.

How do you handle exceptions in @KafkaListener methods?

You can handle exceptions with a @KafkaListener using the errorHandler attribute of the annotation or by configuring a CommonErrorHandler bean globally.

Mastering the tech interviewWhat everyone is doing wrong in tech interviews