1

Reactor 3 has 2 primary data types both of which are reactive stream publishers

  • reactor.core.publisher.Mono<T>
  • reactor.core.publisher.Flux<T>

I understand the difference between Mono being a stream of 0 or 1 elements and Flux being a stream of 0 or N elements.

Since both Mono and Flush are implementing org.reactivestreams.Publisher<T> why do we need both types why not just use Flux for everything?

ams
  • 60,316
  • 68
  • 200
  • 288
  • It's just semantic for developer to know what behavior to expect from the publisher. – JEY Jan 09 '18 at 16:04
  • Possible duplicate of [Mono vs Flux in Reactive Stream](https://stackoverflow.com/questions/47988433/mono-vs-flux-in-reactive-stream) – Jesper Jan 10 '18 at 12:17

1 Answers1

7

Think about what happens in the non-reactive world: we could decide to make all the functions return List<T>, even when we know they will only return a single element. Example:

public List<T> findAll()     <-- looks ok

public List<T> findFirst()   <-- seems strange?

public T findFirst()         <-- better now?

So why don't we always return List<T>? because it's more convenient that sometimes we use List<T> and others simply T, this way it is easier on the user of the function to understand what to expect.

Now if we convert these examples to the reactive world, we get this:

public List<T> findAll()   -->   public Flux<T> findAll()

public T findFirst()       -->   public Mono<T> findFirst()

Sure, we could always use Flux<T> as we could always use List<T>, but it is just more convenient to be able to differentiate when there multiple items or just one.

ESala
  • 6,878
  • 4
  • 34
  • 55