The Basics of Being Reactive
One of the key problems with the whole Reactive space and why it’s so hard to understand is the vocabulary with all the terms and lots of different interpretations of what it means, Peter Ledbrook claims and explains that after attending talks and reading blog posts but still not getting the grip on what Reactive means he decided to work out what it’s all about hoping to share his knowledge with other developers.
Starting with the Reactive Manifesto Ledbrook, one of the core Grails committers, finds that a Reactive system is:
- Responsive, which means a system always should respond in a timely manner keeping the latency down.
- Resilient, meaning a system should be responsive even when parts of it starts failing, keeping downtime to a minimum.
- Elastic, meaning that with an increasing workload a system should scale up as needed to stay responsive, but also that with a decrease in load it should scale down, effectively having an efficient resource utilization.
- Message-driven, sending messages between different parts of a system, which Ledbrook claims is a prescriptive characteristic for a Reactive system.
The next concept Ledbrook takes on is Reactive streams describing it as a series of elements emitted over time and he sees the Observer pattern as a good start for understanding the concept. The pattern consists of a source emitting events and one or more observers waiting for events. For Ledbrook Reactive effectively is the Observer pattern plus an ability for the event source to notify about completion, an ability to propagate errors and finally an ability for listeners to talk to the source of the events.
Reactive streams is a specification and a common cross-platform implementation is ReactiveX. Two other implementations Ledbrook mentions are ProjectReactor and Akka. Three important concepts the Reactive Streams API is built on are:
- Publisher who is sending events and to which it’s possible to subscribe.
- Subscriber who subscribes to events
- Subscription that ties a subscriber to a publisher. It is the subscription that makes it possible for a subscriber to signal to the publisher.
Ledbrook concludes by noting that much of Reactive streams is made up of familiar concepts but hiding at least some complexity. Use cases are contexts with high I/O or where there is a need for processing a lot of data. Java 8 streams are similar to Reactive streams but he notes that they are pull only, lack the possibility for signaling from listeners and not really geared towards I/O as Reactive streams are.