Slide 1

Slide 1 text

Spring 5 & Reactive systems Project Reactor

Slide 2

Slide 2 text

Reactive Programming what is it?

Slide 3

Slide 3 text

Imperative programming expression is evaluated once value is assigned to a variable Reactive programming is all about responding to value changes

Slide 4

Slide 4 text

Reactive programming Who has done it?

Slide 5

Slide 5 text

You have probably done some reactive programming, even if you didn’t realise it at that time: - Defining cell values in spreadsheets is similar to defining variables in imperative programming - Defining cell expressions in spreadsheets is similar to defining and operating on reactive types Reactive programming

Slide 6

Slide 6 text

Previous example in spreadsheet: - assign cell B1 with value of 2 - assign cell B2 with value of 3 - assign cell B3 with an expression that multiplies B1 value with B2 value - when value of either referenced component in the expression changes -> expression is re-evaluated automagically in B3

Slide 7

Slide 7 text

Spring 5 WebFlux

Slide 8

Slide 8 text

Spring 5 Embraced different perspective on modern day web architecture

Slide 9

Slide 9 text

Spring 5 Embraced different perspective on modern day web architecture Spring WebFlux = parallel web architecture next to the servlet-based Spring MVC

Slide 10

Slide 10 text

Non-Blocking Event
 Queue IO
 Calls Schedule Schedule Callback Callback Request Response EVENT
 LOOP

Slide 11

Slide 11 text

Load test milliseconds (95th percentile) 0 500 1000 1500 2000 2500 3000 3500 4000 Concurrent users 1 10 100 200 500 1000 2000 Synchronous Reactive (500ms backend service)

Slide 12

Slide 12 text

Load test requests served per second (parallellism : 100) 0 25 50 75 100 1 8 32 96 768 Blocking WebFlux (Reactive)

Slide 13

Slide 13 text

Project Reactor history

Slide 14

Slide 14 text

Dutch computer scientist Erik Meijer @Microsoft: C#, LINQ Reactive Extensions (RX) Currently Director Of Engineering @ Facebook Bearer of extremely cool tie-dyed shirts reactivex.io

Slide 15

Slide 15 text

Reactive Manifesto

Slide 16

Slide 16 text

Reactive Manifesto Responsive

Slide 17

Slide 17 text

Reactive Manifesto Responsive Resilient

Slide 18

Slide 18 text

Reactive Manifesto Responsive Resilient Elastic

Slide 19

Slide 19 text

Reactive Manifesto Responsive Resilient Elastic Message Driven

Slide 20

Slide 20 text

Reactive Manifesto To be reactive, according to The Reactive Manifesto, you have to be Responsive, Resilient, Elastic, and Message Driven. The last criteria in this list caused big movement into the asynchronous way of communications.

Slide 21

Slide 21 text

Reactive Streams

Slide 22

Slide 22 text

rxjava 1.x rx reactive streams commons rxjava 2.x reactive streams akka reactor 2.x reactor 3.x spring

Slide 23

Slide 23 text

Reactive Streams

Slide 24

Slide 24 text

Reactive Streams

Slide 25

Slide 25 text

Reactive Streams

Slide 26

Slide 26 text

Reactive Streams standard for asynchronous stream processing with non-blocking back pressure

Slide 27

Slide 27 text

Goal of Reactive Streams exchange of data across an asynchronous boundary while ensuring that the receiving side is not forced to buffer arbitrary amounts of data (back pressure)

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Publisher

Slide 32

Slide 32 text

Subscriber

Slide 33

Slide 33 text

Subscription

Slide 34

Slide 34 text

Processor

Slide 35

Slide 35 text

Pull?

Slide 36

Slide 36 text

PUSH! Pull?

Slide 37

Slide 37 text

Iterator Subscriber Comparison Iterable VS Publisher

Slide 38

Slide 38 text

Java 9 2015 Start Reactive Streams 1.0 2016 Growth Akka, Spring, Pivotal Project Reactor, RxJava, MongoDB, Kafka, … 2017 Java The interfaces available in JDK9’s java.util.concurrent.Flow, are 1:1 semantically equivalent

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Reactive Streams PUBLISHER SUBSCRIBER Subscribe then
 request(n) data
 (Backpressure) 0..N data then
 0..1 (Error | Complete)

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Reactive Streams implementations

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Reactive core Typed [0|1|N] sequences Non-blocking IPC non-blocking foundation
 interacts with Java 8
 functional API, Completable
 Future, Streams reactive composable API
 Flux[N]
 Mono[0/1]
 implements Reactive Extensions suited for microservices architecture
 backpressure-ready network engines
 (HTTP / Websockets / TCP / UDP)
 reactive encoding/decoding Reactor

Slide 48

Slide 48 text

Operators work with the stream

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

5 6 7

Slide 54

Slide 54 text

5 6 7 8 9 10

Slide 55

Slide 55 text

5 6 7 8 9 10 8 10

Slide 56

Slide 56 text

5 6 7 8 9 10 8 10 [8,10]

Slide 57

Slide 57 text

SUBSCRIBE! Nothing happens until you

Slide 58

Slide 58 text

Function composition

Slide 59

Slide 59 text

Function composition map collect sorted filter Original list of machines List of model names

Slide 60

Slide 60 text

Nothing happens until you subscribe Assembly Time

Slide 61

Slide 61 text

Nothing happens until you subscribe Execution Time

Slide 62

Slide 62 text

Assembly time

Slide 63

Slide 63 text

subscription Execution time

Slide 64

Slide 64 text

AGNOSTIC! Reactor is threading

Slide 65

Slide 65 text

Concurrency … but facilitates switching

Slide 66

Slide 66 text

Schedulers elastic, parallel, single, …

Slide 67

Slide 67 text

publishOn / subscribeOn determine threading

Slide 68

Slide 68 text

publishOn switch rest of Flux on a thread

Slide 69

Slide 69 text

Flux.op1.op2.publishOn.op3.op4.subscribe

Slide 70

Slide 70 text

Flux.op1.op2.publishOn.op3.op4.subscribe 1 2 Thread calling subscribe()
 is where data flows initially 1 After publishOn() data flows in Thread 2 2

Slide 71

Slide 71 text

subscribeOn make subscription happen on particular thread

Slide 72

Slide 72 text

Flux.op1.op2.subscribeOn.op3.op4.subscribe

Slide 73

Slide 73 text

Flux.op1.op2.subscribeOn.op3.op4.subscribe 1 2 SubscribeOn() changes where sequence subscription happens… 1 … which is also where data flows initially 2

Slide 74

Slide 74 text

and the two together? knock yourself out…

Slide 75

Slide 75 text

But… Frameworks, libraries & operators build on top might be opinionated

Slide 76

Slide 76 text

HARD! Async debugging is

Slide 77

Slide 77 text

When you shift to asynchronous code, things can get more complicated: - Ugly stack traces - Missing async call chain - Stack pollution Debugging

Slide 78

Slide 78 text

java.lang.ArithmeticException: / by zero at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:115) at reactor.core.publisher.FluxJust$WeakScalarSubscription.request(FluxJust.java:99) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.BlockingSingleSubscriber.onSubscribe(BlockingSingleSubscriber.java:49) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxJust.subscribe(FluxJust.java:70) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.Flux.subscribe(Flux.java:6873) at mycode.test(MyTest.java:xx) Debugging

Slide 79

Slide 79 text

Spring WebFlux Spring WebFlux vs Spring MVC

Slide 80

Slide 80 text

Spring WebFlux

Slide 81

Slide 81 text

Spring 5 WebFlux

Slide 82

Slide 82 text

Servlet Container Netty, Tomcat, Jetty, Undertow HTTP / Reactive Streams spring-webflux Servlet API spring-webmvc Router Functions @Controller, @RequestMapping Spring 5

Slide 83

Slide 83 text

Demo Spring WebFlux application Testing Reactive Mongo Server-Sent Events

Slide 84

Slide 84 text

Real-life example Measuring time with Reactor (NTP)

Slide 85

Slide 85 text

Network Time Protocol

Slide 86

Slide 86 text

S1 S2 S3

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

SNTP time Client time Server 135ms 137ms 298ms 231ms t1 t2 t0 t3 δ=65ms time offset ϑ = (t1 − t0 ) + (t2 − t3 ) 2 round − trip delay δ = (t3 − t0 ) − (t2 − t1 )

Slide 89

Slide 89 text

Less than ideal network —UDP

Slide 90

Slide 90 text

DNS Resolution IP1 IP2 IP3 IP4 SNTP Request 1 NTP POOL Least Roundtrip Delay SNTP Request 2 SNTP Request 3 SNTP Request 4 SNTP Request 5 Statistical analysis Sort by clock offset + median NTP Time Best possible approximation of 5 . . .

Slide 91

Slide 91 text

Everything is a STREAM !!! ⋎ almost

Slide 92

Slide 92 text

DNS Resolution IP1 IP2 IP3 IP4 SNTP Request 1 NTP POOL Least Roundtrip Delay SNTP Request 2 SNTP Request 3 SNTP Request 4 SNTP Request 5 Statistical analysis Sort by clock offset + median NTP Time Best possible approximation of 5 . . .

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

testability++ Reactive approach pure functions elegant concise

Slide 96

Slide 96 text

Spring 5 WebFlux

Slide 97

Slide 97 text

Summarizing Reactor

Slide 98

Slide 98 text

Reactor is awesome! Asynchronous, non-blocking, reactive data flows Event-based programs

Slide 99

Slide 99 text

Reactor is so so… Reactor is not the holy grail.

Slide 100

Slide 100 text

Reactor is so so… Reactor is not the holy grail. Should be used for streams processing, for general asynchronous non-blocking computation, see coroutines (Spring 5.2+)

Slide 101

Slide 101 text

Reactor has downsides? Mindshift (also when testing)
 Debugging & stacktraces are harder For the DDD people: (small) pollution of your domain with Mono/Flux

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

request / stream finite stream of many RSocket request / response stream of 1 fire and forget channel no response bi-directional streams

Slide 106

Slide 106 text

Questions with(out) answers

Slide 107

Slide 107 text

Simplicity does not precede complexity but follows it Alan Perlis

Slide 108

Slide 108 text

Thank you! Spring WebFlux projectreactor.io