Implementation HTTP synchronous request response

133 Views Asked by At

I am working on containerization application where a front-end application calls HTTP request to API gateway. The request data received at API Gateway is forward to Micro service via Kafka. But I have to send the response back the result as response back to API gateway and back to front-end application. Can anyone please guide me how can I achieve the synchronous request and response between API gateway and micron service via Kafka.

I have tried with Producer and Consumer, which is like Fire and forget where I am unable to respond back the request.

Producer Code :

using var p = new ProducerBuilder<string, string>(config).Build(); 
// Send the message to our test topic in Kafka
var dr = await p.ProduceAsync("test", message);

Consumer Code :

using var c = new ConsumerBuilder<Ignore, string>(conf).Build(); 
c.Subscribe("test"); 
// Consume a message from the test topic. 
var cr = c.Consume(cts.Token);
1

There are 1 best solutions below

0
On

Kafka Consumers just aren't synchronous. Don't try to make it.

If you need a response, then return 202-Accepted or 400-Bad Request or 50x - Error from the "Producer" API, based on some conditions.

Call a new API to request data to poll a limited number of records from some offset. Don't Subscribe and poll in an endless loop since HTTP clients have timeouts and won't accept waiting for messages. If you don't get a certain event within a specific poll interval (max.poll.records), then the "HTTP consumer" will need to poll again.