You need to integrate Kafka to handle real-time notifications in a social media application built with Spring Boot. How would you set up and configure this integration?

Integrating Kafka for real-time notifications in a Spring Boot social media application involves adding the Spring Kafka dependency, configuring broker and topic settings, and building a producer and consumer to publish and process notification events.

Key Points

Example

When a user comments on a post, the app publishes a CommentNotification event to a notifications topic; a consumer service picks it up, looks up the post owner's device token, and sends a push notification, all decoupled from the original comment request so the commenter doesn't wait on notification delivery.

@Service
public class NotificationProducer {
    private final KafkaTemplate<String, NotificationEvent> kafkaTemplate;

    public void publish(NotificationEvent event) {
        kafkaTemplate.send("notifications", event.getUserId(), event);
    }
}

@Component
public class NotificationConsumer {
    @KafkaListener(topics = "notifications", groupId = "notification-service")
    public void consume(NotificationEvent event) {
        pushService.send(event);
    }
}

Interview Tip

A concise interview answer is

I'd add the Spring Kafka dependency, configure the broker and topic in application.yml, then build a producer that publishes notification events with KafkaTemplate whenever a triggering action happens, and a @KafkaListener consumer that delivers the notification. Partitioning by user ID keeps per-user ordering, and a dead-letter topic handles malformed events without blocking the stream.

Last updated: 16 July 2026

Previous question: How would you handle the scenario where Kafka messages need to be consumed by multiple different services, each requiring different handling logic?

Next question: Which service discovery tools have you used in Spring Boot microservices, and what configuration do they need in the application or Kubernetes cluster?

More Microservices - Level II interview questions

All Microservices - Level II questions