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.
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(oracks=-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=allprovides the highest data reliability.
Technical Example
Here's a basic example of how to set up a Kafka producer in Java with acks=all:
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 Mode | Description | Data Safety | Latency |
0 | No ack required from broker. | Low - High risk of data loss. | Very low |
1 | Ack from the leader broker only. | Medium - Low risk of data loss, unless leader fails. | Low |
all | Acks 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
- how to get acknowledgement from Kafka broker if message is produced by producer?
- How to get back Kafka producer and consumer configuration (Java API)?
- how to get basicproperties header field in pika python from rabbitmq messages?
- How to get current Kafka topic inside Kafka stream?
- How to get data from old offset point in Kafka?
- How to get kafka consume lag in java program
- How to get Kafka lag using Kafka 0.10?
- How to get kafka message's headers in Kafka Connect Sink connector with MongoDB

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.