Page 1
An introduction to reactive applications, Reactive Streams, and
options for the JVMSteve Pember
CTO, ThirdChannel
Software Architecture Con, 2016
THIRDCHANNEL @svpember
Page 2
“Reactive Streams”, “Reactive Extensions”, or “Rx”
Page 3
THIRDCHANNEL @svpember
Agenda• The Problem
• What are Reactive Streams?
• Rx in depth
• An Overview of JVM options
• Demo Time!
Page 4
THIRDCHANNEL @svpember
The Problem: The Need to go Reactive
Page 5
Really, it’s Two problems
Page 6
THIRDCHANNEL @svpember
1) Performance Demands Are Always Increasing
Page 8
We Use Technology from the Beginning of Web Development
Page 11
Users get angry quickly
Page 12
–Johnny Appleseed
“Type a quote here.”
Page 13
Let’s Keep Our Users Happy And Engaged
Page 14
THIRDCHANNEL @svpember
2) The Rise of Microservices
Page 15
Multiple Integration Points
Page 16
It’s Not Only Users That Use Up Resources
Page 18
Embrace Reactive Applications
Page 19
THIRDCHANNEL @svpember
Page 20
THIRDCHANNEL @svpember
Reactive Applications• Responsive
Page 21
THIRDCHANNEL @svpember
Page 22
THIRDCHANNEL @svpember
Reactive Applications• Responsive
• Resilient
Page 26
Independent Things Fail Independently
Page 27
THIRDCHANNEL @svpember
Reactive Applications• Responsive
• Resilient
• Elastic (Scalable)
Page 29
THIRDCHANNEL @svpember
Reactive Applications• Responsive
• Resilient
• Elastic (Scalable)
• Asynchronous / Message Driven
Page 30
Free up resources with Async Operations & Non-Blocking I/O
Page 31
Async is Hard for Humans
Page 32
One Excellent Tool is (are?) Reactive Streams
Page 33
THIRDCHANNEL @svpember
Agenda• The Problem
• What are Reactive Streams?
Page 34
Collections + Time
Page 36
Single abstraction over data from many sources
Page 37
THIRDCHANNEL @svpember
Page 38
Observer Pattern Push (not Pull) based Iterators
Page 39
Stream-Based Functional Programming
Page 40
Imperative vs Stream
Page 41
Streams with Extensions for Reactive Programming
Page 42
Rx makes Async behavior easy!
Page 43
(Reactive Pull) Backpressure
Page 45
THIRDCHANNEL @svpember
What is Rx?• Collections + Time
• A Single Abstraction over data from different sources
• Observer Pattern with Push-based iterators
• Stream Based Functional Programming
• … with Extensions for Reactive Programming
• Async is easy
• Backpressure
Page 46
Rx Simplifies Complex Work
Page 47
…Once you understand, of course…
Page 48
THIRDCHANNEL @svpember
Agenda• The Problem
• What are Reactive Streams?
• Rx in depth
Page 49
THIRDCHANNEL @svpember
Key Terms:
Page 50
An Observable is like Promise ++
Page 51
An Observable pushes items to Subscribers
Page 52
Subscribers receive and operate on emitted data
Page 53
Observables and Subscribers operate on a Scheduler
Page 54
The following examples use rxJava
Page 55
But, try out rxGroovy
Page 56
THIRDCHANNEL @svpember
Groovy• Dynamic Language for the JVM
• Less Verbose (Reduce Java Boilerplate)
• Ruby/Python - esque Collections
• Run Time Meta Programming
• Optionally Typed
• AST Transformations
• Powerful Annotations
• Multi - Inheritance via Traits and @DelegatesTo
Page 57
THIRDCHANNEL @svpember
Page 58
THIRDCHANNEL @svpember
Page 59
THIRDCHANNEL @svpember
Basic Usage
Page 60
THIRDCHANNEL @svpember
Page 61
THIRDCHANNEL @svpember
Page 62
Thankfully, there are shortcuts
Page 63
THIRDCHANNEL @svpember
Page 64
THIRDCHANNEL @svpember
Page 65
Streams are Composable
Page 66
THIRDCHANNEL @svpember
Page 67
THIRDCHANNEL @svpember
You can get much power from 5 functions• filter
• map
• reduce
• groupBy
• flatMap
Page 68
THIRDCHANNEL @svpember
Page 69
THIRDCHANNEL @svpember
Page 70
THIRDCHANNEL @svpember
Page 71
THIRDCHANNEL @svpember
Page 72
First Mental Leap: An Observable of Observables
Page 73
–Johnny Appleseed
“Type a quote here.”
Page 74
THIRDCHANNEL @svpember
Page 75
THIRDCHANNEL @svpember
Page 76
THIRDCHANNEL @svpember
Hot vs Cold
Page 77
Cold Observable: finite data, on demand Hot Observable: infinite data, as it’s ready
Page 78
Cold Observable: only starts emitting data on .subscribe () Hot Observable: emits data whenever it’s ready
Page 79
THIRDCHANNEL @svpember
Asynchronous Streams
Page 80
THIRDCHANNEL @svpember
Page 81
THIRDCHANNEL @svpember
Page 82
THIRDCHANNEL @svpember
Page 83
THIRDCHANNEL @svpember
BackPressure
Page 84
THIRDCHANNEL @svpember
Page 85
THIRDCHANNEL @svpember
Can only Mitigate Hot Streams• throttle
• sample
• window
• buffer
• drop
Page 86
THIRDCHANNEL @svpember
Stream Interaction
Page 87
Don’t Unsubscribe from Observables
Programmatically complete them when another Observable fires
Page 89
THIRDCHANNEL @svpember
AutoComplete Requirements• Wait 250 ms between keypresses before querying
• If no keys are pressed, no query
• Successful queries should render movies
• Any new queries should kill in-flight queries
Page 91
Pretty Great, But what’s going on?
Page 92
The keyPress stream is reacting to itself
Page 93
THIRDCHANNEL @svpember
Page 94
THIRDCHANNEL @svpember
Questions?
Page 95
THIRDCHANNEL @svpember
Agenda• The Problem
• What are Reactive Streams?
• Rx In Depth
• An Overview of JVM options
Page 96
THIRDCHANNEL @svpember
Story Time
Page 97
THIRDCHANNEL @svpember
Story Time
Page 98
THIRDCHANNEL @svpember
Story Time
Page 99
THIRDCHANNEL @svpember
Story Time
2012 - MS Open Source’s RX!
Page 100
THIRDCHANNEL @svpember
Story Time
2012 - MS Open Source’s RX!
Page 101
THIRDCHANNEL @svpember
Page 102
THIRDCHANNEL @svpember
Page 103
THIRDCHANNEL @svpember
We should probably start with RxJava
Page 104
Brings Reactive Streams to the JVM
Page 105
But Don’t Forget rxGroovy
Page 108
THIRDCHANNEL @svpember
• High performance web framework
• Non-opinionated
• Non-Blocking Network Stack
• Built on Reactive Streams, Netty, Java 8, Guice
• Fully embodies reactive
• Light-weight, self-contained deployables
Take a Look at Ratpack
Page 109
http://www.infoq.com/presentations/ratpack-2015
Page 110
Includes rxRatpack module, but we’ll talk about that later
Page 111
THIRDCHANNEL @svpember
Akka & Akka Streams• Library
• Definition of Reactive System
• Created by LightBend
• Actor-Based Concurrency
• Implemented Streams on Top of Actor Model
Page 115
–Johnny Appleseed
“Type a quote here.”
Page 116
–Johnny Appleseed
“Type a quote here.”
Page 117
THIRDCHANNEL @svpember
• Library
• Reactive Streams
• Reactor Pattern
• Built on LMAX Ring Buffer / Disrupter
• Multiple libraries to extend Ring Buffer in multiple ways
Page 118
THIRDCHANNEL @svpember
Page 119
THIRDCHANNEL @svpember
Page 120
THIRDCHANNEL @svpember
Page 121
THIRDCHANNEL @svpember
Page 122
Many Tools Seem to be Moving Towards Reactive Streams
Page 123
THIRDCHANNEL @svpember
Demo Time
Page 124
THIRDCHANNEL @svpember
Any Questions?
Page 125
Thank You!
@svpember [email protected]
THIRDCHANNEL @svpember
Page 126
THIRDCHANNEL @svpember
More Information• Reactive Groovy & Ratpack Demo: https://github.com/spember/reactive-movie-demo • Jafar Husain: RxJS: https://www.youtube.com/watch?v=XRYN2xt11Ek • Reactive Streams Spec: http://www.reactive-streams.org/ • Reactive Manifesto: http://www.reactivemanifesto.org/ • Akka: http://akka.io/ • rxJava / ReactiveX libraries: https://github.com/ReactiveX • Ratpack: http://ratpack.io/ • Reactor: https://github.com/reactor/reactor • The Introduction to Reactive Programming you’ve been missing: https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 • Martin Fowler: Stream / Pipeline programming: http://martinfowler.com/articles/refactoring-pipelines.html • Or Just on Groovy (Groovy the Awesome Parts): http://www.slideshare.net/SpringCentral/groovy-the-awesome-parts • Ratpack Web Presentation: http://www.infoq.com/presentations/ratpack-2015 • Advanced RxJava Blog: http://akarnokd.blogspot.com/ • Martin Fowler LMAX breakdown: http://martinfowler.com/articles/lmax.html
Page 127
Images• Empty Pool: http://www.wtok.com/home/headlines/Water-Problems-205987121.html
• Juggling: https://en.wikipedia.org/wiki/Juggling
• Directing Traffic: https://www.flickr.com/photos/tracilawson/3474012583L
• LMAX Disrupter: http://martinfowler.com/articles/lmax.html
• Mailman: thebrandtstandard.com/2013/02/09/u-s-post-office-to-end-saturday-letter-delivery-this-summer/
• Actors Diagram: https://blog.codecentric.de/en/2015/08/introduction-to-akka-actors/
• Cheetah: www.livescience.com/21944-usain-bolt-vs-cheetah-animal-olympics.html
• Dominoes: https://www.flickr.com/photos/louish/5611657857/sizes/l/in/photostream/
• Spartans: www.300themovie.com/
• Stampeding Buffalo: news.sd.gov/newsitem.aspx?id=15164