Slide 1

Slide 1 text

codecentric AG Patrick Peschlow Reactive Programming

Slide 2

Slide 2 text

codecentric AG The Challenge Users

Slide 3

Slide 3 text

codecentric AG The Challenge Users don’t like to wait

Slide 4

Slide 4 text

codecentric AG The Challenge Users don’t wait

Slide 5

Slide 5 text

codecentric AG The Challenge Many users don’t wait

Slide 6

Slide 6 text

codecentric AG The Challenge Many many users don’t wait

Slide 7

Slide 7 text

codecentric AG The Challenge Today’s applications don’t cope

Slide 8

Slide 8 text

codecentric AG So we need a Manifesto

Slide 9

Slide 9 text

codecentric AG Reactive Traits From: http://www.reactivemanifesto.org

Slide 10

Slide 10 text

codecentric AG Event-driven Trait − Asynchronous − Synchronous APIs tightly couple client and service (often block) − Asynchronous means the client can decide if and where to block ! − Non-blocking − Blocking APIs may require large numbers of threads (often coupled to users) − Wastes CPU resources (context switches, scheduling) − Non-blocking frees threads to do new work − Define worker thread pools based on resources ! − Share nothing ! − Loose coupling Based on: Roland Kuhn, Jamie Allen. „Reactive Design Patterns“. Manning. Chapter 1 preview.

Slide 11

Slide 11 text

codecentric AG Example Time Scale of System Latencies From: Brendan Gregg. „Systems Performance: Enterprise and the Cloud“. Prentice Hall, 2013

Slide 12

Slide 12 text

codecentric AG Futures and Promises − Future: A reference to an asynchronously computed value ! − Promise: A future with additional features − May be completed in various ways (e.g., by a setter) − Enables composition (one promise completes another) ! − (Functional) Reactive Programming − Futures and promises taking to the extreme

Slide 13

Slide 13 text

codecentric AG Futures and Promises Demo

Slide 14

Slide 14 text

codecentric AG RxJava Reactive Extensions for the JVM — A library for composing
 asynchronous and event-based programs using observable sequences for the Java VM From: http://github.com/Netflix/RxJava

Slide 15

Slide 15 text

codecentric AG Observable single items multiple items synchronous T  getData() Iterable  getData() asynchronous Future  getData() Observable  getData() From: http://github.com/Netflix/RxJava/wiki

Slide 16

Slide 16 text

codecentric AG Observable From: http://github.com/Netflix/RxJava/wiki Iterable (pull) Observable (push) T  next() onNext(T) throws Exception onError(Exception) returns onCompleted()

Slide 17

Slide 17 text

codecentric AG Transformations on Observables From: http://netflix.github.io/RxJava/javadoc/rx/Observable.html

Slide 18

Slide 18 text

codecentric AG RxJava Demo

Slide 19

Slide 19 text

codecentric AG Transformations on Observables From: http://netflix.github.io/RxJava/javadoc/rx/Observable.html

Slide 20

Slide 20 text

codecentric AG Transformations on Observables From: http://netflix.github.io/RxJava/javadoc/rx/Observable.html

Slide 21

Slide 21 text

codecentric AG Transformations on Observables From: http://netflix.github.io/RxJava/javadoc/rx/Observable.html

Slide 22

Slide 22 text

codecentric AG Transformations on Observables From: http://netflix.github.io/RxJava/javadoc/rx/Observable.html

Slide 23

Slide 23 text

codecentric AG Transformations on Observables From: http://netflix.github.io/RxJava/javadoc/rx/Observable.html

Slide 24

Slide 24 text

codecentric AG Transformations on Observables From: http://netflix.github.io/RxJava/javadoc/rx/Observable.html

Slide 25

Slide 25 text

codecentric AG Transformations on Observables From: http://netflix.github.io/RxJava/javadoc/rx/Observable.html

Slide 26

Slide 26 text

codecentric AG Transformations on Observables From: http://netflix.github.io/RxJava/javadoc/rx/Observable.html

Slide 27

Slide 27 text

codecentric AG Transformations on Observables From: http://netflix.github.io/RxJava/javadoc/rx/Observable.html

Slide 28

Slide 28 text

codecentric AG Transformations on Observables From: http://netflix.github.io/RxJava/javadoc/rx/Observable.html

Slide 29

Slide 29 text

codecentric AG Benefits of Observables − Life cycle of client and service are decoupled ! − Service API keeps control over its implementation ! − Client decides how its subscription should behave (e.g., timeouts) ! − Note: The „client“ may be another service having its own clients − Not necessarily a human or a browser

Slide 30

Slide 30 text

codecentric AG Back Pressure − An eager producer can overflow its consumer(s) − Due to inversion of control when going from sync to async ! − Consumers need a mechanism for „back pressure“ − Tell the producer how much data you can handle − Similar to, e.g., receiver window in TCP From: http://www.reactive-streams.org

Slide 31

Slide 31 text

codecentric AG So are we Reactive yet? From: http://www.reactivemanifesto.org

Slide 32

Slide 32 text

codecentric AG Responsive Trait − Goal: Achieve bounded latency ! − Parallelize tasks within services ! − Choose algorithms with small variance in execution time − Predictable execution time for some target percentile ! − Use explicit and bounded queues ! − Use circuit breakers that monitor target metrics and react on violation − fail-fast and reject, return partial results, or delegate to another service − e.g., for input queues, reject requests when the queue is full Based on: Roland Kuhn, Jamie Allen. „Reactive Design Patterns“. Manning. Chapter 1 preview.

Slide 33

Slide 33 text

codecentric AG Resilient Trait − Goal: Service stays available even in the presence of unexpected failures − software, hardware, human ! − Distribute the system ! − Use bulkheading ! ! ! ! − Let supervisors handle failure − separate failure handling from the actual business logic of the service Based on: Roland Kuhn, Jamie Allen. „Reactive Design Patterns“. Manning. Chapter 1 preview. From: http://www.reactivemanifesto.org

Slide 34

Slide 34 text

codecentric AG Scalable Trait − Goal: Scale up (and down) based on load ! − Distribute requests to various instances of the services ! − The more stateless services, the better ! − Measure load at run time and react accordingly − But: No reason to forego capacity planning Based on: Roland Kuhn, Jamie Allen. „Reactive Design Patterns“. Manning. Chapter 1 preview.

Slide 35

Slide 35 text

codecentric AG What about Actors? − Seem to fulfill the reactive traits much better than reactive programming does − Asynchronous, non-blocking, and event-driven by definition − Supervisors and „let it crash“ − Distributed − Easily scalable ! − But maybe too „heavyweight“ for some problems within services − Often, stateless actors don’t suffice or get highly complex ! − Promising approach: Combine actors and reactive programming − Actors for the backbone and cluster „infrastructure“ − Reactive Programming inside the nodes

Slide 36

Slide 36 text

codecentric AG Conclusion − Clear need for reactive applications ! − Well addressed by the actor model and reactive programming approaches ! − Building reactive applications requires different thinking ! − Helped by various reactive frameworks, in particular on the JVM ! − Brings up the topic of essential complexity vs. incidental complexity

Slide 37

Slide 37 text

codecentric AG Frameworks − Reactive Extensions (Rx)
 http://rx.codeplex.com ! − RxJava
 http://github.com/Netflix/RxJava ! − Akka
 http://akka.io ! − Vert.x
 http://vertx.io ! − Reactor
 https://github.com/reactor/reactor

Slide 38

Slide 38 text

codecentric AG Resources − Reactive Manifesto
 http://www.reactivemanifesto.org ! − Reactive Streams
 http://www.reactive-streams.org ! − Conference „React 2014“
 http://reactconf.com
 http://www.youtube.com/user/reactconf ! − Online course on Reactive Programming
 http://www.coursera.org/course/reactive ! − Roland Kuhn, Jamie Allen. „Reactive Design Patterns“. Manning. Currently in writing. ! − Michael Nygard. „Release It!“. Pragmatic Bookshelf. 2007.

Slide 39

Slide 39 text

codecentric AG Questions? Dr. rer. nat. Patrick Peschlow
 codecentric AG
 Merscheider Straße 1
 42699 Solingen
 
 tel +49 (0) 212.23 36 28 54
 fax +49 (0) 212.23 36 28 79
 [email protected]
 
 www.codecentric.de

Slide 40

Slide 40 text

codecentric AG Bonus: What does this mean for my (Big) Data? − If reactive applications focus so much on availability, what about consistency − CAP suggests you need to choose between C and A ! − But: Even with CAP you can have C and A most of the time − You just need to have a reliable strategy for detecting and handling partitions − http://www.infoq.com/articles/cap-twelve-years-later-how-the-rules-have-changed ! − Note: There is also a „Reactive Programming“ in the context of databases − http://www.espressologic.com/reactive-programming-database-logic/ − Don’t confuse it with the „Functional Reactive Programming“ we have seen