Skip to main content
InterviewHubPRO
Section Overview
Q34

Question 34 of 50

Part 3: Microservices, Distributed Systems & Observability (25 - 35)

68%
Spring Boot & Microservices (50 Qs)•Part 3: Microservices, Distributed Systems & Observability (25 - 35)

How does Apache Kafka integrate with Spring Boot (`@KafkaListener`)?

senior LevelCode ExampleSystem DesignApache KafkaMicroservices

In Simple Words: Spring for Apache Kafka provides KafkaTemplate for publishing and @KafkaListener for consuming messages:

  • Producers send structured JSON/Avro events to a topic partition.
  • Consumers belong to a ConsumerGroup. Kafka automatically balances partitions among instances in the group.
  • Dead Letter Topics (DLT) handle poisoned pills using DefaultErrorHandler.
java
@Service
public class OrderEventConsumer {

    @KafkaListener(topics = "order-events", groupId = "inventory-group")
    public void handleOrderCreated(OrderEvent event) {
        log.info("Received order event for: {}", event.orderId());
        inventoryService.reserveStock(event.items());
    }
}