One common mistake which we should avoid while using @Async and @Cacheable in Spring boot
When solving performance bottlenecks first two things which come to our mind is parallelization and caching. And Spring boot provides out of the box feature like @Async and @Cacheable to do the same.
Okay, so let’s jump right into the topic
Yep, this is the root cause. Both @Async and @Cacheable doesn’t work when they are called from within the same class.
Spring relies on Spring Proxy AOP to intercept calls to annotated methods like @Async or @Cacheable but when you call the underlying method directly it bypasses all the dynamic proxy and that’s the reason why these annotations doesn't do the job which it is supposed to do even though all the configuration is correct.
To resolve this issue, you can create a separate service which will have Async or Cacheable annotation and then call that service from the main service.
If you want to read more on the proxy and how does it work, go through these docs.