Kafka
Acknowledgements
Pub/Sub Model
Message Processing
Data Streaming

How ack works for pub/sub in Kafka?

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 Kafka is a distributed system, it maintains feeds of messages in categories called topics. At a fundamental level, Kafka provides a durable message store, similar to a message queue or enterprise messaging system.

One key feature of Kafka is its use of the publish-subscribe pattern along with durable storage and processing capabilities. In such a system, message delivery semantics are crucial, and acknowledgments (acks) play a critical role in the reliability and durability of messages.

How Acknowledgments Work in Kafka

In Kafka, the process of acknowledgments is crucial for ensuring data integrity and delivery guarantees. Essentially, acks determine how a producer receives confirmations from the broker:

1. Basic Acknowledgment Mechanism

When a producer sends a message to a Kafka broker, it can specify its requirement for acknowledgments using the acks configuration parameter:

  • acks=0: The producer will not wait for any acknowledgment from the broker. This setting provides the lowest latency but the weakest durability guarantees because a message can be lost if the broker fails before the message is written to disk.
  • acks=1 (default setting): The producer receives an acknowledgment as soon as the lead broker has written the message to its local log but before the data has been written to any replicas. This provides improved durability compared to acks=0.
  • acks=all: This setting ensures the highest level of durability. The producer will receive an acknowledgment only after all in-sync replicas have written the message to their logs.

This acknowledgment pattern provides flexibility to producers in terms of message durability versus latency.

2. In-sync Replicas (ISR)

Kafka maintains a set of in-sync replicas (ISRs) for each partition. These are the replicas that are fully synced with the leader and are guaranteed to have committed all messages up to a certain point in the log. The acks mechanism is tightly integrated with the concept of ISR.

When acks=all, the leader waits until all replicas in the ISR have written the data to their logs before issuing an acknowledgment to the producer. This guarantees that the message will not be lost as long as at least one replica in the ISR survives.

3. Impact on Producer and Consumer

For producers, the choice of acknowledgment strategy (acks=0, acks=1, acks=all) impacts message durability and the latency of message sends. For consumers, however, since Kafka retains all messages for a configurable retention period, there is a guarantee that a message can be consumed even if the consumer falls behind or needs to replay messages for some reason.

Kafka's At-Least-Once Delivery Semantics

At-least-once delivery semantics in Kafka ensure that messages are not lost but may be redelivered in case of failure scenarios. Here, acks play an integral role:

  • If acks=1 or acks=all, producers can resend messages that have failed to be acknowledged due to broker failures, ensuring messages are not lost.
  • Consumers can process messages more than once if they fail to update their offsets accordingly, which allows at-least-once processing.

Performance Considerations

The choice of acks affects the performance of Kafka messaging:

acks ValueLatencyThroughputDurability
0Very lowHighLow
1MediumMediumMedium
allHighLower due to ISR overheadHigh

This table helps producers decide on an optimization strategy balancing throughput, latency, and durability based on their specific use-case requirements.

Conclusion

In conclusion, the acknowledgment mechanism in Kafka offers configurable options for producers to balance the trade-offs between data durability and system performance. By comprehensively understanding and rightly setting the acks parameter, organizations can ensure that their Kafka deployments effectively meet their reliability and performance benchmarks.


Course illustration
Course illustration

All Rights Reserved.