RX JAVA
MATEUSZ BUKOWICZ
REACTIVEX
REACTIVE SUPPORT IN DIFFERENT LANGUAGES
REACTIVE EXTENSIONS
▸ Rx.Net
▸ RxJava
▸ RxJS
▸ RxScala
▸ RxClojure
▸ RxCpp
▸ Rx.rb
▸ RxPY
▸ RxGroovy
▸ RxJRuby
▸ RxKotlin
▸ RxSwift
HOW IT BEGAN
RX.NET
▸ Rx.NET released in 2009
▸ Shipped with .NET 4.0 (2010)
AN API FOR ASYNCHRONOUS
PROGRAMMING WITH
OBSERVABLE STREAMS.
reactivex.io
DEFINITION
OBSERVER AGAIN
OBSERVER PATTERN
https://en.wikipedia.org/wiki/Observer_pattern
Observable
WHAT IS REACTIVE?
REACTIVE MANIFESTO
▸ responsive
▸ resilient
▸ elastic
▸ message-driven
REACTIVE CAN BE REPULSIVE
REACTIVE MANIFESTO CONTROVERSY
REACTIVE AS A LIFESTYLE
REACTIVE PROGRAMMING
OLD WAY OF DOING THINGS
PROCEDURAL WAY
FROM PROCEDURAL TO FUNCTIONAL
FUNCTIONAL WAY
FROM FUNCTIONAL TO REACTIVE
REACTIVE WAY
HOW TO OBSERVE?
ITERABLE VS OBSERVABLE
http://reactivex.io/intro.html
OBSERVER OBSERVES
OBSERVER USAGE
OPERATORS - FILTER
FILTER
http://rxmarbles.com/#filter
OPERATORS - TAKE
TAKE
http://rxmarbles.com/#take
OPERATORS - ZIP
ZIP
http://rxmarbles.com/#zip
WHICH OPERATORS SHOULD I USE?
OPERATORS DECISION TREE
http://reactivex.io/documentation/operators.html#tree
NETFLIX OLD WAY - PROBLEM
http://techblog.netflix.com/2012/07/embracing-differences-inside-netflix.html
NETFLIX OLD WAY - PROBLEM
http://techblog.netflix.com/2012/07/embracing-differences-inside-netflix.html
NETFLIX OLD WAY - PROBLEM
http://techblog.netflix.com/2013/01/optimizing-netflix-api.html
NETFLIX NEW WAY - SOLUTION
http://techblog.netflix.com/2013/01/optimizing-netflix-api.html
THINK ABOUT THE FUTURE
single item multiple items
sync T getData() Iterable<T> getData()
async Future<T> getData() ?
List<Future<T>> getData()
A NEW GUY COMES IN
RX.NET IS SO COOL
OBSERVABLE TO THE RESCUE
single item multiple items
sync T getData() Iterable<T> getData()
async Future<T> getData()
Observable<T>
getData()
NETFLIX NEW WAY - SOLUTION
http://techblog.netflix.com/2012/07/embracing-differences-inside-netflix.html
NETFLIX NEW WAY - SOLUTION
http://techblog.netflix.com/2012/07/embracing-differences-inside-netflix.html
http://techblog.netflix.com/2013/02/rxjava-netflix-api.html
PARALLEL WORK
CONCURRENCY
▸ most operators - synchronous by default
▸ some operators - implicit concurrency
▸ programmatic concurrency: observeOn/subscribeOn
http://reactivex.io/documentation/scheduler.html
can be anywhere
RX.JAVA SCHEDULERS
SCHEDULERS
http://reactivex.io/documentation/scheduler.html
SCHEDULERS IN ACTION
REACTIVE ANDROID
RX ANDROID
ERROR HANDLING
ON ERROR RESUME NEXT
http://reactivex.io/documentation/operators/catch.html
OH, MY BACK!
BACK PRESSURE
BACKPRESSURE-RELATED OPERATORS
BUFFER OPERATOR
https://github.com/ReactiveX/RxJava/wiki/Backpressure
BACK PRESSURE
ON BACKPRESSURE BUFFER
https://github.com/ReactiveX/RxJava/wiki/Backpressure
MORE RESILIENT RX.JAVA
HYSTRIX + RX.JAVA
https://github.com/Netflix/Hystrix/wiki/How-To-Use#Reactive-Execution
CIRCUIT BREAKER
CLOSED
OPENHALF-OPEN
failure threshold
reached
timeout timer
expired
operation failed
success count
threshold reached
always
return
failure
increment failure
counter on failure
increment
success
counter on
success
start
REACTIVE NETTY
RX NETTY
https://github.com/Netflix-Skunkworks/WSPerfLab/blob/master/test-results/RxNetty_vs_Tomcat_April2015.pdf
CPU saturated
clients
req/s avg lat [ms]
RX NETTY VS TOMCAT
WHY RX NETTY IS BETTER?
▸ lower CPU consumption
▸ lower object allocation rate/less GC
▸ event loop architecture vs thread pool architecture
▸ no thread pool locks
▸ less thread migrations
▸ more CPU Instructions Per Cycle (IPC)
https://github.com/Netflix-Skunkworks/WSPerfLab/blob/master/test-results/RxNetty_vs_Tomcat_April2015.pdf
REACTIVE STREAMS - INITIATIVE
http://www.reactive-streams.org
REACTIVE STREAMS - SCOPE
http://www.reactive-streams.org
FUTURE IS REACTIVE
JAVA 9 FLOW API
▸ JDK 9.0
▸ RxJava 2.x -> Reactive Streams implementation
▸ JPA -> Hibernate
▸ Reactive Streams -> RxJava 2.x
THE END

rx-java-presentation