How to implement request-reply (synchronous) messaging paradigm in Kafka?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka, primarily known for handling streaming data, can also be configured to support a request-reply or synchronous messaging pattern, though it is inherently designed for high-throughput, asynchronous message processing. This article outlines how to implement a synchronous communication model using Kafka, which is not a typical use case but can be necessary for certain applications requiring immediate responses.
Overview of Request-Reply in Kafka
In the request-reply pattern, a sender (client) sends a message (request) and waits synchronously for a response from the receiver (server). This pattern is typical in web services but is not directly supported by Kafka, which is built for decoupled, asynchronous message streams.
Key Challenges
- Message Correlation: Associating requests with their respective responses.
- Response Consumer Design: Ensuring the consumer of the reply listens only for responses relevant to its request.
Implementing Request-Reply in Kafka
Here's a step-by-step guide to implementing the request-reply pattern:
Step 1: Topic Setup
You need two topics:
- A
request-topicfor sending requests. - A
response-topicfor sending back responses.
Step 2: Producing Requests
Each request message should include a unique identifier (correlation ID) to correlate the response with the request.
Step 3: Consuming Requests and Producing Responses
On the server side, read from the request-topic, process the request, and produce a response to the response-topic. Include the same correlation ID in the response header.
Step 4: Consuming Responses
The original requester consumes from the response-topic. It uses a consumer that filters messages based on the correlation ID.
Alternative Approach: Using Kafka Streams
Kafka Streams API can be leveraged to manage state more efficiently, which is crucial for tracking request-reply pairs.
Best Practices and Considerations
- Timeouts and Error Handling: Implement timeouts and handle errors for scenarios where responses may never come.
- Scalability: Monitor performance, as synchronous operations can introduce bottlenecks.
Summary
| Feature | Description | Importance |
| Correlation ID | Unique identifier for each request-reply pair | Critical |
| Asynchronous Core | Kafka's inherent design and strength | High |
| Scalability | Effective use of Kafka's scalability features | Moderate |
| Error Handling | Must be robust to handle missing or delayed responses | Essential |
In conclusion, while Kafka does not natively support synchronous messaging, with careful design focusing on message correlation and response handling, it is feasible to implement a request-reply pattern effectively.
Related reading
- How to implement single-consumer-multi-queue model for rabbitMQ
- How to improve slow performance of reactive-kafka (Scala plus Akka Streams)?
- How to increase debezium / kafka connect performance for initial snapshot of millions of records and enable snapshot parallely if possible?
- How to increase the number of messages consumed by Spring Kafka Consumer in each batch?
- How to implement segment trees with lazy propagation?
- How to improve accuracy of decision tree in matlab
- How to install Kafka on Windows?
- How to install rabbitmq management plugin (rabbitmq-plugins)

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.