Kafka
Acknowledgement
Data Streaming
Message Queues
Information Technology

How to get Acknowledgement from Kafka

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a distributed streaming platform capable of handling trillions of events a day. One crucial feature of Kafka is its ability to acknowledge messages, ensuring data is properly received and processed. Understanding how acknowledgments work in Kafka can help you design systems with reliable data delivery and fault tolerance.

Understanding Kafka Acknowledgements

When a producer sends a message to a Kafka topic, the message is not considered "safe" until it is acknowledged by the Kafka brokers. This mechanism ensures data integrity and prevents data loss. The acknowledgement process in Kafka is controlled by the acks configuration setting in the Kafka producer.

Acknowledgement Configurations

There are three main configurations for the acks setting:

  • acks=0 — The producer will not wait for any acknowledgment from the broker. This setting means that as soon as the message is sent, the producer assumes it was successfully written. However, there's a high risk of data loss if the broker goes down before the message is actually written to disk.
  • acks=1 — The producer will wait for the leader broker to acknowledge the message. This means that as soon as the leader replica receives the message and writes it to its local log, an acknowledgment is sent to the producer. This strikes a balance between performance and reliability.
  • acks=all (or acks=-1) — The producer waits for all in-sync replicas (ISR) to acknowledge the message. This is the safest mode, ensuring that the message is replicated across all ISR before an acknowledgment is sent back to the producer. acks=all provides the highest data reliability.

Technical Example

Here's a basic example of how to set up a Kafka producer in Java with acks=all:

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
4props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
5props.put("acks", "all");
6
7Producer<String, String> producer = new KafkaProducer<>(props);
8
9try {
10    producer.send(new ProducerRecord<>("test-topic", "key", "value")).get();
11    System.out.println("Message sent successfully");
12} catch (Exception e) {
13    e.printStackTrace();
14}
15producer.close();

In this example, the producer is configured to wait for acknowledgments from all in-sync replicas. This guarantees that the message is not only accepted by the leader but also replicated successfully.

Handling Failures and Retries

Kafka producers can automatically retry sending messages if acknowledgments are not received due to network issues, broker failures, or other transient problems. The retry behavior can be controlled using the following producer configurations:

  • retries - This setting specifies the number of retries if the initial send fails.
  • retry.backoff.ms - This controls the time to wait between retries.

However, using retries increases the chance of duplicates if the producer sends the same message more than once. Thus, the consumer or the system should be designed to handle potential duplicate messages.

Table: Kafka Acknowledgement Modes

Ack ModeDescriptionData SafetyLatency
0No ack required from broker.Low - High risk of data loss.Very low
1Ack from the leader broker only.Medium - Low risk of data loss, unless leader fails.Low
allAcks from all in-sync replicas.High - Very low risk of data loss.High

Additional Considerations

Consumer Acknowledgments

Alongside producer acknowledgments, Kafka also has concepts where consumers acknowledge the messages they have processed. This is typically managed through consumer offsets, which mark how much of each topic partition has been consumed.

Cluster Configuration

The reliability guaranteed by acks=all also depends on the correct configuration of your Kafka cluster, including settings like min.insync.replicas, which controls the minimum number of replicas that must acknowledge a record before the write is considered successful.

In conclusion, understanding and configuring Kafka acknowledgments properly is critical for designing robust distributed systems with Kafka at their core. The choice of acknowledgment mode impacts both performance and data safety, and should be made based on the specific requirements and constraints of your system.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.