Since there seems to be misunderstandings i will try to answer the questions to the best of my knowledge.
How safe is it to use .block()
and is it ok in general to block threads in WebClient
?
Blocking is always safe but whether or not it affects performance is another thing. When a request comes in, it gets assigned a thread. When we do a request using RestTemplate
the same thread will do the external request, and RestTemplate
will block that thread under the hood in wait for the response.
This is by no means efficient usage of a thread, but its completely safe, and this is how most web servers in general have been working for the past 20 years.
When using WebClient
in a non-reactive application and you block a Mono<T>
(which you essentially will be doing) the framework will first check that the thread is a type of thread that you are allowed to block (not a nio-thread) and after that it uses a CountDownLatch
which pauses/blocks the calling thread at CountDownLatch#await
until the first onNext/onComplete/onError signal arrives. This is completely fine in a blocking application. You can find the relevant code here.
When you add WebClient
to the class path you will automatically get netty as the underlying server, which could be good to know. If you wish to change that out, then you need to be explicit about it.
Also, it is recommended that if you do multiple requests, then you should chain on as many reactive calls as possible before resorting to block
.
If you want to move over to a reactive application, then this is a very good way of slowly transferring an application, by slowly doing more and more reactive stuff and then calling block
to return to the regular world.
Are you fully reactive? no, are you a blocking web server like before, well yes. Is it worse that RestTemplate
most likely not. Are you better than before, from a maintenance standpoint, yes since spring has officially gone out with that there is not going to be any more updates to RestTemplate
.
Is the mechanics behind blocking calls with WebClient
similar to what RestTemplate
does?
Well this is hard to tell, because RestTemplate
is mainly just a wrapper around the HttpClient
provided by the underlying server implementation.
How the blocking is written is probably different, but what they do in the end, is most likely the same. A Mono<T>
blocks using a CountDownLatch
with repeated calls to getCount
and then inbetween blocks with a call on latch await
until the response is back. I havn't looked at the different HttpClient
s that RestTemplate
is wrapping, you need to read up on each of them (tomcat, jetty, undertow, etc. etc.)
Is there a possibility that the performance would be worse than in case I just use RestTemplate
?
This is extremely hard to tell because performance is not black and white. It all depends on hardware, what type of jobs are to be done, how the code is written, thread pool sizes, operating system etc..
Netty
is a fully event driven server and it is starting to become the de facto standard of web server in the Java community. Undertow decided to switch out their entire core to the netty core
, because it was so good, and easier to maintain.
As Netty
is event driven, running it as an old server with one thread per request could hurt performance since it is not optimized for that type of work, but on the other hand when you run it fully event driven it will shine.
The only way to answer this, is to do your own benchmarks, we can't answer that for you.
If you want to know more about how netty actually works under the hood a recommend reading the book Netty in Action which is not free but very good read to understand the innerworkings of Netty
and its async
threading model.