Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microservices asynchronous response

I come across many blog that say using rabbitmq improve the performance of microservices due to asynchronous nature of rabbitmq.

I don't understand in that case how the the http response is send to end user I am elaborating my question below more clearly.

  1. user send a http request to microservice1(which is user facing service)

  2. microservice1 send it to rabbitmq because it need some service from microservice2

  3. microservice2 receive the request process it and send the response to rabbitmq

  4. microservice1 receive the response from rabbitmq

NOW how this response is send to browser? Does microservice1 waits untill it receive the response from rabbitmq? If yes then how it become aynchronous??

like image 476
jeff Avatar asked Oct 09 '19 14:10

jeff


People also ask

How do you handle asynchronous calls in microservices?

You might use any protocol to communicate and propagate data asynchronously across microservices in order to have eventual consistency. As mentioned, you could use integration events using an event bus or message broker or you could even use HTTP by polling the other services instead.

How asynchronous communication works in microservices?

In asynchronous event-driven communication, one microservice publishes events to an event bus and many microservices can subscribe to it, to get notified and act on it. Your implementation will determine what protocol to use for event-driven, message-based communications.

Should microservices be asynchronous?

In this article I showed why asynchronous communication is the way to go in a microservices architecture. It's more convenient for testing and development, makes deployment easier and is more reliable and scalable.

Why microservices are asynchronous communication?

Microservices that communicate in an asynchronous manner can use a protocol such as AMQP to exchange messages via a message broker. The intended service receives the message in its own time. The sending service is not locked to the broker. It simply fires and forgets.


1 Answers

It's a good question. To answer, you have to imagine the server running one thread at a time. Making a request to a microservice via RestTemplate is a blocking request. The user clicks a button on the web page, which triggers your spring-boot method in microservice1. In that method, you make a request to microservice2, and the microservice1 does a blocking wait for the response.

That thread is busy waiting for microservice2 to complete the request. Threads are not expensive, but on a very busy server, they can be a limiting factor.

RabbitMQ allows microservice1 to queue up a message to microservice2, and then release the thread. Your receive message will be trigger by the system (spring-boot / RabbitMQ) when microservice2 processes the message and provides a response. That thread in the thread pool can be used to process other users' requests in the meantime. When the RabbitMQ response comes, the thread pool uses an unused thread to process the remainder of the request.

Effectively, you're making the server running microservice1 have more threads available more of the time. It only becomes a problem when the server is under heavy load.

like image 127
Kieveli Avatar answered Nov 14 '22 21:11

Kieveli