RxJava

Presented by Emanuelle Garduño

RxJava


Functional Reactive Programming

Prepared by Emanuelle Garduño

Java Facts

  • Not a Functional Programming Language.
  • Lambdas (Java 8) are not Functions.
  • Method references (Java 8) are not Functions.
  • Closures or anonymous classes are not Functions.
  • It is possible to use a functional style of programming (Guava and others).

Functional Reactive Programming

Principles of functional programming:

  • Data In, Data Out
  • Immutability
  • Lazy Evaluation
  • Declarative Style
  • Strong Typing

What's a function?

Data In, Data Out. Always return the same output for the same input.

How to Functions in Java

In theory:

  • Don't have state (Instance Variables).
  • Don't access global state (Class Variables).
  • Don't modify the environment.

In practice:

  • The nature of OOP and the lack of Functions as first-class citizen makes it harder to apply.
  • Functions are not for every aspect of OOP.

Functional Reactive Programming

Reactive Manifesto

http://www.reactivemanifesto.org/

Responsive

The system responds in a timely manner if at all possible. Responsiveness is the cornerstone of usability and utility, but more than that, responsiveness means that problems may be detected quickly and dealt with effectively. Responsive systems focus on providing rapid and consistent response times, establishing reliable upper bounds so they deliver a consistent quality of service. This consistent behaviour in turn simplifies error handling, builds end user confidence, and encourages further interaction.

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. Resilience is achieved by replication, containment, isolation and delegation. Failures are contained within each component, isolating components from each other and thereby ensuring that parts of the system can fail and recover without compromising the system as a whole. Recovery of each component is delegated to another (external) component and high-availability is ensured by replication where necessary. The client of a component is not burdened with handling its failures.

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. This implies designs that have no contention points or central bottlenecks, resulting in the ability to shard or replicate components and distribute inputs among them. Reactive Systems support predictive, as well as Reactive, scaling algorithms by providing relevant live performance measures. They achieve elasticity in a cost-effective way on commodity hardware and software platforms.

Message Driven

Reactive Systems rely on asynchronous message-passing to establish a boundary between components that ensures loose coupling, isolation, location transparency, and provides the means to delegate errors as messages. Employing explicit message-passing enables load management, elasticity, and flow control by shaping and monitoring the message queues in the system and applying back-pressure when necessary. Location transparent messaging as a means of communication makes it possible for the management of failure to work with the same constructs and semantics across a cluster or within a single host. Non-blocking communication allows recipients to only consume resources while active, leading to less system overhead.

Functional Reactive Programming

RxJava

Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

It extends the observer pattern to support sequences of data/events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety and concurrent data structures.

https://github.com/ReactiveX/RxJava

RxJava

Observer Pattern

Hystrix

Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.

https://github.com/Netflix/Hystrix

Hystrix

Let's code something fun

Our task is to build a RESTful API on top of the Marvel API. This is the requirement from a Fake Game development company.

  1. Create an endpoint that accepts up to 4 Marvel Character names.
  2. The endpoint will return a JSON String with the following information: Character: id, name, description, thumbnail. Comic: title, price and thumbnail.
  3. The Marvel API client is already build.
  4. Has to be FAST.

http://marvel.com

Game preview

Questions?

Thank you!