Guaranteed delivery of multiple messages to Kafka cluster
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka, an open-source stream-processing software platform developed by the Apache Software Foundation, is written in Scala and Java. It aims to provide a high-throughput, low-latency platform for handling real-time data feeds. A critical aspect of working with Kafka is ensuring the reliable delivery of messages, which can be somewhat complex given its distributed nature. This article will delve into how the delivery of multiple messages can be guaranteed in a Kafka cluster, exploring the mechanisms and configurations involved.
Understanding Kafka's Core Concepts
Before discussing message delivery guarantees, it's important to understand several key Kafka concepts:
- Producer: The component that publishes messages to Kafka topics.
- Consumer: The component that subscribes to topics and reads messages.
- Broker: A server in the Kafka cluster that stores data and serves clients.
- Topic: A category or feed name to which messages are published.
- Partition: Topics are split into partitions for scalability, each partition can be hosted on different brokers.
Message Delivery Guarantees
Kafka offers three levels of message delivery semantics:
- At most once: Messages may be lost but won't be redelivered.
- At least once: Messages are never lost but may be redelivered.
- Exactly once: Each message is delivered exactly once.
Configuring Producers for Reliable Delivery
To ensure messages are reliably delivered, you must configure the Kafka producer correctly. Key settings include:
- acks: This setting determines how many partition replicas must receive the message before considering a write successful.
acks=0means the producer will not wait for any acknowledgment from the server.acks=1waits for only the leader replica to acknowledge.acks=allensures all in-sync replicas acknowledge the message, providing the strongest guarantee. - retries and retry.backoff.ms: These settings control the retry mechanism if message delivery fails.
retriesset to a higher number (like 10) and a reasonableretry.backoff.msensure the producer attempts to resend messages in case of failures.
Idempotent Producers
From Kafka version 0.11 onwards, the concept of idempotent producers was introduced. An idempotent producer can guarantee that messages are delivered exactly once to a particular partition during a single producer session. This is achieved through internal sequence numbers that prevent duplicates even after retries.
Consumer Configurations for Reliable Processing
On the consumer side, ensuring that messages are processed once involves managing offsets carefully. Consumers commit offsets to indicate which messages have been processed. If a consumer acknowledges an offset, it should not read any message before that offset again.
- auto.offset.reset: This setting controls the behavior when no initial offset is found or the current offset does not exist anymore (e.g., due to data being deleted).
earliestcauses Kafka to revert to the earliest offset, which may lead to reprocessing. - enable.auto.commit: By setting this to
false, you can have explicit control over when offsets are committed, which usually happens after the message has been processed.
Ensuring Durability and Fault Tolerance
To enhance durability:
- Replication factor: Setting this for each topic to more than one ensures that data is replicated to multiple brokers. In the event of a broker failure, one of the replicas can take over.
- min.insync.replicas: This setting in the broker config enforces how many replicas must acknowledge a record for it to be considered committed, ensuring data is not lost due to a broker failure shortly after acknowledgement.
Summary Table
| Feature | Setting | Description |
| Acknowledgments | acks | Controls how acknowledgments from brokers are handled.
Choices: 0, 1, all |
| Retries | retries | Number of times the producer retries sending a message. |
| Idempotence | enable.idempotence | Ensures messages are not duplicated during retries. |
| Offset Handling | auto.offset.reset | Decides where the consumer starts reading if no offset is saved. |
| Offset Committing | enable.auto.commit | Controls automatic offset committing. |
| Replica Management | min.insync.replicas | Minimum number of replicas that must acknowledge a record. |
Ensuring reliable message delivery in Kafka often involves a careful configuration of both producer and consumer settings, along with a robust understanding of Kafka's internal mechanisms. By correctly leveraging these features and settings, Kafka can serve as a highly reliable component of a data architecture.
Related reading
- Guidelines to handle Timeout exception for Kafka Producer?
- Handling exceptions in Kafka streams
- Handling long running tasks in pika / RabbitMQ
- Hardware requirement for apache kafka
- Hadoop - Directory Structure and Distributed Cache
- Hadoop - Large files in distributed cache
- Have Docker wait for Kafka to startup before running tests
- Having a hard time getting Rabbitmq Server started and wonder why keep getting this Error initdo_boot/3 line 817

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.