Reputation: 1
What is the correct way to do multiple calls to the same service API? The below code is how I am trying to achieve.
Flux.fromIterable(userIds)
.parallel()
.runOn(Schedulers.elastic())
.flatMap(this::getUser)
Upvotes: 0
Views: 1620
Reputation: 14712
This is to my understanding from reading the docs, but i could very well be wrong, in such case, downvote and i'll remove this answer.
Documentation about parallel
:
dividing data on a number of 'rails' matching the number of CPU cores, in a round-robin fashion.
Flux#parallel
will return a ParallelFlux
that will portion out any work on a number of so called rails
that will distribute the workload in a round-robin fashion over the number of cores the computer has. You are guaranteed that the work is placed on multiple computer cores.
Documentation about flatmap:
Transform the elements emitted by this Flux asynchronously into Publishers, then flatten these inner publishers into a single Flux through merging, which allow them to interleave.
While flatMap
(and here i could be wrong) is just making the work asynchronous by placing all items in individual Mono<T>
and the designated threads will switch between the workloads trying to perform the workload and resolve the Mono<T>
s as fast as possible using the designated threads in the defined scheduler. Here there seems to be no guarantee that multiple cores will be used.
This is my understanding from reading the documentation.
Parallelizing Work with ParallelFlux
My personal opinion is that it is probably overkill to designate its own core for each request, there is probably some setup time to assign the jobs to cores etc. etc.
I would only use parallel for CPU intensive work, while regular flatMap
will work fine for blocking
tasks where the threads can just easily just switch to other work when waiting for a response.
Upvotes: 2