user1189332
user1189332

Reputation: 1941

Spring Reactor WebClient how does it achieve non-blocking?

Basic question: How does Spring Reactors WebClient achieve non blocking when compared to RestTemplate? Doesn't it have to block somewhere after it has dispatched the request to the external service (for example)? HTTP by nature is synchronous right? So the calling application has to wait for the response? How does the thread know the context to react upon the response from the service?

Upvotes: 5

Views: 3095

Answers (1)

Brian Clozel
Brian Clozel

Reputation: 59211

There are several separate questions here.

  • How I/O operations are managed?
  • What's the threading model behind this runtime?
  • How does the application deal with the request/response model behind HTTP?

In the case of WebClient and project Reactor, the Netty event loop is used to queue/dispatch/process events. Each read/write operation is done is a non-blocking manner, meaning that no thread sits waiting for an I/O operation to complete. In this model, concurrency is not done through thread pools, but there's a small number of threads that process unit of work which should never block.

From a pure HTTP standpoint (i.e. if you were capturing the HTTP packets on the network), you'd see no big difference between a RestTemplate and a WebClient call. The HTTP transport itself doesn't support the backpressure concept. So the client still has to wait for the response - the difference here is that the application using that WebClient won't waste resources on waiting for that operation to complete - it will use them to process other events.

For more information on that, please check out the reactive programming introduction in the Reactor reference documentation and this talk given by Rossen Stoyanchev that explains things well if you're used to the typical Servlet container model.

Upvotes: 5

Related Questions