Spring Kafka Basics Interview Questions and Answers
Master Spring Kafka fundamentals with interview questions covering Kafka architecture, topics, partitions, brokers, producers, consumers, offsets, Spring Kafka components, and production-ready messaging.
Introduction
Modern enterprise applications generate millions of events every day.
Examples include:
- Banking transactions
- Credit card payments
- Order placements
- User registrations
- Fraud detection
- Inventory updates
- Notification events
Traditional synchronous REST communication struggles under heavy load because services become tightly coupled.
Apache Kafka solves this problem by providing a distributed event streaming platform.
Spring Kafka integrates Apache Kafka with Spring Boot, making it easy to build scalable, reliable, and event-driven applications.
Spring Kafka Architecture
flowchart LR
Producer --> KafkaBroker
KafkaBroker --> Topic
Topic --> Consumer
Q1. What is Spring Kafka?
Answer
Spring Kafka is the official Spring project that simplifies integration with Apache Kafka.
It provides
- KafkaTemplate
- @KafkaListener
- Producer APIs
- Consumer APIs
- Error handling
- Retry support
- Transactions
- Kafka Streams integration
Benefits
- Easy Spring Boot integration
- Simplified configuration
- Enterprise messaging support
- Production-ready features
Q2. Why do we need Kafka?
Without Kafka
Service A
↓
REST
↓
Service B
↓
REST
↓
Service C
Problems
- Tight coupling
- Cascading failures
- Limited scalability
With Kafka
flowchart LR
Producer --> Kafka
Kafka --> ConsumerA
Kafka --> ConsumerB
Kafka --> ConsumerC
Kafka enables asynchronous communication.
Q3. What is a Topic?
A Topic is a logical category where Kafka stores messages.
Examples
- payments
- orders
- customers
- notifications
- audit-events
A producer writes to a topic.
Consumers read from a topic.
Topic
flowchart LR
Producer --> PaymentsTopic
PaymentsTopic --> Consumer
Q4. What is a Partition?
A Topic is divided into one or more partitions.
Benefits
- Parallel processing
- Horizontal scalability
- High throughput
Example
payments
↓
Partition 0
Partition 1
Partition 2
Topic Partitioning
flowchart TD
Topic --> Partition0
Topic --> Partition1
Topic --> Partition2
Each partition maintains message ordering.
Q5. What is a Broker?
A Broker is a Kafka server.
Responsibilities
- Store messages
- Serve producers
- Serve consumers
- Replicate partitions
- Maintain durability
Production clusters usually contain multiple brokers.
Q6. What is a Producer?
A Producer publishes messages to Kafka topics.
Example
kafkaTemplate.send(
"payments",
payment);
Producer Flow
flowchart LR
Application --> Producer
Producer --> Broker
Broker --> Topic
Q7. What is a Consumer?
A Consumer reads messages from Kafka topics.
Example
@KafkaListener(
topics="payments")
Consumer Flow
flowchart LR
Topic --> Consumer
Consumer --> BusinessService
BusinessService --> Database
Consumers process events independently.
Q8. What is an Offset?
Each message inside a partition has a unique offset.
Example
Partition 0
Offset 0
Offset 1
Offset 2
Offset 3
Consumers track offsets to know which messages have already been processed.
Offsets enable fault recovery and replay.
Q9. How does Spring Kafka work?
Workflow
- Producer sends event.
- Kafka stores the event.
- Consumer receives the event.
- Business logic executes.
- Offset is committed.
Message Lifecycle
sequenceDiagram
Application->>Producer: Publish Event
Producer->>Kafka: Store Event
Kafka->>Consumer: Deliver Event
Consumer->>BusinessService: Process
BusinessService-->>Consumer: Success
Consumer->>Kafka: Commit Offset
Q10. Spring Kafka Best Practices
Keep Messages Small
Avoid sending very large payloads.
Design Immutable Events
Events should represent facts that have already occurred.
Use Meaningful Topic Names
Examples
- payment-events
- customer-events
- fraud-alerts
Avoid Business Logic in Listeners
Delegate processing to service classes.
Monitor Consumer Lag
Lag directly affects processing latency.
Banking Example
flowchart TD
MobileBanking --> PaymentProducer
PaymentProducer --> Kafka
Kafka --> PaymentConsumer
PaymentConsumer --> PaymentService
PaymentService --> PostgreSQL
PaymentService --> NotificationService
Payments are processed asynchronously with high scalability.
Common Interview Questions
- What is Spring Kafka?
- Why use Kafka?
- What is a Topic?
- What is a Partition?
- What is a Broker?
- What is a Producer?
- What is a Consumer?
- What is an Offset?
- How does Spring Kafka work?
- Spring Kafka best practices?
Quick Revision
| Topic | Summary |
|---|---|
| Spring Kafka | Spring integration for Apache Kafka |
| Topic | Logical message category |
| Partition | Parallel message storage |
| Broker | Kafka server |
| Producer | Publishes events |
| Consumer | Processes events |
| Offset | Message position |
| KafkaTemplate | Producer API |
| @KafkaListener | Consumer API |
| Consumer Lag | Processing delay |
Kafka Message Lifecycle
sequenceDiagram
Producer->>Broker: Publish Message
Broker->>Topic: Store Message
Topic->>Partition: Append Record
Consumer->>Partition: Fetch Records
Partition-->>Consumer: Events
Consumer->>BusinessService: Process
BusinessService->>Database: Persist
Consumer->>Broker: Commit Offset
Production Example – Banking Payment Event Platform
A digital banking platform processes millions of payment transactions daily.
Workflow
- Customer initiates a payment from the mobile application.
- The Payment Service validates the request.
- The Producer publishes a
PaymentInitiatedevent to the payment-events topic. - Kafka stores the event across multiple partitions.
- Multiple consumer instances process events in parallel.
- The Payment Consumer updates the transaction database.
- The Notification Consumer sends SMS and email confirmations.
- The Fraud Detection Consumer analyzes the same event independently.
flowchart LR
MobileApp --> PaymentService
PaymentService --> KafkaProducer
KafkaProducer --> PaymentEventsTopic
PaymentEventsTopic --> PaymentConsumer
PaymentEventsTopic --> FraudConsumer
PaymentEventsTopic --> NotificationConsumer
PaymentConsumer --> PostgreSQL
FraudConsumer --> FraudEngine
NotificationConsumer --> EmailSMSService
Advantages
- Loose coupling between services.
- Independent scaling of consumers.
- High throughput using partitions.
- Reliable message storage and replay.
- Event reuse by multiple downstream services.
Kafka Core Components
flowchart TB
Producer --> Topic
Topic --> Partition1
Topic --> Partition2
Topic --> Partition3
Partition1 --> Broker1
Partition2 --> Broker2
Partition3 --> Broker3
Broker1 --> ConsumerGroup
Broker2 --> ConsumerGroup
Broker3 --> ConsumerGroup
This illustrates how Kafka distributes topic partitions across brokers while consumer groups process events in parallel.
Key Takeaways
- Spring Kafka provides seamless integration between Spring Boot applications and Apache Kafka.
- Topics organize messages logically, while Partitions enable horizontal scalability and parallel processing.
- Producers publish events, and Consumers process them asynchronously.
- Offsets track message consumption and enable replay, recovery, and fault tolerance.
- Kafka Brokers persist events and replicate data for high availability.
- Spring Kafka simplifies development using KafkaTemplate for producers and @KafkaListener for consumers.
- Kafka is ideal for event-driven architectures requiring high throughput, loose coupling, and scalable messaging.
- Combining Spring Boot and Kafka enables resilient enterprise systems capable of processing millions of business events reliably.