SlideShare a Scribd company logo
Christophe Marchal
Reactive programming with RxJava
christophe.marchal@ilegra.com
http://github.com/toff63
@toff63
http://francesbagual.net
About me
MOTIVATION
Scaling
Moore’s law is
delivering more cores
but not faster cores
Amdahl’s law
Speedup limited by
the sequential
portion of the code
In other words,
parallelize your
code to scale
I am not Netflix!
http://techblog.netflix.com/2016/09/zuul-2-netflix-journey-to-asynchronous.html
Multithreaded System Architecture
Efficient use of resources
● CPU efficient
● Memory efficient
● Hard Drive efficient
● Network efficient
Efficient use of threads
Non-Blocking
architecture
http://techblog.netflix.com/2016/09/zuul-2-netflix-journey-to-asynchronous.html
Asynchronous and Non-Blocking System Architecture
Challenges
● listeners / callback
● force functional code
● exception handling
● Everything becomes a
Stream
RxJava for the win!
Challenges
Erik Meijer
Origins
Reactive eXtension
Collection Future
Current vision
Observable: Stream of event
Reactive vision
Everything is an event
Observable: Stream of event
Observer
Reactive vision
Reactive vision
T getData()
One Item
synchronous
Reactive vision
Iterable<T> getData()T getData()
One Item Several Items
synchronous
Reactive vision
Iterable<T> getData()T getData()
Future<T> getData()
One Item Several Items
synchronous
Asynchronous
Reactive vision
Iterable<T> getData()
Observable<T> getData()
T getData()
Future<T> getData()
One Item Several Items
synchronous
Asynchronous
Pull vs Push
Iterable<T> getData() Observable<T> getData()
Pull
T next()
throw Exception()
returns;
Push
onNext()
onError()
onComplete()
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/basic/Application.java
Simple collection
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/restclient/Application.java
Calling remote API
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/restclient/Application.java
Calling remote API
Observable from Future
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/restclient/Application.java
Calling remote API
Exception handling
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/restclient/Application.java
Calling remote API
Fallback
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/composition/Service.java
Composing Observable
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/composition/Application.java#L17
Composing Observable
https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/flatMap.png
FlatMap
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/composition/Application.java#L34
Composing Observables
https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/zip.png
Zip
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/composition/Application.java#L24
Composing Observables
https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/merge.png
Merge
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/composition/SeriesHttpHandler.java#L22
RxNetty
Demo
Concurrency
Observable is sequential
Scheduling and Combining
Observables enable Concurrency
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/composition/SeriesHttpHandler.java
Adding concurrency
Scheduler
Demo
Cold Stream vs Hot Stream
Hot Stream Cold Stream
no control on emission rate emits when requested
UI events, Metric events,
System events
DB query, Service request,
Downloading file
Cold Stream vs Hot Stream
Hot Stream Cold Stream
no control on emission rate emits when requested
UI events, Metric events,
System events
DB query, Service request,
Downloading file
Flow control Flow control & Back pressure
Flow Control
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/flowcontrol/Backpressure.java
Backpressure needed
Synchronous on same thread
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/flowcontrol/Backpressure.java
No backpressure needed
Asynchronous (queuing)
Block Operator
Hot Stream Cold Stream
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/flowcontrol/Backpressure.java
toBlocking
Temporal Operators
Hot Stream
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/flowcontrol/Backpressure.java
Sample
98910
407719
1087903
1787798
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/flowcontrol/Backpressure.java
Throttle First
0
118117
1186307
1753066
2584575
Debounce
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/flowcontrol/Backpressure.java
Debounce
5
20
25
Buffer
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/flowcontrol/Backpressure.java
Buffer
[0, 1, 2, 3, 4, 5]
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[21, 22, 23, 24, 25]
Window time
https://github.com/toff63/Sandbox/blob/master/java/rsjug-rx/rsjug-rx/src/main/java/rs/jug/rx/flowcontrol/Backpressure.java
2854533
3541
675
424
446235
Window time
Reactive Stream
● Push when Consumer keeps up
● Pull when Consumer is slow
● Bound all queues
Maximize throughput
Reactive Stream: Consumer keeps up
Publisher Subscriber
As many as you can
Push
Reactive Stream: Consumer Starts buffering
Publisher Subscriber
As many as you can
Reactive Stream: Consumer Starts buffering
Publisher Subscriber
Give me 0
Give me 2
Pull
On backpressure buffer
On backpressure buffer Hot Stream
Hot Stream
On backpressure buffer
Scheduler
Hot Stream
On backpressure Drop
On backpressure buffer Hot Stream
And lots and lots
of other operators
Rx Ports
● Observable API is complex
● Takes time to become fluent with Observable
● Hard to test !
● Debugging is harder as everything is asynchronous
● Stacktraces can be truncated due to scheduler
Drawbacks
Stacktrace example
18:42:59.487 [rx-request-processor-5-67] ERROR
n.k.t.util.HttpContentInputStream - Error on server
io.netty.util.IllegalReferenceCountException: refCnt: 0
at io.netty.buffer.AbstractByteBuf.ensureAccessible(AbstractByteBuf.java:1178) ~[netty-buffer-4.0.27.Final.jar:4.0.27.Final]
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:848) ~[netty-buffer-4.0.27.Final.jar:4.0.27.Final]
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:841) ~[netty-buffer-4.0.27.Final.jar:4.0.27.Final]
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:831) ~[netty-buffer-4.0.27.Final.jar:4.0.27.Final]
at netflix.karyon.transport.util.HttpContentInputStream$1.onNext(HttpContentInputStream.java:67) [karyon2-governator-2.3.0.jar:2.3.0]
at netflix.karyon.transport.util.HttpContentInputStream$1.onNext(HttpContentInputStream.java:33) [karyon2-governator-2.3.0.jar:2.3.0]
at rx.Observable$33.onNext(Observable.java:7480) [rxjava-1.0.10.jar:1.0.10]
at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:130) [rxjava-1.0.10.jar:1.0.10]
at io.reactivex.netty.protocol.http.UnicastContentSubject$AutoReleaseByteBufOperator$1.onNext(UnicastContentSubject.java:262) [rxnetty-0.4.9.jar:0.4.9]
at rx.internal.operators.NotificationLite.accept(NotificationLite.java:150) [rxjava-1.0.10.jar:1.0.10]
at rx.internal.operators.BufferUntilSubscriber.emit(BufferUntilSubscriber.java:151) [rxjava-1.0.10.jar:1.0.10]
at rx.internal.operators.BufferUntilSubscriber.onNext(BufferUntilSubscriber.java:184) [rxjava-1.0.10.jar:1.0.10]
at io.reactivex.netty.protocol.http.UnicastContentSubject.onNext(UnicastContentSubject.java:286) [rxnetty-0.4.9.jar:0.4.9]
at io.reactivex.netty.protocol.http.server.ServerRequestResponseConverter.invokeContentOnNext(ServerRequestResponseConverter.java:193)
[rxnetty-0.4.9.jar:0.4.9]
at io.reactivex.netty.protocol.http.server.ServerRequestResponseConverter.channelRead(ServerRequestResponseConverter.java:129) [rxnetty-0.4.9.jar:0.4.9]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:339) [netty-transport-4.0.27.Final.jar:4.0.27.Final]
at io.netty.channel.AbstractChannelHandlerContext.access$600(AbstractChannelHandlerContext.java:32) [netty-transport-4.0.27.Final.jar:4.0.27.Final]
at io.netty.channel.AbstractChannelHandlerContext$7.run(AbstractChannelHandlerContext.java:329) [netty-transport-4.0.27.Final.jar:4.0.27.Final]
at io.netty.util.concurrent.DefaultEventExecutor.run(DefaultEventExecutor.java:36) [netty-common-4.0.27.Final.jar:4.0.27.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111) [netty-common-4.0.27.Final.jar:4.0.27.Final]
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) [netty-common-4.0.27.Final.jar:4.0.27.Final]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_73]
Netflix experience migrating zuul
http://techblog.netflix.com/2016/09/zuul-2-netflix-journey-to-asynchronous.html
References
● https://www.infoq.com/presentations/rx-service-archite
cture
● https://www.infoq.com/presentations/rxjava-reactor
● http://reactivex.io/tutorials.html
● http://reactivex.io/documentation/operators.html
Christophe Marchal
Thank you !
Questions?

More Related Content

PDF
RxJava - introduction & design
PPTX
Intro to Functional Programming with RxJava
PDF
Reactive programming using rx java & akka actors - pdx-scala - june 2014
PPTX
Introduction to Reactive Java
PDF
Streams, Streams Everywhere! An Introduction to Rx
PDF
rx-java-presentation
PPTX
Reactive Java (GeeCON 2014)
PDF
Reactive programming with RxJava
RxJava - introduction & design
Intro to Functional Programming with RxJava
Reactive programming using rx java & akka actors - pdx-scala - june 2014
Introduction to Reactive Java
Streams, Streams Everywhere! An Introduction to Rx
rx-java-presentation
Reactive Java (GeeCON 2014)
Reactive programming with RxJava

What's hot (20)

PDF
Building Scalable Stateless Applications with RxJava
PPTX
Reactive Programming in Java 8 with Rx-Java
PPT
Reactive programming with examples
PDF
Rxjava 介紹與 Android 中的 RxJava
PDF
Modern app programming with RxJava and Eclipse Vert.x
PPTX
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
PPTX
Javantura v3 - ES6 – Future Is Now – Nenad Pečanac
PDF
RxJava 2.0 介紹
PDF
Reactive programming in Angular 2
PDF
Journey into Reactive Streams and Akka Streams
PDF
Reactive Streams: Handling Data-Flow the Reactive Way
PDF
Reactive Programming with Rx
PDF
Gatling @ Scala.Io 2013
PPTX
Distributed Tests on Pulsar with Fallout - Pulsar Summit NA 2021
PPTX
Async and Await on the Server
PPTX
CTU June 2011 - C# 5.0 - ASYNC & Await
PDF
Performance measurement methodology — Maksym Pugach | Elixir Evening Club 3
PPTX
Java 7 & 8
PPTX
Async in .NET
PDF
Tempest scenariotests 20140512
Building Scalable Stateless Applications with RxJava
Reactive Programming in Java 8 with Rx-Java
Reactive programming with examples
Rxjava 介紹與 Android 中的 RxJava
Modern app programming with RxJava and Eclipse Vert.x
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
Javantura v3 - ES6 – Future Is Now – Nenad Pečanac
RxJava 2.0 介紹
Reactive programming in Angular 2
Journey into Reactive Streams and Akka Streams
Reactive Streams: Handling Data-Flow the Reactive Way
Reactive Programming with Rx
Gatling @ Scala.Io 2013
Distributed Tests on Pulsar with Fallout - Pulsar Summit NA 2021
Async and Await on the Server
CTU June 2011 - C# 5.0 - ASYNC & Await
Performance measurement methodology — Maksym Pugach | Elixir Evening Club 3
Java 7 & 8
Async in .NET
Tempest scenariotests 20140512
Ad

Viewers also liked (20)

PDF
Reactive Programming for a demanding world: building event-driven and respons...
PPTX
RxJS and Reactive Programming - Modern Web UI - May 2015
PDF
Modern Java Web
PDF
RxJava in practice
PDF
Introduction To Functional Reactive Programming Poznan
PPTX
Reactive programming
PDF
Reactive Stream Processing Using DDS and Rx
PDF
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
PPTX
Microservices architecture ext
PDF
RxJava on Android
PDF
BED-Con 2016 - I have a stream - Einsichten in Reactive Programming
PDF
Introduction to Retrofit and RxJava
PPTX
Scaling wix with microservices architecture devoxx London 2015
PDF
Reactive java - Reactive Programming + RxJava
PDF
Functional Reactive Programming in the Netflix API
PPTX
Vagrant to-aws-flow
PDF
Microservices: Architecture to scale Agile
PDF
Building and deploying microservices with event sourcing, CQRS and Docker (Ha...
PDF
From Mainframe to Microservice: An Introduction to Distributed Systems
PPT
Java 8 Streams
Reactive Programming for a demanding world: building event-driven and respons...
RxJS and Reactive Programming - Modern Web UI - May 2015
Modern Java Web
RxJava in practice
Introduction To Functional Reactive Programming Poznan
Reactive programming
Reactive Stream Processing Using DDS and Rx
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
Microservices architecture ext
RxJava on Android
BED-Con 2016 - I have a stream - Einsichten in Reactive Programming
Introduction to Retrofit and RxJava
Scaling wix with microservices architecture devoxx London 2015
Reactive java - Reactive Programming + RxJava
Functional Reactive Programming in the Netflix API
Vagrant to-aws-flow
Microservices: Architecture to scale Agile
Building and deploying microservices with event sourcing, CQRS and Docker (Ha...
From Mainframe to Microservice: An Introduction to Distributed Systems
Java 8 Streams
Ad

Similar to Reactive programming with Rxjava (20)

PDF
Reactive systems
PDF
Spring 5 Project Reactor
PDF
Reactive Applications in Java
PDF
Not Only Streams for Akademia JLabs
PDF
Spring Framework 5.0による Reactive Web Application #JavaDayTokyo
PDF
Reactor, Reactive streams and MicroServices
PDF
An introduction to Reactive applications, Reactive Streams, and options for t...
PDF
Springone2gx 2014 Reactive Streams and Reactor
PDF
Reactive Thinking in Java with RxJava2
PDF
Reactive Streams and RxJava2
PPTX
Reactive programming intro
PDF
What are the benefits of reactive programming in java
PDF
Reactive mesh
PDF
Atmosphere Conference 2015: Need for Async: In pursuit of scalable internet-s...
PPTX
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
PDF
Intro To Reactive Programming
PDF
Intro to Reactive Programming
PDF
An Introduction to Reactive Application, Reactive Streams, and options for JVM
PPTX
From Streams to Reactive Streams
PDF
Reactive Programming in Java and Spring Framework 5
Reactive systems
Spring 5 Project Reactor
Reactive Applications in Java
Not Only Streams for Akademia JLabs
Spring Framework 5.0による Reactive Web Application #JavaDayTokyo
Reactor, Reactive streams and MicroServices
An introduction to Reactive applications, Reactive Streams, and options for t...
Springone2gx 2014 Reactive Streams and Reactor
Reactive Thinking in Java with RxJava2
Reactive Streams and RxJava2
Reactive programming intro
What are the benefits of reactive programming in java
Reactive mesh
Atmosphere Conference 2015: Need for Async: In pursuit of scalable internet-s...
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
Intro To Reactive Programming
Intro to Reactive Programming
An Introduction to Reactive Application, Reactive Streams, and options for JVM
From Streams to Reactive Streams
Reactive Programming in Java and Spring Framework 5

More from Christophe Marchal (20)

PDF
Elasticsearch avoiding hotspots
PDF
Performance
PDF
PDF
Elasticsearch cluster deep dive
PDF
Elasticsearch
PDF
PDF
Consul in 5 minutes
PDF
Spark in 15 min
PDF
Microservices Architecture: Nirvana or Nightmare
PDF
Capistrano
PDF
Aws, play! couch db scaling soa in the cloud
PDF
Devops e a nova cultura - TDC Florianopolis 2015
PDF
Devops and the New Culture
PDF
Monads in practice
PDF
Productivity and scalability with Play and Scala
PDF
Reactive application
PDF
Internet of things and arduino
PDF
Integration with hdfs using WebDFS and NFS
Elasticsearch avoiding hotspots
Performance
Elasticsearch cluster deep dive
Elasticsearch
Consul in 5 minutes
Spark in 15 min
Microservices Architecture: Nirvana or Nightmare
Capistrano
Aws, play! couch db scaling soa in the cloud
Devops e a nova cultura - TDC Florianopolis 2015
Devops and the New Culture
Monads in practice
Productivity and scalability with Play and Scala
Reactive application
Internet of things and arduino
Integration with hdfs using WebDFS and NFS

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Electronic commerce courselecture one. Pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
Teaching material agriculture food technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
A Presentation on Artificial Intelligence
PPTX
Machine Learning_overview_presentation.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Unlocking AI with Model Context Protocol (MCP)
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Electronic commerce courselecture one. Pdf
The AUB Centre for AI in Media Proposal.docx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Review of recent advances in non-invasive hemoglobin estimation
Digital-Transformation-Roadmap-for-Companies.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MYSQL Presentation for SQL database connectivity
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Teaching material agriculture food technology
Programs and apps: productivity, graphics, security and other tools
A Presentation on Artificial Intelligence
Machine Learning_overview_presentation.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

Reactive programming with Rxjava