How to send messages synchronously 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. When you publish (or produce) a message to Kafka, you may choose how messages are sent: synchronously or asynchronously. This article focuses on synchronous message sending in Kafka, why it's important, how to implement it, and considerations for its use.
Understanding Synchronous Messaging in Kafka
In synchronous messaging, the producer sends a message to a Kafka broker and waits for an acknowledgement (ACK) from the broker before proceeding. This mode ensures reliability at the cost of latency and throughput. There are several configurations and considerations involved in implementing synchronous messaging in Kafka.
How to Implement Synchronous Messaging
To send messages synchronously, Kafka producers require specific configurations. Below is a step-by-step guide:
- Configuration of the Producer:
- acks: Set
acks=all(or-1) which means the leader will wait for the full set of in-sync replicas to acknowledge the record. This guarantees that the record will not be lost as long as at least one in-sync replica remains alive. - retries: Set the number of retries in case of message send failure.
- retry.backoff.ms: Set the time to wait before attempting to retry a failed send.
- Sending a Message: Create a producer record and send it. Since Kafka is inherently asynchronous, to achieve synchronous behavior, the
send()method is used along withget()to block the thread until it receives a response.
Trade-offs
While synchronous communication ensures reliable message delivery, it comes at the cost of performance. Here are few important trade-offs:
- Throughput vs. Durability: Synchronous sends are slower because the producer waits for acknowledgements, reducing throughput.
- Latency: Increased latencies as each message has to wait for the acknowledgment.
- Resource Utilization: Higher resource usage on the broker and the producer as more waiting and processing is involved.
Use Cases
Synchronous messaging is critical in scenarios where data loss cannot be tolerated, such as banking transactions or critical stateful operations. It is used less frequently in scenarios where latency and throughput are critical.
Summary Table
| Configuration | Description | Impact |
| acks=all | Producer waits for the full set of in-sync replicas to acknowledge | Highest data durability |
| retries | Number of retries in case of send failures | Can eventually succeed in message delivery after a failure |
| retry.backoff.ms | Time to wait before retrying a failed send | Affects the time to recover from failures |
Advanced Considerations
- Network Configuration: Optimal network settings can help minimize the impact on latency and throughput.
- Batch Size: Configure producer batching along with synchronous sends to improve throughput.
- Timeouts: Appropriate timeout settings are necessary to prevent the system from hanging indefinitely in case of network or broker failures.
Conclusion
Implementing synchronous message delivery in Kafka provides guaranteed message delivery at the expense of latency and throughput. It's crucial to balance these factors based on the application's specific needs and the reliability required from your messaging system.

