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?
-
You may want to read this [another answer](https://stackoverflow.com/a/49678428/697630). – Edwin Dalorzo Aug 15 '18 at 05:05
1 Answers
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.

- 56,583
- 15
- 167
- 176
-
2With my limited understanding of Java NIO, am I (conceptually and not strictly technically) correct by saying there will be some SocketChannel opened and registered with a Selector which will then be managed by the event loop that spins infinitely to listen to the selectors to receive events from these channels and then react (calls the actual code waiting for the response) when the channel emits an event? – user1189332 Aug 18 '18 at 10:10
-
1Sounds right to me. Note that WebClient can support other http client libraries so the runtime model might vary. – Brian Clozel Aug 18 '18 at 10:44