Search code examples
javaspringreactive-programmingproject-reactorreactive-streams

Is reusing project reactor publisher Flux/Mono considered a best practice?


Project Reactor publisher Flux/Mono are reusable. We can subscribe multiple time to the same publisher. By doing that we avoid useless object creation but i feel like it make the code harder to read in some case.

Is there specific argument that encourage to reuse Flux/Mono ?


Solution

  • There’s no best practice as such. A reactive publisher is not “reusable” in general. Depends on whether the it is a hot or cold source. You can, or course, cache some elements if you need to. For example, if your source is emitting the stock prices for a company, it makes no sense to cache or reuse the historical prices (unless for analysis but that’s irrelevant).