Spring Kafka Consumers Interview Questions and Answers
Master Spring Kafka Consumers with interview questions covering @KafkaListener, listener containers, polling, offsets, acknowledgements, manual commits, concurrency, rebalancing, and production best practices.
Introduction
A Producer publishes events to Kafka.
A Consumer receives those events and performs business processing.
Examples
- Process banking payments
- Update customer balances
- Send notifications
- Detect fraud
- Generate audit logs
- Update inventory
Spring Kafka simplifies consumer development through the @KafkaListener annotation and listener containers.
Consumers are responsible for
- Reading records
- Processing business logic
- Managing offsets
- Handling failures
- Committing processed messages
Kafka Consumer Architecture
flowchart LR
KafkaTopic --> ConsumerGroup
ConsumerGroup --> KafkaListener
KafkaListener --> BusinessService
BusinessService --> Database
Q1. What is a Kafka Consumer?
Answer
A Kafka Consumer is a client application that reads messages from Kafka topics.
Responsibilities
- Poll records
- Process events
- Commit offsets
- Recover from failures
- Participate in consumer groups
Consumers allow applications to process events asynchronously.
Q2. What is @KafkaListener?
@KafkaListener is Spring Kafka's annotation for consuming Kafka messages.
Example
@KafkaListener(
topics = "payment-events"
)
public void consume(
PaymentEvent event
){
}
Spring automatically creates the consumer and listener container.
Benefits
- Minimal configuration
- Automatic polling
- Offset management
- Error handling support
Q3. How does a Consumer work?
Workflow
- Consumer subscribes to a topic.
- Kafka assigns partitions.
- Consumer polls records.
- Business logic executes.
- Offset is committed.
Consumer Flow
sequenceDiagram
Consumer->>Kafka: Poll Records
Kafka-->>Consumer: Messages
Consumer->>BusinessService: Process
BusinessService-->>Consumer: Success
Consumer->>Kafka: Commit Offset
Q4. What is a Listener Container?
Spring Kafka uses listener containers internally.
Responsibilities
- Create consumer threads
- Poll Kafka
- Invoke
@KafkaListener - Manage acknowledgements
- Handle retries
Developers rarely interact with listener containers directly.
Listener Container
flowchart LR
Kafka --> ListenerContainer
ListenerContainer --> KafkaListener
KafkaListener --> BusinessService
Q5. What is Polling?
Kafka Consumers continuously poll the broker for new records.
Example
Poll
↓
Records
↓
Process
↓
Commit
↓
Poll Again
Kafka follows a pull model rather than pushing events.
Advantages
- Better flow control
- Backpressure support
- Consumer-controlled processing
Q6. What is Offset Commit?
After successful processing,
the consumer commits the offset.
This tells Kafka
"Messages up to this offset have been processed."
Example
Partition 0
Offset 25
↓
Commit
↓
Next Poll Starts at 26
Offsets enable recovery after failures.
Q7. What are Acknowledgement Modes?
Spring Kafka supports multiple acknowledgement strategies.
| Mode | Description |
|---|---|
| RECORD | Commit after each record |
| BATCH | Commit after processing a batch |
| TIME | Commit periodically |
| COUNT | Commit after N records |
| MANUAL | Application commits manually |
| MANUAL_IMMEDIATE | Commit immediately when acknowledged |
Example
AckMode.MANUAL
Choose the mode based on reliability and throughput requirements.
Q8. How do Manual Acknowledgements work?
Example
@KafkaListener(
topics = "payments"
)
public void consume(
Payment payment,
Acknowledgment ack
){
process(payment);
ack.acknowledge();
}
Advantages
- Full control
- Commit only after successful business processing
- Useful for financial systems
Q9. Can Consumers process messages concurrently?
Yes.
Spring Kafka supports concurrent consumers.
Configuration
spring.kafka.listener.concurrency=5
Concurrent Consumers
flowchart LR
Topic --> Partition0
Topic --> Partition1
Topic --> Partition2
Partition0 --> Consumer1
Partition1 --> Consumer2
Partition2 --> Consumer3
Concurrency improves throughput but is limited by the number of partitions.
Q10. Consumer Best Practices
Keep Consumers Idempotent
Prevent duplicate processing.
Use Manual Acknowledgements
For critical financial operations.
Keep Listeners Lightweight
Delegate business logic to services.
Monitor Consumer Lag
Track processing delays.
Handle Failures Properly
Use retries and Dead Letter Topics.
Banking Example
flowchart TD
PaymentEventsTopic --> ConsumerGroup
ConsumerGroup --> KafkaListener
KafkaListener --> PaymentService
PaymentService --> PostgreSQL
PaymentService --> NotificationService
PaymentService --> AuditService
Each payment event is processed reliably before the offset is committed.
Common Interview Questions
- What is a Kafka Consumer?
- What is
@KafkaListener? - How does a Consumer work?
- What is a Listener Container?
- What is Polling?
- What is an Offset Commit?
- What are Acknowledgement Modes?
- What are Manual Acknowledgements?
- How does Consumer Concurrency work?
- Consumer best practices?
Quick Revision
| Topic | Summary |
|---|---|
| Kafka Consumer | Reads events |
| @KafkaListener | Spring consumer annotation |
| Listener Container | Manages consumer lifecycle |
| Polling | Fetch records from Kafka |
| Offset | Record position |
| Offset Commit | Mark processed records |
| AckMode | Commit strategy |
| Manual Ack | Application-controlled commit |
| Concurrency | Multiple consumer threads |
| Consumer Lag | Processing delay |
Consumer Lifecycle
sequenceDiagram
Kafka->>Consumer: Assign Partition
Consumer->>Kafka: Poll
Kafka-->>Consumer: Records
Consumer->>BusinessService: Process
BusinessService->>Database: Save
Database-->>BusinessService: Success
BusinessService-->>Consumer: Completed
Consumer->>Kafka: Commit Offset
Production Example – Banking Payment Processing
A banking platform processes payment events using Spring Kafka.
Workflow
-
A producer publishes
PaymentInitiatedEvent. -
The Payment Consumer subscribes to the payment-events topic.
-
Kafka assigns partitions to consumer instances.
-
The consumer polls messages continuously.
-
The listener validates the payment.
-
Business services:
- Debit sender account.
- Credit receiver account.
- Save transaction history.
- Publish notification events.
-
After successful completion, the consumer manually acknowledges the message.
-
Kafka commits the offset.
@KafkaListener(
topics = "payment-events",
groupId = "payment-group"
)
public void process(
PaymentEvent event,
Acknowledgment ack
){
paymentService.process(event);
ack.acknowledge();
}
flowchart LR
PaymentEventsTopic --> ConsumerGroup
ConsumerGroup --> KafkaListener
KafkaListener --> PaymentService
PaymentService --> PostgreSQL
PaymentService --> NotificationService
PaymentService --> AuditService
PaymentService --> Acknowledgment
Acknowledgment --> KafkaOffsetCommit
Production Configuration Example
spring.kafka.listener.ack-mode=manual
spring.kafka.listener.concurrency=6
spring.kafka.consumer.enable-auto-commit=false
spring.kafka.consumer.max-poll-records=500
spring.kafka.consumer.auto-offset-reset=earliest
This configuration provides reliable processing, controlled offset commits, and scalable parallel consumption for high-volume financial transactions.
Key Takeaways
- Kafka Consumers read and process events from Kafka topics asynchronously.
@KafkaListenergreatly simplifies consumer implementation by automatically managing listener containers.- Kafka consumers use a poll model, allowing them to control the rate of message consumption.
- Offsets track processing progress and enable reliable recovery after failures.
- Spring Kafka supports multiple acknowledgement modes, with manual acknowledgement providing maximum control for critical workloads.
- Consumer concurrency increases throughput but cannot exceed the number of partitions assigned.
- Keep listeners lightweight by delegating business logic to service classes.
- Combine manual acknowledgements, idempotent processing, retries, and monitoring to build reliable enterprise-grade Kafka consumers.