Toffee Lu
Toffee Lu

Reputation: 123

Do we have to write reactive code inside WebFlux?

I know that a reactive controller should return a Flux<T> or Mono<T>, which means it's reactive on http handler level.

But if we do an external http call in a controller with non-reactive code, which have to wait a long time IO until that http call response, what will happen if 10000 users use http to call this controller at the same time? Let's assume there is only one thread to handle the code inside controller, so will more requests be handled during the IO?

If no, do we have to use reactive code such as WebClient and ReactiveRepository to call external http API and CRUD on DB?

If yes, how can that be implemented? Because it's just lines of non-reactive code, how Java know like "hey, it's waiting for response, let's handle another event first"?

Upvotes: 1

Views: 1233

Answers (2)

Brian Clozel
Brian Clozel

Reputation: 59231

Doing blocking I/O within a reactive pipeline (e.g. a reactive controller method) is forbidden; doing so can lead to serious problems at runtime or even terminal errors if a block operator is used.

Reactor provides infrastructure to wrap blocking calls and schedule that work on specific threads. See the How do I wrap a synchronous, blocking call section in the reactor reference documentation. Doing that will work but is likely to have a negative effect on performance.

Upvotes: 2

Toffee Lu
Toffee Lu

Reputation: 123

I did an experiment myself, looks like we have to use reactive code inside WebFlux so that everything is reactive and the performance is really high

Upvotes: 0

Related Questions