spring-bootspring-webfluxspring-async

Spring Web Flux and Spring Async method what is the difference?


I am new to Spring Reactive programming and Spring Async methods. I have a doubt. By using spring webflux we can have reactive programming so that we can execute a particular piece of code and our current thread need not to wait for it to finish the execution. Similarly by using @async method we can make that particular piece of code run in a different thread so that our current thread wont wait.

So in these cases how webflux and async method are different and when to use which?


Solution

  • They're chalk and cheese - they're different enough that they're not really directly comparable.

    The @Async annotation simply executes the annotated method in a separate thread, and doesn't block on that thread before returning (doesn't wait for it to finish.) The normal use case for async is for fire-and-forget (or fire-and-read-later) scenarios, for long running tasks, in a traditional blocking environment.

    Webflux on the other hand chucks out the traditional blocking threading model entirely, and instead serves every request through an event loop of just a handful of threads that must never block. To do so requires a fundamentally different style of writing code than a traditional blocking application. It's not just used for fire-and-forget scenarios, it becomes the default way you handle every request that comes into your application.