When Does Kafka Make Sense in a Spring Boot Application?

Kafka is often introduced as a default messaging solution, but it's not always the right choice. This post explores the decision between synchronous and asynchronous communication, when Kafka's distributed log model provides value, and where simpler alternatives suffice.

Synchronous vs Asynchronous

Synchronous communication works well when immediate response is required and the caller can tolerate retries at the API level. Asynchronous communication with Kafka shines when events need to be fan-out to multiple consumers, buffered for replay, or when decoupling producer and consumer lifecycles is a priority.

Kafka Basics & Architecture

Kafka stores records in topics partitioned across brokers. Each partition is an ordered, immutable sequence of records that are continually appended. Producers send records to a topic, and consumers read from partitions, tracking their position with an offset. The architecture follows a leader-replica model for fault tolerance.

Implementation in Spring Boot

Using Spring for Apache Kafka, a producer sends an OrderCreated event to the orders topic:

@KafkaTopic("orders")
public class OrderEvents {
    @KafkaProducer
    public void produceOrderCreated(OrderCreated event) {
        kafkaTemplate.send("orders", event.getOrderId(), event);
    }
}

A consumer listens and processes the event:

@KafkaListener(topics = "orders", groupId = "order-processor")
public void consumeOrderCreated(ConsumerRecord record) {
    OrderCreated event = record.value();
    // process event
}

Failure Handling & Trade-offs

Kafka provides retry mechanisms via acks=all and idempotence at the producer level. Duplicate processing is handled by making consumer operations idempotent or by using Kafka's transactional API. However, Kafka introduces operational complexity: cluster management, Zookeeper (or KRaft), and network considerations. Kafka is unnecessary when the workload is simple, request-response patterns dominate, or the team lacks Kafka expertise.

When Kafka Is Unnecessary

  • Event volume is low (< 100 messages/second).
  • Only one consumer needs the event.
  • Latency requirements are in the millisecond range.
  • Team has no Kafka experience and short time-to-market is critical.
A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles

A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles