Slide 1

Slide 1 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 2

Slide 2 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 3

Slide 3 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 4

Slide 4 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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’

Slide 5

Slide 5 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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 | sort | grep ‘doge’

Slide 6

Slide 6 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 7

Slide 7 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 8

Slide 8 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 9

Slide 9 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 10

Slide 10 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Dealing with reality: Latency 6

Slide 11

Slide 11 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 12

Slide 12 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 13

Slide 13 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 14

Slide 14 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 10 Loading

Slide 15

Slide 15 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 10 Loading

Slide 16

Slide 16 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 17

Slide 17 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Seriously, what is Reactive Programming ? 12

Slide 18

Slide 18 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 19

Slide 19 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 20

Slide 20 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 21

Slide 21 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 22

Slide 22 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 23

Slide 23 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 24

Slide 24 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor-Core features 15

Slide 25

Slide 25 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 26

Slide 26 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 27

Slide 27 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 28

Slide 28 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 29

Slide 29 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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]

Slide 30

Slide 30 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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]

Slide 31

Slide 31 text

import reactor.rx.spec.Streams def stream = Streams.defer() stream.map{ name -> Tuple.of(name, 'so wow') }.map{ tuple -> Tuple.of(tuple.t1, "$tuple.t2, much sad") }.consume{ tuple -> println "bye bye ! $tuple.t2... $tuple.t1" } stream.broadcastNext('Doge') Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Using a Stream ? 16

Slide 32

Slide 32 text

import reactor.rx.spec.Streams def stream = Streams.defer() stream.map{ name -> Tuple.of(name, 'so wow') }.map{ tuple -> Tuple.of(tuple.t1, "$tuple.t2, much sad") }.consume{ tuple -> println "bye bye ! $tuple.t2... $tuple.t1" } stream.broadcastNext('Doge') Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Prepare a simple Stream Using a Stream ? 16

Slide 33

Slide 33 text

import reactor.rx.spec.Streams def stream = Streams.defer() stream.map{ name -> Tuple.of(name, 'so wow') }.map{ tuple -> Tuple.of(tuple.t1, "$tuple.t2, much sad") }.consume{ tuple -> println "bye bye ! $tuple.t2... $tuple.t1" } stream.broadcastNext('Doge') Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Prepare a simple Stream 1st step Using a Stream ? 16

Slide 34

Slide 34 text

import reactor.rx.spec.Streams def stream = Streams.defer() stream.map{ name -> Tuple.of(name, 'so wow') }.map{ tuple -> Tuple.of(tuple.t1, "$tuple.t2, much sad") }.consume{ tuple -> println "bye bye ! $tuple.t2... $tuple.t1" } stream.broadcastNext('Doge') Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Prepare a simple Stream 1st step 2nd step Using a Stream ? 16

Slide 35

Slide 35 text

import reactor.rx.spec.Streams def stream = Streams.defer() stream.map{ name -> Tuple.of(name, 'so wow') }.map{ tuple -> Tuple.of(tuple.t1, "$tuple.t2, much sad") }.consume{ tuple -> println "bye bye ! $tuple.t2... $tuple.t1" } stream.broadcastNext('Doge') Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Prepare a simple Stream 1st step 2nd step Terminal callback Using a Stream ? 16

Slide 36

Slide 36 text

import reactor.rx.spec.Streams def stream = Streams.defer() stream.map{ name -> Tuple.of(name, 'so wow') }.map{ tuple -> Tuple.of(tuple.t1, "$tuple.t2, much sad") }.consume{ tuple -> println "bye bye ! $tuple.t2... $tuple.t1" } stream.broadcastNext('Doge') Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Prepare a simple Stream 1st step 2nd step Terminal callback Send some data into the stream Using a Stream ? 16

Slide 37

Slide 37 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 38

Slide 38 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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) }

Slide 39

Slide 39 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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 authenticateUser(long id){ Streams.create{ it.onNext(User.findById(id)) it.oncComplete() } } }

Slide 40

Slide 40 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 41

Slide 41 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 42

Slide 42 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 43

Slide 43 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 44

Slide 44 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor : Iterable Cold Stream 22 Streams .just(1, 2, 3, 4, 5) .take(3) .consume(System.out::println);

Slide 45

Slide 45 text

LapinStreams .fromQueue(queueName) .dispatchOn(Environment.get()) .timeout(30) .consume(System.out::println); Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor : AMQP Hot Stream 23

Slide 46

Slide 46 text

LapinStreams .fromQueue(queueName) .dispatchOn(Environment.get()) .timeout(30) .consume(System.out::println); Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor : AMQP Hot Stream 23 COMING SOON IN A REPOSITORY NEAR YOU

Slide 47

Slide 47 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Introducing Reactive Streams Specification ! 24

Slide 48

Slide 48 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 49

Slide 49 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 50

Slide 50 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 51

Slide 51 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 52

Slide 52 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 53

Slide 53 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive-Streams: Dynamic Message-Passing 26

Slide 54

Slide 54 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 55

Slide 55 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 56

Slide 56 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 57

Slide 57 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 58

Slide 58 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 59

Slide 59 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 60

Slide 60 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 61

Slide 61 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 62

Slide 62 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 63

Slide 63 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 64

Slide 64 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 65

Slide 65 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 66

Slide 66 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 67

Slide 67 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 68

Slide 68 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Streams: Signals 29 onError | (onSubscribe onNext* (onError | onComplete)?)

Slide 69

Slide 69 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ccv Doug Lea – SUNY Oswego Reactive Streams: Joining forces 30

Slide 70

Slide 70 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 71

Slide 71 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 72

Slide 72 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 73

Slide 73 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 74

Slide 74 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 75

Slide 75 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 76

Slide 76 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 77

Slide 77 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 78

Slide 78 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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 { public void subscribe(Subscriber s); } public interface Subscriber { 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(); }

Slide 79

Slide 79 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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 { public void subscribe(Subscriber s); } public interface Subscriber { 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 extends Subscriber, Publisher {}

Slide 80

Slide 80 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor <=> Reactive Streams 34 Publisher reactor.rx.Stream Subscriber reactor.rx.actions.Action Processor reactor.rx.Promise

Slide 81

Slide 81 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor <=> Reactive Streams 34 Publisher reactor.rx.Stream Subscriber reactor.rx.actions.Action Processor reactor.rx.Promise

Slide 82

Slide 82 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor <=> Reactive Streams 34 Publisher reactor.rx.Stream Subscriber reactor.rx.actions.Action Processor reactor.rx.Promise

Slide 83

Slide 83 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor <=> Reactive Streams 34 Publisher reactor.rx.Stream Subscriber reactor.rx.actions.Action Processor reactor.rx.Promise

Slide 84

Slide 84 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor <=> Reactive Streams 34 Publisher reactor.rx.Stream Subscriber reactor.rx.actions.Action Processor reactor.rx.Promise

Slide 85

Slide 85 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor <=> Reactive Streams 34 Publisher reactor.rx.Stream Subscriber reactor.rx.actions.Action Processor reactor.rx.Promise

Slide 86

Slide 86 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 35

Slide 87

Slide 87 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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.

Slide 88

Slide 88 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 89

Slide 89 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 90

Slide 90 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ slidesSubscription.request(18); //talkSubscription.cancel(); 38

Slide 91

Slide 91 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 39

Slide 92

Slide 92 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 93

Slide 93 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Streams: Async Boundaries 40 httpRequestStream map(f) filter(p) consume(httpResponseConsumer) Resource #1 Resource #4 R2 R3 dispatchOn(dispatcher4) dispatchOn(dispatcher1) disp[..] disp[..]

Slide 94

Slide 94 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Streams: Async Boundaries 40 httpRequestStream map(f) filter(p) consume(httpResponseConsumer) Resource #1 Resource #4 R2 R3 dispatchOn(dispatcher4) Not that efficient dispatchOn(dispatcher1) disp[..] disp[..]

Slide 95

Slide 95 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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) dispatchOn(dispatcher1)

Slide 96

Slide 96 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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)

Slide 97

Slide 97 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Streams: Async Boundaries 42 httpRequestStream map(f) filter(p) consume(httpResponseConsumer) Resource #1 Resource #2 dispatchOn(dispatcher1) dispatchOn(dispatcher2)

Slide 98

Slide 98 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Streams: Async Boundaries 42 httpRequestStream map(f) filter(p) consume(httpResponseConsumer) Resource #1 Resource #2 81,2% OK, producer not blocked dispatchOn(dispatcher1) dispatchOn(dispatcher2)

Slide 99

Slide 99 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 100

Slide 100 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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()

Slide 101

Slide 101 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 102

Slide 102 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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”.

Slide 103

Slide 103 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 104

Slide 104 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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.

Slide 105

Slide 105 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 106

Slide 106 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 107

Slide 107 text

Streams.just('doge’).flatMap{ name -> Streams.just(name) .observe{ println 'so wow' } .map{ 'much monad'} }.consume{ assert it == 'much monad' } Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Before you miss it, a FlatMap example 47

Slide 108

Slide 108 text

Streams.just('doge’).flatMap{ name -> Streams.just(name) .observe{ println 'so wow' } .map{ 'much monad'} }.consume{ assert it == 'much monad' } Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Before you miss it, a FlatMap example 47 Feed a dynamic Sub-Stream with a name

Slide 109

Slide 109 text

Streams.just('doge’).flatMap{ name -> Streams.just(name) .observe{ println 'so wow' } .map{ 'much monad'} }.consume{ assert it == 'much monad' } Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Before you miss it, a FlatMap example 47 Feed a dynamic Sub-Stream with a name Sub-Stream definition

Slide 110

Slide 110 text

Streams.just('doge’).flatMap{ name -> Streams.just(name) .observe{ println 'so wow' } .map{ 'much monad'} }.consume{ assert it == 'much monad' } Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Before you miss it, a FlatMap example 47 Feed a dynamic Sub-Stream with a name Sub-Stream definition Sub-Stream result is merged back to the top-level Steam

Slide 111

Slide 111 text

deferred = Streams.defer(Environment.get()); deferred .partition(8).consume(stream -> stream .dispatchOn(cachedDispatcher()) .map(i -> i) .reduce(2, service::reducePairAsMap) .consume(service::forwardToOutput) ); Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Doing IO work (esp. O) : Cloning Pipeline 48

Slide 112

Slide 112 text

deferred = Streams.defer(Environment.get()); deferred .partition(8).consume(stream -> stream .dispatchOn(cachedDispatcher()) .map(i -> i) .reduce(2, service::reducePairAsMap) .consume(service::forwardToOutput) ); Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Doing IO work (esp. O) : Cloning Pipeline 48 Will create 8 clones competing for upstream data

Slide 113

Slide 113 text

deferred = Streams.defer(Environment.get()); deferred .partition(8).consume(stream -> stream .dispatchOn(cachedDispatcher()) .map(i -> i) .reduce(2, service::reducePairAsMap) .consume(service::forwardToOutput) ); Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Doing IO work (esp. O) : Cloning Pipeline 48 Will create 8 clones competing for upstream data Start consuming the full Stream until complete

Slide 114

Slide 114 text

Streams.range(1, Integer.MAX_VALUE) .requestOn(Environment.masterDispatcher()) .sample(2l, TimeUnit.SECONDS) .dispatchOn(Environment.cachedDispatcher()) .consume{ println it } Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Dealing with a nervous Publisher 49

Slide 115

Slide 115 text

Streams.range(1, Integer.MAX_VALUE) .requestOn(Environment.masterDispatcher()) .sample(2l, TimeUnit.SECONDS) .dispatchOn(Environment.cachedDispatcher()) .consume{ println it } Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Dealing with a nervous Publisher 49 An intense publisher

Slide 116

Slide 116 text

Streams.range(1, Integer.MAX_VALUE) .requestOn(Environment.masterDispatcher()) .sample(2l, TimeUnit.SECONDS) .dispatchOn(Environment.cachedDispatcher()) .consume{ println it } Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Dealing with a nervous Publisher 49 An intense publisher Assigned with a global dispatcher

Slide 117

Slide 117 text

Streams.range(1, Integer.MAX_VALUE) .requestOn(Environment.masterDispatcher()) .sample(2l, TimeUnit.SECONDS) .dispatchOn(Environment.cachedDispatcher()) .consume{ println it } Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Dealing with a nervous Publisher 49 An intense publisher Assigned with a global dispatcher Retaining a single value every 2s

Slide 118

Slide 118 text

Streams.range(1, Integer.MAX_VALUE) .requestOn(Environment.masterDispatcher()) .sample(2l, TimeUnit.SECONDS) .dispatchOn(Environment.cachedDispatcher()) .consume{ println it } Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Dealing with a nervous Publisher 49 An intense publisher Assigned with a global dispatcher Retaining a single value every 2s Dispatching the samples with a different dispatcher to avoid publisher contention

Slide 119

Slide 119 text

def closeCircuit = stream def openCircuit = fallback ?: Streams.fail(new Exception("service unavailable")) def circuitSwitcher = Streams.switchOnNext() stream .materialize() .window(maxSignals, maxTime) .flatMap{ s -> s.reduce(metrics){ tuple -> if(tuple.t1.isOnNext()) tuple.t2.incrementSuccess() else if(tuple.t1.isOnError()) tuple.t2.incrementFailure() } } .consume({ metrics -> if(metrics.isOpen()) { Streams.timer(metrics.retryTimeout).consume{ circuitSwitcher.onNext(closeCircuit) } circuitSwitcher.onNext(openCircuit) } else { circuitSwitcher.onNext(closeCircuit) } }, circuitSwitcher.&onError, circuitSwitcher.&onComplete) //start with close circuit circuitSwitcher.onNext(closeCircuit) circuitSwitcher Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Failproof: Implementing a Circuit Breaker 50

Slide 120

Slide 120 text

def closeCircuit = stream def openCircuit = fallback ?: Streams.fail(new Exception("service unavailable")) def circuitSwitcher = Streams.switchOnNext() stream .materialize() .window(maxSignals, maxTime) .flatMap{ s -> s.reduce(metrics){ tuple -> if(tuple.t1.isOnNext()) tuple.t2.incrementSuccess() else if(tuple.t1.isOnError()) tuple.t2.incrementFailure() } } .consume({ metrics -> if(metrics.isOpen()) { Streams.timer(metrics.retryTimeout).consume{ circuitSwitcher.onNext(closeCircuit) } circuitSwitcher.onNext(openCircuit) } else { circuitSwitcher.onNext(closeCircuit) } }, circuitSwitcher.&onError, circuitSwitcher.&onComplete) //start with close circuit circuitSwitcher.onNext(closeCircuit) circuitSwitcher TL;DR Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Failproof: Implementing a Circuit Breaker 50

Slide 121

Slide 121 text

def closeCircuit = stream def openCircuit = fallback ?: Streams.fail(new Exception("service unavailable")) def circuitSwitcher = Streams.switchOnNext() stream .materialize() .window(maxSignals, maxTime) .flatMap{ s -> s.reduce(metrics){ tuple -> if(tuple.t1.isOnNext()) tuple.t2.incrementSuccess() else if(tuple.t1.isOnError()) tuple.t2.incrementFailure() } } .consume({ metrics -> if(metrics.isOpen()) { Streams.timer(metrics.retryTimeout).consume{ circuitSwitcher.onNext(closeCircuit) } circuitSwitcher.onNext(openCircuit) } else { circuitSwitcher.onNext(closeCircuit) } }, circuitSwitcher.&onError, circuitSwitcher.&onComplete) //start with close circuit circuitSwitcher.onNext(closeCircuit) circuitSwitcher TL;DR Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Failproof: Implementing a Circuit Breaker 50 materialize

Slide 122

Slide 122 text

def closeCircuit = stream def openCircuit = fallback ?: Streams.fail(new Exception("service unavailable")) def circuitSwitcher = Streams.switchOnNext() stream .materialize() .window(maxSignals, maxTime) .flatMap{ s -> s.reduce(metrics){ tuple -> if(tuple.t1.isOnNext()) tuple.t2.incrementSuccess() else if(tuple.t1.isOnError()) tuple.t2.incrementFailure() } } .consume({ metrics -> if(metrics.isOpen()) { Streams.timer(metrics.retryTimeout).consume{ circuitSwitcher.onNext(closeCircuit) } circuitSwitcher.onNext(openCircuit) } else { circuitSwitcher.onNext(closeCircuit) } }, circuitSwitcher.&onError, circuitSwitcher.&onComplete) //start with close circuit circuitSwitcher.onNext(closeCircuit) circuitSwitcher TL;DR Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Failproof: Implementing a Circuit Breaker 50 materialize window

Slide 123

Slide 123 text

def closeCircuit = stream def openCircuit = fallback ?: Streams.fail(new Exception("service unavailable")) def circuitSwitcher = Streams.switchOnNext() stream .materialize() .window(maxSignals, maxTime) .flatMap{ s -> s.reduce(metrics){ tuple -> if(tuple.t1.isOnNext()) tuple.t2.incrementSuccess() else if(tuple.t1.isOnError()) tuple.t2.incrementFailure() } } .consume({ metrics -> if(metrics.isOpen()) { Streams.timer(metrics.retryTimeout).consume{ circuitSwitcher.onNext(closeCircuit) } circuitSwitcher.onNext(openCircuit) } else { circuitSwitcher.onNext(closeCircuit) } }, circuitSwitcher.&onError, circuitSwitcher.&onComplete) //start with close circuit circuitSwitcher.onNext(closeCircuit) circuitSwitcher TL;DR Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Failproof: Implementing a Circuit Breaker 50 materialize window flatMap

Slide 124

Slide 124 text

def closeCircuit = stream def openCircuit = fallback ?: Streams.fail(new Exception("service unavailable")) def circuitSwitcher = Streams.switchOnNext() stream .materialize() .window(maxSignals, maxTime) .flatMap{ s -> s.reduce(metrics){ tuple -> if(tuple.t1.isOnNext()) tuple.t2.incrementSuccess() else if(tuple.t1.isOnError()) tuple.t2.incrementFailure() } } .consume({ metrics -> if(metrics.isOpen()) { Streams.timer(metrics.retryTimeout).consume{ circuitSwitcher.onNext(closeCircuit) } circuitSwitcher.onNext(openCircuit) } else { circuitSwitcher.onNext(closeCircuit) } }, circuitSwitcher.&onError, circuitSwitcher.&onComplete) //start with close circuit circuitSwitcher.onNext(closeCircuit) circuitSwitcher TL;DR Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Failproof: Implementing a Circuit Breaker 50 materialize window flatMap reduce

Slide 125

Slide 125 text

def closeCircuit = stream def openCircuit = fallback ?: Streams.fail(new Exception("service unavailable")) def circuitSwitcher = Streams.switchOnNext() stream .materialize() .window(maxSignals, maxTime) .flatMap{ s -> s.reduce(metrics){ tuple -> if(tuple.t1.isOnNext()) tuple.t2.incrementSuccess() else if(tuple.t1.isOnError()) tuple.t2.incrementFailure() } } .consume({ metrics -> if(metrics.isOpen()) { Streams.timer(metrics.retryTimeout).consume{ circuitSwitcher.onNext(closeCircuit) } circuitSwitcher.onNext(openCircuit) } else { circuitSwitcher.onNext(closeCircuit) } }, circuitSwitcher.&onError, circuitSwitcher.&onComplete) //start with close circuit circuitSwitcher.onNext(closeCircuit) circuitSwitcher TL;DR Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Failproof: Implementing a Circuit Breaker 50 materialize window flatMap reduce switchOnNext

Slide 126

Slide 126 text

def closeCircuit = stream def openCircuit = fallback ?: Streams.fail(new Exception("service unavailable")) def circuitSwitcher = Streams.switchOnNext() stream .materialize() .window(maxSignals, maxTime) .flatMap{ s -> s.reduce(metrics){ tuple -> if(tuple.t1.isOnNext()) tuple.t2.incrementSuccess() else if(tuple.t1.isOnError()) tuple.t2.incrementFailure() } } .consume({ metrics -> if(metrics.isOpen()) { Streams.timer(metrics.retryTimeout).consume{ circuitSwitcher.onNext(closeCircuit) } circuitSwitcher.onNext(openCircuit) } else { circuitSwitcher.onNext(closeCircuit) } }, circuitSwitcher.&onError, circuitSwitcher.&onComplete) //start with close circuit circuitSwitcher.onNext(closeCircuit) circuitSwitcher TL;DR Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Failproof: Implementing a Circuit Breaker 50 materialize window flatMap reduce switchOnNext timer

Slide 127

Slide 127 text

def fallback = Streams.fail(new Exception("Fast fail”)) //Open the circuit if there were more than 50% errors over 5 elements or within 3 sec. //Will automatically close the circuit again after 2 sec. Streams.circuitBreaker(stream, fallback, 5, 3, 0.5, 2) .retryWhen{ s -> s.zipWith(Streams.range(1,3)){ tuple.t2 } .flatMap{ Streams.timer(it) } } .consume ( { println it }, { println it.message }, { println errors } ) Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Failproof: Implementing a Circuit Breaker 51

Slide 128

Slide 128 text

def fallback = Streams.fail(new Exception("Fast fail”)) //Open the circuit if there were more than 50% errors over 5 elements or within 3 sec. //Will automatically close the circuit again after 2 sec. Streams.circuitBreaker(stream, fallback, 5, 3, 0.5, 2) .retryWhen{ s -> s.zipWith(Streams.range(1,3)){ tuple.t2 } .flatMap{ Streams.timer(it) } } .consume ( { println it }, { println it.message }, { println errors } ) Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Failproof: Implementing a Circuit Breaker 51 Coming soon™ in M2

Slide 129

Slide 129 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 130

Slide 130 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Featuring…. 53 RabbitMQ Ratpack Reactor Stream Websocket client (RxJS + Angular)

Slide 131

Slide 131 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Featuring…. 53 RabbitMQ Ratpack Reactor Stream Websocket client (RxJS + Angular)

Slide 132

Slide 132 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Featuring…. 53 RabbitMQ Ratpack Reactor Stream Websocket client (RxJS + Angular)

Slide 133

Slide 133 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Featuring…. 53 RabbitMQ Ratpack Reactor Stream Websocket client (RxJS + Angular)

Slide 134

Slide 134 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Early adopters § Checkpoint • Reactor 2.0.0.M2 (TBA) implements 1.0.0.M3 - TCK OK • Akka Streams 1.0-M1 implements 1.0.0.M1 - TCK OK • RxJava - ReactiveStreams 0.3.0 implements 0.4.0 - TCK OK • Ratpack 0.9.12-SNAPSHOT implements 1.0.0.M1 - TCK OK § Links • https://github.com/Netflix/RxJava • http://typesafe.com/blog/typesafe-announces-akka-streams • https://github.com/reactor/reactor • http://www.ratpack.io/manual/0.9.9/streams.html 54

Slide 135

Slide 135 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 136

Slide 136 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 137

Slide 137 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, 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

Slide 138

Slide 138 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Q & A 58

Slide 139

Slide 139 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ session.onComplete() 59