In (Java) reactive programming, what is the difference between a Future<T>
and a (Project Reactor) Mono<T>
? Both seem to be means for accessing the result of an asynchronous computation at a time in the future when the computation is complete. Why introduce the Mono
interface if Future
already does the job?
The greatest difference is that a Mono<T>
can be fully lazy, whereas when you get hold of a Future<T>
, the underlying processing has already started.
With a typical cold Mono
, nothing happens until you subscribe()
to it, which makes it possible to pass the Mono
around in the application and enrich it with operators along the way, before even starting the processing.
It is also far easier to keep things asynchronous using a Mono
compared to a Future
(where the API tends to drive you to call the blocking get()
).
Finally, compared to both Future
and CompletableFuture
, the composition aspect is improved in Mono
with the extensive vocabulary of operators it offers.