Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ParallelFlux vs flatMap() for a Blocking I/O task

I have a Project Reactor chain which includes a blocking task (a network call, we need to wait for response). I'd like to run multiple blocking tasks concurrently.

It seems like either ParallelFlux or flatMap() could be used, bare-bone examples:

Flux.just(1)
    .repeat(10)
    .parallel(3)
    .runOn(Schedulers.elastic())
    .doOnNext(i -> blockingTask())
    .sequential()
    .subscribe()

or

Flux.just(1)
    .repeat(10)
    .flatMap(i -> Mono.fromCallable(() -> {blockingTask(); return i;}).subscribeOn(Schedulers.elastic()), 3)
    .subscribe();

What are the merits of the two techniques? Is one to be preferred over the other? Are there any alternatives?

like image 501
Corin Fletcher Avatar asked Apr 07 '17 03:04

Corin Fletcher


1 Answers

parallel is tailored for parallelization of tasks for performance purposes, and dispatching of work between "rails" or "groups", each of which get their own execution context from the Scheduler you pass to runOn. In short, it will put all your CPU cores to work if you do CPU intensive work. But you're doing I/O bound work...

So in your case, flatMap is a better candidate. That use of flatMap for parallelization is more about orchestration.

These are pretty much the 2 alternatives, if you don't count the slightly different flavor of flatMap that flatMapSequential is (concatMap doesn't really allow for parallelization).

like image 132
Simon Baslé Avatar answered Oct 20 '22 21:10

Simon Baslé