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.
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);
}
}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?