Kafka
Spring Kafka
Message Redelivery
Message Queuing
Application Development

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:

java
1import org.springframework.kafka.annotation.KafkaListener;
2import org.springframework.kafka.support.Acknowledgment;
3import org.springframework.kafka.annotation.EnableKafka;
4import org.springframework.stereotype.Component;
5
6@EnableKafka
7@Component
8public class KafkaConsumer {
9
10    @KafkaListener(topics = "exampleTopic")
11    public void listen(String message, Acknowledgment acknowledgment) {
12        try {
13            processMessage(message);
14            acknowledgment.acknowledge();
15        } catch (Exception ex) {
16            // log the exception
17        }
18    }
19
20    private void processMessage(String message) {
21        // process the message
22    }
23}

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

FeatureKafkaSpring Kafka
Messaging ModelPublish-subscribePublish-subscribe
Fault ToleranceHigh (replication, retention policies)High (builds on Kafka capabilities)
Consumer GroupsSupportedSimplified with annotations
Offset ManagementManual or automaticEnhanced controls with manual committing
IntegrationRequires custom implementationSimplified with Spring Boot auto-configuration
Exception HandlingBasicAdvanced, 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.


Course illustration
Course illustration