an introduction to reactive applications, reactive streams, and options for the jvm (sacon sf 2016)

158
An introduction to reactive applications, Reactive Streams, and options for the JVM Steve Pember CTO, ThirdChannel Software Architecture Con, 2016 THIRDCHANNEL @svpember

Upload: steve-pember

Post on 21-Jan-2017

252 views

Category:

Software


3 download

TRANSCRIPT

An introduction to reactive applications, Reactive Streams, and

options for the JVMSteve Pember

CTO, ThirdChannel

Software Architecture Con, 2016

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Agenda• The Problem

• To Be Reactive

• What are Reactive Streams?

• Rx in Depth

• An Overview of JVM Options

• Demo Time!

THIRDCHANNEL @svpember

The Problem: The Need to go Reactive

Really, it’s Two problems

THIRDCHANNEL @svpember

1) Performance Demands Are Always Increasing

We Use Technology from the Beginning of Web Development

Things Slow Down

Users get angry quickly

–Johnny Appleseed

“Type a quote here.”

Let’s Keep Our Users Happy And Engaged

THIRDCHANNEL @svpember

2) The Rise of Microservices

Multiple Integration Points

It’s Not Only Users That Use Up Resources

So… what to do?

Embrace Reactive Applications

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Reactive Applications• Responsive

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Reactive Applications• Responsive

• Resilient

Embrace Failure

Independent Things Fail Independently

THIRDCHANNEL @svpember

Reactive Applications• Responsive

• Resilient

• Elastic (Scalable)

THIRDCHANNEL @svpember

Reactive Applications• Responsive

• Resilient

• Elastic (Scalable)

• Asynchronous / Message Driven

Free up resources with Async Operations & Non-Blocking I/O

Now do all of this at every level of your app…

All the Way Down

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

• User Interface: What the user interacts with (JS, iOS, Android)

• Inter-Service: Distributed application topology + service communication design (e.g. Kafka for async messaging)

• Intra-Service: The code! (well designed with bounded contexts)

• Framework: Structures your code + provides external communication, DI, etc

• Execution Environment: Broad area, but includes how your code is executed (e.g. in JVM: Tomcat, Jetty, Netty)

THIRDCHANNEL @svpember

Mostly talk about this…

We’ll talk a little about this…

And a little more about this

There’s not enough time!

Writing Reactive Code is the best method of introduction…

Async is Hard for Humans

One Excellent Tool is (are?) Reactive Streams

But Wait…

THIRDCHANNEL @svpember

Agenda• The Problem

• What are Reactive Streams?

“Reactive Streams”, “Reactive Extensions”, or “Rx”

Collections + Time

Single abstraction over data from many sources

THIRDCHANNEL @svpember

Observer Pattern Push (not Pull) based Iterators

Stream-Based Functional Programming

Imperative vs Stream

Streams with Extensions for Reactive Programming

Rx makes Async behavior easy!

(Reactive Pull) Backpressure

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

Rx Simplifies Complex Work

…Once you understand, of course…

THIRDCHANNEL @svpember

Story Time!

THIRDCHANNEL @svpember

Story Time

THIRDCHANNEL @svpember

Story Time

THIRDCHANNEL @svpember

Story Time

THIRDCHANNEL @svpember

Story Time

2012 - MS Open Source’s RX!

THIRDCHANNEL @svpember

Story Time

2012 - MS Open Source’s RX!

THIRDCHANNEL @svpember

Agenda• The Problem

• What are Reactive Streams?

• Rx in depth

THIRDCHANNEL @svpember

Key Terms:

An Observable is like Promise ++

(Observable: aka ‘Publisher’)

Observables are most powerful when wrapping external input

An Observable pushes items to Subscribers

Subscribers receive and operate on emitted data

Observables and Subscribers operate on a Scheduler

The following examples use rxJava 1.x

Also, try out rxGroovy

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

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Basic Usage

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Thankfully, there are shortcuts

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Streams are Composable

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

You can get much power from 5 functions• filter

• map

• reduce

• groupBy

• flatMap

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

First Mental Leap: An Observable of Observables

–Johnny Appleseed

“Type a quote here.”

THIRDCHANNEL @svpember

Let’s get a little crazy

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Hot vs Cold

Cold Observable: finite data, on demand Hot Observable: infinite data, as it’s ready

Cold Observable: only starts emitting data on .subscribe () Hot Observable: emits data whenever it’s ready

THIRDCHANNEL @svpember

Asynchronous Streams

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

BackPressure

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Can only Mitigate Hot Streams• throttle

• sample

• window

• buffer

• drop

THIRDCHANNEL @svpember

Stream Interaction

Don’t Unsubscribe from Observables

Programmatically complete them when another Observable fires

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

Pretty Great, But what’s going on?

keyPress stream creates a sub- stream which is reacting to subsequent data from the parent

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Questions?

Ok, what about 2.0?

API hasn’t changed much

THIRDCHANNEL @svpember

RxJava 2.0• Completely rebuilt for Reactive-Streams.org 1.0 spec

• Decreased resource usage

• No nulls allowed

• New Publisher types

THIRDCHANNEL @svpember

Publishers• Observable - classic. In 2.0: no Backpressure

• Flowable - new to 2.0. Use this for Backpressure

• Single - only one item will be emitted or signal error

• Completable - only signal success or error

• Maybe - new to 2.0: one item emitted, signal success, or signal error (think, Promise)

THIRDCHANNEL @svpember

Agenda• The Problem

• What are Reactive Streams?

• Rx In Depth

• An Overview of JVM options

THIRDCHANNEL @svpember

Intra-Service Options

RxJava, Obviously

THIRDCHANNEL @svpember

ReactiveX JVM Family• rxJava

• rxJavaFX

• rxGroovy

• rxClojure

• rxKotlin

• rxScala

• And many more (beyond JVM): https://github.com/ReactiveX

THIRDCHANNEL @svpember

Akka & Akka Streams• Library

• Definition of Reactive System

• Created by LightBend

• Actor-Based Concurrency

• Implemented Streams on Top of Actor Model

–Johnny Appleseed

“Type a quote here.”

–Johnny Appleseed

“Type a quote here.”

THIRDCHANNEL @svpember

• Pivotal Project

• Library

• Reactive Streams

• Built on LMAX Ring Buffer / Disrupter

• Multiple libraries to extend Disruptor in multiple ways

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Java 9 will have Flow

THIRDCHANNEL @svpember

Framework Level

THIRDCHANNEL @svpember

• High Performance Web Framework

• Non-Opinionated

• Non-Blocking Network Stack

• Built on Reactive Streams, Netty, Java 8, Guice

• Deterministic Asynchronous Execution

Take a Look at Ratpack

http://www.infoq.com/presentations/ratpack-2015

Includes rxRatpack module, but we’ll talk about that later

… specifically, Spring 5

THIRDCHANNEL @svpember

Spring Web Reactive vs Spring Web MVC

THIRDCHANNEL @svpember

Spring 5• spring-web-reactive instead of spring-mvc

• Same @Controller programming model

• Different underlying API

• Based on Project Reactor

• Can run within Tomcat, Jetty, or Netty (e.g. can fallback to use servlets)

THIRDCHANNEL @svpember

Play Framework• Part of the Lightbend family

• Built on Akka and Netty

• Async I/O

• Lightweight and stateless

• Encourages RESTful design

• Focus on JSON

THIRDCHANNEL @svpember

Vert.X• Event-driven & Non blocking

• lightweight, non-opinionated, and modular

• integrates with rxJava

• support for additional languages (like JS and Ruby)

• Can be used as a library embedded in an existing app

THIRDCHANNEL @svpember

Demo Time

THIRDCHANNEL @svpember

Any Questions?

Thank You!

@svpember [email protected]

THIRDCHANNEL @svpember

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 • Reactive Web Apps with Spring 5: https://www.youtube.com/watch?v=rdgJ8fOxJhc&feature=youtu.be

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

• Turtles (Cosmic): http://synchronicity313.deviantart.com/art/Turtles-All-The-Way-Down-68160813

• XkCD turtles: https://xkcd.com/1416/

• Simpsons “Old Coot”: http://simpsons.wikia.com/wiki/Category:Grandparents

• Meditation: http://i.huffpost.com/gen/1405484/images/o-MEDITATION-facebook.jpg

• Death Star Architectures: http://www.slideshare.net/adriancockcroft/monitorama-please-no-more