Kafka, Spring Kafka and redelivering old messages
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. Since being open-sourced by LinkedIn in 2011, Kafka has become one of the most popular platforms for handling real-time data feeds.
Kafka Basics
Kafka stores streams of records (messages) in categories called topics. Each record consists of a key, a value, and a timestamp. Kafka clusters are composed of multiple brokers to ensure load balancing and fault tolerance. Producers publish data to topics. Consumers use topics to subscribe to the data they are interested in. Kafka ensures message order within a partition and allows for real-time processing.
Kafka Retries and Message Redelivery
Message processing might fail due to various reasons such as temporary network issues or service downtimes. Kafka provides a mechanism to retry message delivery when a consumer fails to process the message successfully.
Redelivery can be configured using Kafka’s consumer properties like enable.auto.commit and max.poll.records. By setting enable.auto.commit to false, you can manually commit offsets. This way, if the processing fails, the same message can be reread and processed again until it succeeds, achieving exactly-once semantics.
Spring Kafka
Spring Kafka brings familiar Spring concepts to Kafka-based messaging solutions. It provides a high-level abstraction for Kafka-based messaging, simplifying the integration of Kafka into Spring applications. It leverages Spring's messaging support to make it easy to work with dynamic data.
Key Features of Spring Kafka:
- Listener Container: Facilitates consuming messages from Kafka topics.
- KafkaTemplate: Provides high-level operations to send messages to Kafka topics.
- Serialization and Deserialization: Offers simple ways to serialize and deserialize objects sent to or received from Kafka.
- Error Handling: Allows for graceful error handling in message-driven POJOs.
Example of Handling Message Redelivery in Spring Kafka
Here is a basic example of how you can handle redelivery of messages using Spring Kafka:
In the above example, the KafkaListener container manages message consumption. It provides an Acknowledgment that you can use to manually control when a message's offset is committed. If processMessage throws an exception, the message will not be acknowledged and thus will be redelivered during the next poll.
Summary Table of Kafka and Spring Kafka Features
| Feature | Kafka | Spring Kafka |
| Messaging Model | Publish-subscribe | Publish-subscribe |
| Fault Tolerance | High (replication, retention policies) | High (builds on Kafka capabilities) |
| Consumer Groups | Supported | Simplified with annotations |
| Offset Management | Manual or automatic | Enhanced controls with manual committing |
| Integration | Requires custom implementation | Simplified with Spring Boot auto-configuration |
| Exception Handling | Basic | Advanced, with retry and error handler configs |
Conclusion
Apache Kafka combined with Spring Kafka provides a robust solution for handling message-driven applications even in cases where message delivery has to be retried. This configuration not only enables resilient, scalable, and fault-tolerant system designs but also simplifies development through high-level abstractions provided by Spring Kafka.

