I am using SpringDoc Open Api 3 for my rest API. Right now I'm using the WebMvc version. Is there any advantage to switching to the WebFlux version? Wouldn't using WebClient (or some other async client) on the client side be the same thing except the async would happen on the client side instead? At the end of the day, a Rest method can use async methods internally, but trying to see if it is worth it to migrate the exposed methods to WebFlux.
1 Answers
No, it's a completely different thing. Spring MVC operates in thread-per-request model. You have 100 concurrent request = you have 100 threads to handle those requests. 100 threads is already a lot, now imagine 1k, 10k or even 100k - completely impossible in this model.
Point is that those threads are not doing work 100% of the time. If you call a database or another service then the thread is just waiting for the response, not doing the work it could be doing in that time.
That's the Webflux way, you use fewer threads, because instead of waiting for response from external service, threads are working in that time, making it possible to handle 1k concurrent requests without much of a problem.
Why is everyone not doing this then: less resources used, better performance etc.? First and I think the most important - it's incredibly hard to do. Program flow is not as easy as in synchronous programming, debugging is really hard, stack traces become basically useless, you need to be very careful not to block everything. Second - the benefit becomes worthy at some threshold, most apps don't need to handle thousands of concurrent users. Before that threshold, not only there is no benefit, it may be even worse performance-wise, while paying the price in developer knowledge and experience mentioned in first point. Three - for this to work you need the whole flow to be asynchronous, otherwise you will just block the event loop - calls to external services and most importantly the database - you need asynchronous database driver for that and not every database supports it.

- 5,421
- 2
- 19
- 38
-
The OP is talking about consuming the API from component tests, not the implementation of the controllers. – chrylis -cautiouslyoptimistic- Jul 06 '20 at 23:36
-
@chrylis-cautiouslyoptimistic- No, I AM asking about the implementation of the controllers. I was asking if there is a diff implementing the controllers as Webflux vs using WebClient on the client side. – SledgeHammer Jul 06 '20 at 23:48
-
@Shadov Thanks for clearing up the threading model on the server side is the diff. So what kind of request per minute rate would you suggest for switching to webflux? From your description, sounds like a pretty low bar where it becomes an advantage. – SledgeHammer Jul 06 '20 at 23:52
-
@Shadov been doing some research and it seems like the reactive driver for mssql is pretty primitive. – SledgeHammer Jul 07 '20 at 03:41