Java Interview Preparation
Kafka Interview Questions
Events, partitions, consumer groups, delivery guarantees, and reliable processing.
Kafka Interview Focus
Events, partitions, consumer groups, delivery guarantees, and reliable processing.
Interview questions
1. How do you integrate Kafka with Spring Boot? Beginner
Add the spring-kafka dependency and configure bootstrap servers, serializers, and deserializers in application.yml or application.properties. Use KafkaTemplate to publish records and @KafkaListener to consume them. Spring Boot auto-configures the main producer, consumer, and listener components from those settings.
spring:
kafka:
bootstrap-servers: localhost:90922. What is the role of KafkaTemplate? Beginner
KafkaTemplate is Spring Kafka’s producer helper. It obtains a producer, serializes the key and value, sends a record to a topic, and returns a future-like result that can be used to observe success or failure. It also supports sending keys, headers, partitions, and transactional records.
kafkaTemplate.send("orders-topic", order);3. How do you create Kafka producers and consumers? Beginner
A producer publishes with KafkaTemplate after producer serializers are configured. A consumer uses a method annotated with @KafkaListener; Spring creates and manages the listener container using the configured consumer factory. The consumer group controls how partitions are shared among instances.
@KafkaListener(topics = "orders-topic", groupId = "order-group")
public void consume(Order order) {
process(order);
}4. What is a Kafka consumer group? Beginner
A consumer group is a set of consumers that share the work of reading a topic. Kafka assigns each partition to only one consumer within the same group, so a record is normally processed once per group. Different groups receive their own independent view of the topic and can process the same records for different services.
@KafkaListener(topics = "orders", groupId = "payment-service")5. How do Kafka partitions affect concurrency? Intermediate
Partitions are the unit of parallelism in Kafka. Each partition is assigned to one consumer at a time within a group, so a topic with five partitions can use up to five active consumers in that group. Extra consumers remain idle. Increase partitions carefully because partition count also affects ordering, rebalancing, and operational cost.
6. How do you handle Kafka consumer failures? Intermediate
Use an error handler, controlled retries, backoff, and a dead-letter topic. Retry transient failures such as temporary database or network errors, but send permanently invalid messages to a DLT after the limit. Log the topic, partition, offset, key, exception, and correlation ID. Commit an offset only according to the chosen delivery and processing strategy.
7. How do you configure retries? Intermediate
Spring Kafka supports blocking retries with DefaultErrorHandler and non-blocking retry topics with @RetryableTopic. Configure a maximum attempt count and an exponential or fixed backoff. After retries are exhausted, publish the record to a dead-letter topic and monitor the retry path rather than retrying forever.
@RetryableTopic(attempts = "3")
@KafkaListener(topics = "orders")
public void consume(Order order) {
process(order);
}8. What is a dead-letter topic? Beginner
A dead-letter topic stores records that still fail after the configured retry attempts. It prevents one poison message from repeatedly blocking normal processing. Include failure context such as the original topic, partition, offset, exception, and timestamp so operators can inspect, correct, and safely replay the message.
orders → orders-retry → orders-dlt9. How do you ensure idempotent message processing? Advanced
Assume a Kafka record can be delivered more than once. Give each event a unique ID or business key, store processed IDs with a unique constraint, and check that store before applying the business change. When the event update and processed-event record share a database, commit them in one transaction. Idempotency is usually more practical than relying on exactly-once delivery across every external system.
10. How do you manage Kafka transactions? Advanced
Configure a transactional producer with a unique transaction ID prefix and use Spring’s Kafka transaction manager. A transaction can publish multiple records atomically, and consume-process-produce flows can include consumed offsets in the transaction. Consumers that should not read aborted records use isolation.level=read_committed. Kafka transactions do not automatically make an external database update atomic with Kafka; use an outbox or another consistency pattern for that boundary.
kafkaTemplate.executeInTransaction(template -> {
template.send("payments", payment);
template.send("notifications", notification);
return true;
});11. How do you handle message ordering? Intermediate
Kafka preserves order within one partition, not across all partitions in a topic. Send related events with the same key so Kafka routes them to the same partition. Keep processing for that partition sequential when strict order matters, and avoid retries that allow later events to overtake earlier ones without a clear design.
kafkaTemplate.send("orders", order.getCustomerId(), order);12. How do you monitor consumer lag? Intermediate
Consumer lag is the difference between the latest available partition offset and the consumer group’s committed offset. Monitor it with Kafka tools, Spring and Micrometer metrics, Prometheus, Grafana, or a Kafka UI. Investigate sustained growth by checking processing time, errors, rebalances, partition count, consumer CPU, database latency, and downstream capacity.
Consumer lag = latest partition offset - committed group offset13. What happens during a Kafka consumer rebalance? Advanced
A rebalance redistributes topic partitions among consumers in the same group when consumers join, leave, fail, or change subscriptions. Partitions may be revoked and assigned again, causing a short pause. Consumers should commit offsets safely, release partition-specific resources, and avoid long processing that exceeds poll and session timeouts. Frequent rebalances usually indicate unstable consumers, slow processing, or unsuitable timeout settings.
How to Prepare for Java Technical Interviews
Use this Java technical interview question bank to revise core concepts and practise explaining your decisions clearly. The questions cover Java fundamentals, object-oriented programming, collections, exceptions, multithreading, Spring Boot, Hibernate, databases, and other topics used in real software development interviews.
Start with the fundamentals, then move to scenario-based questions and advanced topics. Filter questions by difficulty to build confidence gradually. A strong answer should define the concept, explain why it matters, and include a practical example when appropriate.
Continue your preparation with the Java tutorials, or explore all interview preparation tracks for managerial and company-focused questions.
