Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor and Reactive Streams for MicroServices Stephane Maldini, Solutions Architect, Pivotal @smaldini
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 2 @smaldini - solve 9 issues, create 10 problems Customer Success Organization @ Pivotal Reactor Committer II Try to contribute to Reactive-Streams
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ NanoService, MicroService, NotTooBigService™… cat file.csv 3
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ NanoService, MicroService, NotTooBigService™… cat file.csv 3 | grep ‘doge’
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ NanoService, MicroService, NotTooBigService™… POST [json] http://dogecoin.money/send/ 4
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ NanoService, MicroService, NotTooBigService™… POST [json] http://dogecoin.money/send/ 4 —> GET [json] http://dogeprofile.money/id
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ NanoService, MicroService, NotTooBigService™… POST [json] http://dogecoin.money/send/ 4 —> GET [json] http://dogeprofile.money/id —> POST [json] http://nsa.gov.us/cc/trace/id
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ NanoService, MicroService, NotTooBigService™… § A SomethingService will always need to interact • With the user • With other services § The boundary between services is the real deal • A boundary is a separation in time and space 5
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Dealing with reality: Latency 6
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Latency 7 UberFact : Humans don’t really enjoy waiting
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Neither do The Machines 8 http://ferd.ca/queues-don-t-fix-overload.html
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What is latency doing to you ? § Loss of revenues • because users switched to another site/app • because services are compounding inefficiency • because aggressive scaling will be needed 9
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ All hail Reactive Programming § A possible answer to this issue § The very nature of Reactor, look at the name dude § A fancy buzz-word that might work better than MDA or SOA § A simple accumulation of years of engineering 11
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Seriously, what is Reactive Programming ? 12
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Seriously, what is Reactive Programming ? 12 Event-Driven
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Seriously, what is Reactive Programming ? 12 Fault-Tolerant Event-Driven
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Seriously, what is Reactive Programming ? 12 Fault-Tolerant Event-Driven Elastic
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Seriously, what is Reactive Programming ? 12 Low-latency* Fault-Tolerant Event-Driven Elastic
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Architecture ? § A Reactive system MUST be resilient • splitting concerns to achieve error bulk-heading and modularity § A Reactive system MUST be scalable • scale-up : partition work across CPUs • scale-out : distribute over peer nodes 13
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor has 99 problems but Latency isn’t one 14
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor-Core features 15 [A]synchronous Dispatchers
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor-Core features 15 Event Bus [A]synchronous Dispatchers
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor-Core features 15 Event Bus [A]synchronous Dispatchers Streams and Promises
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor-Core features 15 Functional artifacts (SAM components, tuples, timers) Event Bus [A]synchronous Dispatchers Streams and Promises
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor-Core features 15 Functional artifacts (SAM components, tuples, timers) Event Bus [A]synchronous Dispatchers Streams and Promises Fast IO [buffer, net, persistent queues, codec]
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor-Core features 15 Functional artifacts (SAM components, tuples, timers) Event Bus [A]synchronous Dispatchers Streams and Promises Fast IO [buffer, net, persistent queues, codec] Fast Data [allocators, batch- processors]
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Using a Stream ? 17 Embedded data-processing Event Processing Metrics, Statistics Micro-Batching Composition Error Handling
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Stream Oriented Services § [CODE] 18 Win this way class UserService{ User authenticateUser(long id) throws Exception{ User.findById(id) }
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Stream Oriented Services § [CODE] 18 Win this way class UserService{ User authenticateUser(long id) throws Exception{ User.findById(id) } class UserService{ Stream<User> authenticateUser(long id){ Streams.create{ it.onNext(User.findById(id)) it.oncComplete() } } }
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Defining a Stream § Represents a sequence of data, possibly unbounded § Provide for processing API such as filtering and enrichment § Not a Collection, not a Storage 19
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Stream VS Event Bus [Reactor] § Works great combined (stream distribution) § Type-checked flow § Publisher/Subscriber tight control § No Signal concurrency 20
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Stream VS Event Bus [Reactor] § Works great combined (stream distribution) § Type-checked flow § Publisher/Subscriber tight control § No Signal concurrency 20 Rule of thumb: if nested event composition > 2, switch to Stream
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Hot Stream vs Cold Stream § An Hot Stream multi-casts real-time signals • think Trade, Tick, Mouse Click, Websocket § A Cold Stream uni-casts deferred signals • think File, Array, Computation result (Future) 21
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Introducing Reactive Streams Specification ! 24
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What is defined by Reactive Streams ? 25
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What is defined by Reactive Streams ? 25 Async non-blocking data sequence
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What is defined by Reactive Streams ? 25 Async non-blocking data sequence Minimal resources requirement
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What is defined by Reactive Streams ? 25 Interoperable protocol (Threads, Nodes…) Async non-blocking data sequence Minimal resources requirement
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What is defined by Reactive Streams ? 25 Async non-blocking flow-control Interoperable protocol (Threads, Nodes…) Async non-blocking data sequence Minimal resources requirement
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive-Streams: Dynamic Message-Passing 26
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive-Streams: Dynamic Message-Passing 26 PUBLISHER
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive-Streams: Dynamic Message-Passing 26 PUBLISHER SUBSCRIBER
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive-Streams: Dynamic Message-Passing 26 PUBLISHER SUBSCRIBER
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive-Streams: Dynamic Message-Passing 26 PUBLISHER SUBSCRIBER Events
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive-Streams: Dynamic Message-Passing 26 PUBLISHER SUBSCRIBER Events Demand
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Now You Know § It is not only queue-based pattern: • Signaling demand on a slower Publisher == no buffering • Signaling demand on a faster Publisher == buffering § Data volume is bounded by a Subscriber • Scaling dynamically if required 27
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Out Of The Box : Flow Control 28
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Out Of The Box : Flow Control 28 PUBLISHER
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Out Of The Box : Flow Control 28 PUBLISHER SUBSCRIBER
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Out Of The Box : Flow Control 28 PUBLISHER SUBSCRIBER
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Out Of The Box : Flow Control 28 PUBLISHER SUBSCRIBER Slow SUBSCRIBER
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Out Of The Box : Flow Control 28 PUBLISHER SUBSCRIBER Slow SUBSCRIBER
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Out Of The Box : Flow Control 28 PUBLISHER SUBSCRIBER Slow SUBSCRIBER
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Out Of The Box : Flow Control 28 PUBLISHER SUBSCRIBER Slow SUBSCRIBER Fast
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor Data Driver Akka Distributed System RxJava Metrics Pipeline Ratpack HTTP server Reactive Streams: Joining forces 31
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor Data Driver Akka Distributed System RxJava Metrics Pipeline Ratpack HTTP server Reactive Streams: Joining forces 31
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor Data Driver Akka Distributed System RxJava Metrics Pipeline Ratpack HTTP server Reactive Streams: Joining forces 31
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor Data Driver Akka Distributed System RxJava Metrics Pipeline Ratpack HTTP server Reactive Streams: Joining forces 31
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor Data Driver Akka Distributed System RxJava Metrics Pipeline Ratpack HTTP server Reactive Streams: Joining forces 31
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor Data Driver Akka Distributed System RxJava Metrics Pipeline Ratpack HTTP server Reactive Streams: Joining forces 31
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor Data Driver Akka Distributed System RxJava Metrics Pipeline Ratpack HTTP server Reactive Streams: Joining forces 31
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Streams: An industrial matured spec § Semantics • Single document listing full rules • Open enough to allow for various patterns § 4 API Interfaces • Publisher, Subscriber, Subscription, Processor § TCK to verify implementation behavior 32
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Streams: org.reactivestreams 33 public interface Publisher<T> { public void subscribe(Subscriber<T> s); } public interface Subscriber<T> { public void onSubscribe(Subscription s); public void onNext(T t); public void onError(Throwable t); public void onComplete(); } public interface Subscription { public void request(int n); public void cancel(); }
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Streams: org.reactivestreams 33 public interface Publisher<T> { public void subscribe(Subscriber<T> s); } public interface Subscriber<T> { public void onSubscribe(Subscription s); public void onNext(T t); public void onError(Throwable t); public void onComplete(); } public interface Subscription { public void request(int n); public void cancel(); } public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {}
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 35 What about RxJava mate ! All those hipsters use it.
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor == RxJava § Reactor Streams 2.0 are inspired by Rx and RxJava • http://msdn.microsoft.com/en-gb/data/gg577609.aspx • Naming and behavior is mostly aligned with RxJava (just, flatMap, retry…) • Rx Patterns should apply to Reactor Streams • Lightweight, embeddable 36
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor != RxJava § Reactor Streams mission: • Data throughput over Functional facilities (Dispatchers, Subscription model, pre-allocation) • Pivotal integration (Spring.io, RabbitMQ, Redis, CloudFoundry, Gemfire…) • Native Reactive Streams, all Stream actions benefit from back pressure model and can talk to any implementation 37
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ slidesSubscription.request(18); //talkSubscription.cancel(); 38
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 39 Let’s finish with practical patterns to empower your Reactive Service
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Streams: Async Boundaries 41 httpRequestStream map(f) filter(p) consume(httpResponseConsumer) Resource #1 Resource #4 dispatchOn(dispatcher14) Non Blocking Stream until last callback dispatchOn(dispatcher1)
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ A Full Slide Just To Talk About flatMap() 43
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ A Full Slide Just To Talk About flatMap() 43 FlatMap Bucket Challenge ! Nominate 3 friends to explain flatMap()
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Another Slide Just To Talk About flatMap() 44
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Another Slide Just To Talk About flatMap() 44 flatMap() is nothing more than the functional alternative to RPC. Just a way to say “Ok bind this incoming data to this sub-flow and listen for the result, dude”.
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Another Slide Just To Talk About flatMap() 45
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Another Slide Just To Talk About flatMap() 45 Usually in Functional Reactive Programming, flatMap is often used for crossing an async boundary.
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Another Slide Just To Talk About flatMap() 46
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Another Slide Just To Talk About flatMap() 46 This applies to Reactor too, but ALL Reactor actions also have built-in backpressure support and dispatching facilities
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ DEMO [if time left > 1.mn] 52
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ReactiveStreams.onSubscribe(Resources) § www.reactive-streams.org § https://github.com/reactive-streams/reactive-streams § on maven central : 1.0.0.M3 • org.reactivestreams/reactive-streams • org.reactivestreams/reactive-streams-tck 55
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ReactiveStreams.onNext(Roadmap) § Discussed for inclusion in JDK § Close to release: 1.0.0.RC1 • Evaluating TCK before going 1.0 final • Need 3 fully passing implementations before going 1.0.0.RC1 • TCK improvements 56
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor.onSubscribe(Resources) § http://projectreactor.cfapps.io/ § https://github.com/reactor § Twitter: @projectReactor § Blog Post: https://spring.io/blog/2014/10/21/reactor-2-0-0-m1-released-with- reactive-streams-integration § on maven central : 2.0.0.M1, 2.0.0.BUILD-SNAPSHOT • org.projectreactor/reactor 57