Does Kafka support request response messaging

Hector picture Hector · Feb 21, 2016 · Viewed 19.8k times · Source

I am investigating Kafka 9 as a hobby project and completed a few "Hello World" type examples.

I have got to thinking about Real World Kafka applications based on request response messaging in general and more specifically how to link a Kafka request message to its response message.

I was thinking along the lines of using a generated UUID as the request message key and employ this request UUID as the associated response message key. Much the same type of mechanism that WebSphere MQ has message correlation id.

My end 2 end process would be.

1). Kafka client generates a random UUID and sends a single Kafka request message. 2). The server would consume this request message extract & store the request UUID value 3). complete a Business Process using the message payload. 4). Respond with a response message that employs the stored UUID value from the request message as response message Key. 5). the Kafka client polls the response topic until it either timeouts or retrieves a message with the original request UUID value.

What I concerned about is that the Kafka Consumer polling will remove other clients messages from the response topic and increment the offsets making other clients fail.

Am I trying to apply Kafka in a use case it was never designed for?

Is it possible to implement request/response messaging in Kafka?

Answer

Chris Gerken picture Chris Gerken · Feb 21, 2016

Even though Kafka provides convenience methods to persist the committed offsets for a given consumer group, you're not required to use that behavior and can write your own if you feel the need. Even so, the use of Kafka the way you've described it is a bit awkward for the use case as each client needs to repeatedly search the topic for a specific response. That's inefficient at best.

You could break the problem into two parts, continuing to use Kafka to deliver requests to and responses from your server. The only piece you'd need to add would be some sort of API layer that your clients talk to and which encapsulates the Kafka-specific logic from your clients. This layer would need a local DB (relational or NoSQL) that could store responses by uuid making it very fast and easy for the API to answer whether a response is available for a specific uuid.