How to return HTTP response when the result need to wait for Kafka event finish first

Viewed 922

I am using Kafka with Spring Boot. Is it possible to return the HTTP response when the result need to wait for Kafka event finish first?

I have some services and sub-processes. Service A receives HTTP request from user and it sends message to Kafka in order to do some sub-processes. Before service A returns the HTTP response, it needs to wait for service B to update the record in database.

enter image description here

1 Answers

I think it's better if service B produces the "done" message to a different Kafka topic and service A consumes it if you want to implement event driven model, as service A should not be aware and care about what happens behind the scenes, the DB update in this case.

And regarding how service A can wait and return, here's my idea in a conceptual code:

class RestController {
getInfo(input){
  Future future = KF_bean.produce(input,random_req_id)
  return future.get()
}
} 

class KF_bean {

Map map = Hashmap...

CompletableFuture produce(input, random_req_id){
   kafkalib.produce(input).withHeader(random_req_id)
future = new CompletableFuture()
   map.put(random_req_id, future)
   return future
} 

void onConsume(msg){
   req_id = msg.getHeader("vin")
   map.get(req_id).complete()
} 

}

It should work if none of instances of the service A are in same consumer group, or they should be group less consumers.

Related