What does prefetch mean in Project Reactor?

Viewed 3653

I am using Project Reactor and am using Flux.flatMapIterable. (I basically have a stream of object A; for each object A I flatmap it into a stream of object B and I make a new flux out of it.)

I am trying to understand what the prefetch setting does. In my case, each object A could potentially be converted into any number of object B (between 0 to N; N is large).

I just want to understand:

  • What is prefetch? Does it only apply to the initial request? (i.e. if I set it to 1, is Project Reactor intelligent enough to increase the request size if it finds out that 1 is too small?)

  • Is it relevant for my situation here? I was thinking about setting the prefetch to 1 in order to be conservative, since a single A object has the potential to be flatmapped into a large stream of B objects.

1 Answers

Prefetch will affect how many items Reactor requests from the Publisher in it's first request. It is an upper bound that affects subsequent requests where subsequent requests will be triggered when 75% of the prefetch amount has been emitted so it will not increase automatically.

It is typically used for scenarios where consumer(s) request a large amount of data but the data source behaves better or can be optimized with smaller requests (eg. database paging, etc...).

Whether it is relevant for your use case depends on the characteristics of your publisher.

From the [documentation] on configuring back pressure 1

You might also have noticed that some operators have variants that take an int input parameter called prefetch. This is another category of operators that modify the downstream request. These are usually operators that deal with inner sequences, deriving a Publisher from each incoming element (like flatMap).

Prefetch is a way to tune the initial request made on these inner sequences. If unspecified, most of these operators start with a demand of 32.

These operators usually also implement a replenishing optimization: once the operator has seen 25% of the prefetch request fulfilled, it re-requests 25% from upstream. This is a heuristic optimization made so that these operators proactively anticipate the upcoming requests.

Related