At a high level, imperative code calls an object and the request stays open until all of the results are gathered, returning the entire result set. The connection stays open and is closed when results are sent back to the client. As you might imagine, for large requests, this might take some time and has the possibility to either overload the client or server resources.
Reactive code, on the other hand, allows for incremental results returned, as they become available or as the resource can handle. This means that large result sets can be returned in smaller pieces and/or over a period of time. It grants the opportunity for systems to specify how much they can handle to avoid overload.
For our demo purposes, imperative or reactive does not make a difference in efficiency or performance, but depending on the architecture of a production system or the stability of certain components, the choice between reactive or imperative can make a big difference between a smoothly-running system and IT’s version of fire-fighting.
Within the projects, differences between the sdn-marvel-basic and sdnrx-marvel-basic code boil down to the following changes for the reactive version:
Repository interfaces - return types for methods are either
Controller classes - return types for methods are either
Flux<>, media type for
/characters endpoint results in
TEXT_EVENT_STREAM_VALUE and a delay of 1 second between returned elements is added.
Application class (SdnrxMarvelBasicApplication) - an additional bean with the
Outside of those, the code between the two versions operates the same. Nothing changes in the domain classes. Only the way results are gathered and returned between the database and application is adjusted.