Reactive APIs — A non imperative communication

Rafael Manzoni
Apr 25, 2018 · 6 min read

We have been accustomed for many years to developing with an imperative and deterministic computing model. We can see that imperative programming is a programming paradigm that describes computing through actions, statements, or commands that change the state of a program.

Imperative Programming Model

If we look at the previous example all the commands will be executed as actions and in the written order.

When we externalize these kind of commands to Input/Output (I/O) operations, such as a database access, these executions happen synchronously, that is, the program will wait for the query result.

Database Sync Call — Imperative Model
Sync / Blocking Model

Alternative to Imperative and Blocking

Async / Blocking

Thread management is not an efficient model for using computational resources. Not to mention that somehow you will have to block and wait for the response at some point.

Async / Nonblocking

This entire execution model happens around a continuous loop for functions that need to be executed, ie these functions go into standby mode when they execute an I/O operation and return to the loop when the I/O state was processed. It is worth mentioning that the event loop processes function one at a time.
This model became popular by the usage of Node.JS. So we can say that this model take I/O operations for granted to do asynchronous processing between all functions, but does not treat the information as an asynchronous data stream.

So, should ask ourselves: How can we achieve an asynchronous model that composes non-blocking data stream?

And the answer is: Reactive Programming!

Reactive — What it really means?

Reactive Manifesto — Core Principles

Response: The system responds in a timely manner if possible at all. Responsiveness is the cornerstone of usability and utility.

Elastic: The system stays responsive under varying workload. Reactive Systems can react to changes in the input rate by increasing or decreasing the resources allocated to service these inputs. The back-pressure strategy makes use of feedback among the parties involved in order to control the flow and elasticity of the data stream.

Message Driven: Reactive Systems rely on asynchronous message-passing to establish a boundary between components that ensures loose coupling, isolation and location transparency.

Resilient: The system stays responsive in the face of failure. This applies not only to highly-available, mission critical systems — any system that is not resilient will be unresponsive after a failure.

Reactive Programming Model

  • Non blocking
  • Asynchronous
  • Functional / Declarative
  • Data Streams

A stream is a sequence of ongoing events ordered in time. It can emit three different things: a value (of some type), an error, or a “completed” signal.

Observer Design Pattern

The listening to the stream is called subscribing. The functions we are defining are observers. The stream is the subject (or “observable”) being observed. This is precisely the Observer Design Pattern.

Publish Subscriber

The next example shows us how a click event data stream can be composed by the usage of declarative functional strategy:

Reactive API

  • Reactive APIs are: Web API Observer Pattern
  • The request is the subscription -> subscribe();
  • The server sends back the events as data stream -> onNext();
  • The end of the response -> onComplete() or onError();
  • This behavior is specified by the W3C as text event stream content type requisition named Server-Sent Events.
  • https://www.w3.org/TR/2009/WD-eventsource-20090421/

Server-Sent Events specification defines an API for opening an HTTP connection for receiving push notifications from a server in the form of DOM events. The API is designed such that it can be extended to work with other push notification schemes such as Push SMS.

Reactive API Communication

Project Reactor

  • Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API, Completable Future, Stream and Duration.
  • Reactor offers 2 reactive composable components: Flux [N] and Mono [0|1] extensively implementing Reactive Extensions.
  • Suited for Microservices Architecture, Reactor offers backpressure-ready network engines for HTTP (including Websockets), TCP and UDP. Reactive Encoding/Decoding is fully supported;
https://projectreactor.io/

Spring WebFlux

  • The Spring Framework uses Reactor internally for its own reactive support;
  • Spring Core 5 includes a new spring-webflux module that contains support for reactive HTTP and WebSocket clients as well as for reactive server web applications including REST, HTML browser, and WebSocket style interactions.
Spring WebFlux

WebFlux can run on Servlet containers with support for the Servlet 3.1 Non-Blocking IO API as well as on other async runtimes such as Netty and Undertow.

REST-style JSON and XML serialization and deserialization is supported on top as a Flux<Object>, and so is HTML view rendering and Server-Sent Events.

REST Reactive API Spring Controller

Reference

Our goal in Product Technology is to create the best solution to our customers’ needs by offering the most complete and innovative portfolio of financial products from financing to investing. We’re here to build a fully automated and intelligent digital platform and we’re looking for people who’ll join us on this link.

Creditas Tech

Our technologies, innovations, digital product management…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store