Apache Kafka with Spring Boot
Kafka is the most common event streaming platform in Java backends. Interviewers ask about topics, partitions, consumer groups and offsets, delivery guarantees, and how to produce and consume with Spring for Apache Kafka.
What you will learn
- Explain topics, partitions, offsets and consumer groups
- Produce and consume with KafkaTemplate and @KafkaListener
- Handle retries and dead-letter topics
- Keep ordering with message keys
Key concepts
Partition
A topic is split into partitions, each an ordered, append-only log. Ordering is guaranteed only within a partition; the message key decides the partition.
Consumer group
Consumers with the same group id share a topic's partitions: each partition is read by one consumer in the group. More consumers than partitions leaves some idle.
Offset
A consumer's position in a partition. Committing after processing gives at-least-once delivery; committing before gives at-most-once.
Producer settings
acks=all with enable.idempotence=true (default in recent clients) avoids losing or duplicating messages from producer retries.
Step by step
Producer and consumer
@Service
class OrderEvents {
private final KafkaTemplate<String, OrderPlaced> kafka;
void publish(OrderPlaced e) {
kafka.send("orders.placed", e.orderId(), e); // key = orderId keeps one order in order
}
}
@Component
class InventoryListener {
@RetryableTopic(attempts = "4", backoff = @Backoff(delay = 1000, multiplier = 2)) // retry topics + DLT
@KafkaListener(topics = "orders.placed", groupId = "inventory")
void on(OrderPlaced e) {
inventory.reserve(e); // must be idempotent
}
@DltHandler
void dead(OrderPlaced e) { alerts.notify("reservation failed for " + e.orderId()); }
}Examples
Key settings
spring:
kafka:
bootstrap-servers: localhost:9092
producer:
acks: all
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
consumer:
group-id: inventory
auto-offset-reset: earliest
enable-auto-commit: false
value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
properties: { spring.json.trusted.packages: "com.app.events" }Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Create a topic with 3 partitions, run 4 consumers in one group and explain what you see.
- Implement the transactional outbox for OrderPlaced events.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
What happens during a consumer group rebalance?
When consumers join, leave or time out, partitions are reassigned. Processing pauses briefly and uncommitted messages may be redelivered to the new owner, which is another reason consumers must be idempotent. Cooperative rebalancing reduces the pause.
How do you scale Kafka consumers?
Add consumers to the group up to the number of partitions; beyond that, add partitions (which changes key-to-partition mapping for new messages). Also increase concurrency per listener container.
Summary
Kafka orders messages per partition by key, consumer groups share partitions, and committed offsets set delivery semantics. Use KafkaTemplate and @KafkaListener with retry topics, a dead-letter handler and idempotent consumers.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus