When @Async is applied to a method making an external call, that Tomcat request thread is now able to handle the next incoming request. But what happens when the external service returns?
If the same thread pool is used, theoretically when the external service returns, all the threads could be busy and now we have to wait for a free thread - which means using @Async can actually be slower in this case.
I'm asking just to see the benefits of @Async and to confirm my understanding.
In itself calling an @Async method doesn't free up a Tomcat thread - you need to combine it with e.g. DeferredResult<> or AsyncContext/startAsync() - then the asynchronous method will run on a thread from a separate pool (configurable via Spring), and as you say the original thread can be freed to process another request.
If your Tomcat thread pool is processing other, shorter-lived types of request as well, this allows many of these requests to be processed while waiting for your slow external request to complete, so obviously that's an immediate benefit. If your other requests are frequent enough and/or intensive enough that they're maxing out your Tomcat thread pool, you're going to have performance issues anyway, async threads or not.
If on the other hand your Tomcat request threads are always just firing off these external calls asynchronously, it would be very unlikely that your Tomcat pool would be exhausted before you hit other limits. The advantage in this case is that you can have more requests in parallel than threads in your Tomcat thread pool.
All of the above assumes you are using DeferredResult<>, AsyncContext/startAsync() etc. to free up the Tomcat thread, but even if you're not doing that there are still cases where @Async can be useful, e.g.
@Async request, then do your local work, then wait for the request to finish.As I understand, an @Async method will be run in a new thread. That new thread will remain occupied until the external service returns. So, if your pool lacks threads, it will impact when the method is called, and the request will hang until a free thread is found.
All this can be verified by logging Thread.currentThread().getName().
Yes, if there will be lack of free threads, there would be no benefit of @Async. A solution would be to increase the number of threads in the pool.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With