Slide 1

Slide 1 text

Reactor 3 a Reactive foundation for the JVM

Slide 2

Slide 2 text

About me & how to get in touch @SimonBasle

Slide 3

Slide 3 text

First a Survey

Slide 4

Slide 4 text

Who Here Uses...

Slide 5

Slide 5 text

Who Here Uses... Java 8

Slide 6

Slide 6 text

Who Here Uses... Java 8 RxJava

Slide 7

Slide 7 text

Who Here Uses... Java 8 RxJava Reactive Streams

Slide 8

Slide 8 text

Who Here Thinks... Reactor is a fork of Atom editor

Slide 9

Slide 9 text

ok bad joke, done

Slide 10

Slide 10 text

the Agenda

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

101 Reactive Programming

Slide 13

Slide 13 text

101 Reactive Programming types & operators Reactor 3

Slide 14

Slide 14 text

101 Reactive Programming types & operators Reactor 3 other beasts backpressure and

Slide 15

Slide 15 text

101 Reactive Programming types & operators Reactor 3 debugging testing and other beasts backpressure and

Slide 16

Slide 16 text

101 Reactive Programming types & operators Reactor 3 and Spring Reactor debugging testing and other beasts backpressure and

Slide 17

Slide 17 text

101 Reactive Programming types & operators Reactor 3 and Spring Reactor debugging testing and reactor-netty other beasts backpressure and reactor-kafka...

Slide 18

Slide 18 text

Reactive Programming 101 what does it bring to the table?

Slide 19

Slide 19 text

WHY?

Slide 20

Slide 20 text

WHY? because blocking is evil

Slide 21

Slide 21 text

sync/blocking main thread processing resumes I/O ! app does nothing

Slide 22

Slide 22 text

sync/blocking main thread processing resumes I/O BAD ! app does nothing

Slide 23

Slide 23 text

async & blocking main thread wait & join ! new threads, costly ! complex

Slide 24

Slide 24 text

async & blocking main thread wait & join ! new threads, costly ! complex BAD

Slide 25

Slide 25 text

async & nonblocking “event loop” in non-blocking processing chunks no more threads than needed

Slide 26

Slide 26 text

how do you achieve that without losing your mind ?

Slide 27

Slide 27 text

Reactive Programming

Slide 28

Slide 28 text

Composing asynchronous & event-based sequences, using non-blocking operators “ ”

Slide 29

Slide 29 text

without sacrifice

Slide 30

Slide 30 text

without sacrifice Callbacks ? Futures ? easy to block hard to compose callback hell ! not readable

Slide 31

Slide 31 text

Pull? Push!

Slide 32

Slide 32 text

Pull? Push!

Slide 33

Slide 33 text

Pull? Push! (or actually a little bit of Both)

Slide 34

Slide 34 text

vs Iterable - Iterator Publisher - Subscriber

Slide 35

Slide 35 text

Data in Flux

Slide 36

Slide 36 text

Publisher Subscriber push events produces consumes feedback

Slide 37

Slide 37 text

interfaces from Reactive Streams spec Publisher Subscriber feedback consumes push events produces

Slide 38

Slide 38 text

Publisher Subscriber push events produces consumes feedback Subscriber onNext(T) onComplete(); onError(Throwable);

Slide 39

Slide 39 text

Publisher Subscriber produces consumes feedback 0..N elements + 0..1 (complete | error)

Slide 40

Slide 40 text

Publisher Subscriber push events produces consumes feedback backpressure

Slide 41

Slide 41 text

Publisher Subscriber push events produces consumes feedback can I have an API though?

Slide 42

Slide 42 text

Publisher Subscriber push events produces consumes feedback

Slide 43

Slide 43 text

Reactor 3 types and operators

Slide 44

Slide 44 text

Flux for 0..N elements

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Mono for at most 1 element

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

Reactive Streams all the way

Slide 49

Slide 49 text

focus on Java 8

Slide 50

Slide 50 text

focus on Java 8 Duration, CompletableFuture, Streams

Slide 51

Slide 51 text

an Rx-inspired API with a vocabulary of operators similar to RxJava...

Slide 52

Slide 52 text

an Rx-inspired API ...but not exactly the same

Slide 53

Slide 53 text

Flux/Mono generator operator operator operator nothing happens until you subscribe

Slide 54

Slide 54 text

Flux/Mono generator Subscriber operator operator operator nothing happens until you subscribe

Slide 55

Slide 55 text

Flux/Mono generator Subscriber operator operator operator per Subscription state Sub Sub Sub

Slide 56

Slide 56 text

Flux/Mono generator Subscriber operator operator operator data flows Sub Sub Sub

Slide 57

Slide 57 text

examples

Slide 58

Slide 58 text

Flux.range Subscriber map filter buffer

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

Flux.range Subscriber map filter buffer

Slide 63

Slide 63 text

Flux.range Subscriber map filter buffer

Slide 64

Slide 64 text

Flux.range Subscriber map filter buffer

Slide 65

Slide 65 text

Flux.range Subscriber map filter buffer

Slide 66

Slide 66 text

Flux.range Subscriber map filter buffer

Slide 67

Slide 67 text

Flux.range Subscriber map filter buffer

Slide 68

Slide 68 text

Flux.range Subscriber map filter buffer

Slide 69

Slide 69 text

Flux.range(5, 3) .map(i -> i + 3) .filter(i -> i % 2 == 0) .buffer(3)

Slide 70

Slide 70 text

Flux.range(5, 3) .map(i -> i + 3) .filter(i -> i % 2 == 0) .buffer(3) 5, 6, 7 | 8, 9, 10 | 8, 10 | [8,10]|

Slide 71

Slide 71 text

Flux.from Subscriber map filter retry Publisher from HTTP reactive client

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Flux.from Subscriber map filter retry Publisher from HTTP reactive client

Slide 74

Slide 74 text

Flux.from Subscriber map filter retry Publisher from HTTP reactive client resubscribe

Slide 75

Slide 75 text

go DEEPER! async sub-processes with flatMap

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

flatMap(user -> tweetStream(user))

Slide 78

Slide 78 text

flatMap(user -> tweetStream(user))

Slide 79

Slide 79 text

flatMap(user -> tweetStream(user))

Slide 80

Slide 80 text

flatMap(user -> tweetStream(user))

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

& much more...

Slide 83

Slide 83 text

threading contexts

Slide 84

Slide 84 text

Reactor is agnostic

Slide 85

Slide 85 text

however it facilitates switching

Slide 86

Slide 86 text

Schedulers

Slide 87

Slide 87 text

Schedulers elastic, parallel, single, timer...

Slide 88

Slide 88 text

publishOn switch rest of the flux on a thread

Slide 89

Slide 89 text

subscribeOn make the subscription and request happen on a particular thread

Slide 90

Slide 90 text

Flux/Mono generator operator subscribeO n operator publishOn operator operator Subscriber Sub Sub Sub Sub Sub Sub

Slide 91

Slide 91 text

Flux/Mono generator operator subscribe On operator publish On operator operator Subscriber Sub Sub Sub Sub Sub Sub

Slide 92

Slide 92 text

Flux/Mono generator operator subscribeO n operator publishOn operator operator Subscriber Sub Sub Sub Sub Sub Sub

Slide 93

Slide 93 text

Flux/Mono generator operator subscribeO n operator publishOn operator operator Subscriber Sub Sub Sub Sub Sub Sub

Slide 94

Slide 94 text

Flux/Mono generator operator subscribeO n operator publishOn operator operator Subscriber Sub Sub Sub Sub Sub Sub

Slide 95

Slide 95 text

Flux/Mono generator operator subscribeO n operator publishOn operator operator Subscriber Sub Sub Sub Sub Sub Sub

Slide 96

Slide 96 text

Testing & Debugging in an asynchronous world

Slide 97

Slide 97 text

Testing a Publisher StepVerifier

Slide 98

Slide 98 text

Testing a Publisher with Virtual Time support

Slide 99

Slide 99 text

Simulate a source TestPublisher

Slide 100

Slide 100 text

Debugging Issues stacktraces get hard to decipher

Slide 101

Slide 101 text

usually just show where Subscription happens

Slide 102

Slide 102 text

java.lang.IndexOutOfBoundsException: Source emitted more than one item at reactor.core.publisher.MonoSingle$SingleSubscriber.onNext(MonoSingle.java:120) at reactor.core.publisher.FluxOnAssembly$OnAssemblySubscriber.onNext(FluxOnAssembly.java:314) ... ... at reactor.core.publisher.Mono.subscribeWith(Mono.java:2668) at reactor.core.publisher.Mono.subscribe(Mono.java:2629) at reactor.core.publisher.Mono.subscribe(Mono.java:2604) at reactor.core.publisher.Mono.subscribe(Mono.java:2582) at reactor.guide.GuideTests.debuggingActivated(GuideTests.java:727)

Slide 103

Slide 103 text

Find where the Flux was instantiated (assembly)

Slide 104

Slide 104 text

Checkpoint() or full assembly tracing

Slide 105

Slide 105 text

costly! Checkpoint() or full assembly tracing

Slide 106

Slide 106 text

Assembly trace from producer [reactor.core.publisher.MonoSingle] : reactor.core.publisher.Flux.single(Flux.java:5335) reactor.guide.GuideTests.scatterAndGather(GuideTests.java:689) reactor.guide.GuideTests.populateDebug(GuideTests.java:702)

Slide 107

Slide 107 text

BACKPRESSURE and other beasts

Slide 108

Slide 108 text

Publisher Subscriber subscribe

Slide 109

Slide 109 text

Publisher Subscriber push data as fast as possible

Slide 110

Slide 110 text

Publisher Subscriber subscribe with small request (eg. 1)

Slide 111

Slide 111 text

Publisher Subscriber 1 onNext

Slide 112

Slide 112 text

Publisher Subscriber request more (eg. 2)

Slide 113

Slide 113 text

Publisher Subscriber 2 onNext

Slide 114

Slide 114 text

Publisher Subscriber backpressure

Slide 115

Slide 115 text

other ways of dealing with backpressure eg. drop, buffer...

Slide 116

Slide 116 text

internal optimisations

Slide 117

Slide 117 text

macro FUSION avoids unnecessary request back-and-forth

Slide 118

Slide 118 text

micro FUSION share internal structures for less allocation

Slide 119

Slide 119 text

lock free operators

Slide 120

Slide 120 text

lock free operators and Work Stealing CPU 1 CPU 2 CPU 3 CPU 4 CPU 5

Slide 121

Slide 121 text

Reactor and Spring

Slide 122

Slide 122 text

Reactor and Spring and do I need Spring to use Reactor ?

Slide 123

Slide 123 text

NO philosoraptor you don’t

Slide 124

Slide 124 text

Reactor 3 is a dependency of Spring 5 not the other way around

Slide 125

Slide 125 text

5

Slide 126

Slide 126 text

Java 8 baseline

Slide 127

Slide 127 text

reactive focus

Slide 128

Slide 128 text

new WEB stack WebFlux

Slide 129

Slide 129 text

@RestController(“/user”) public class UserController { @GetMapping(“/{id}”) Mono getUser(String id) {...} }

Slide 130

Slide 130 text

functional option for Routing

Slide 131

Slide 131 text

Spring Data reactive repositories

Slide 132

Slide 132 text

@GetMapping(“/{id}”) Mono getUser(String id) { return reactiveRepo.findOne(id); }

Slide 133

Slide 133 text

Reactor and the Network reactor-netty

Slide 134

Slide 134 text

reactor-netty builds on Netty to provide reactive I/O

Slide 135

Slide 135 text

Client / Server

Slide 136

Slide 136 text

TCP or udp

Slide 137

Slide 137 text

Http and WebSockets

Slide 138

Slide 138 text

HttpServer.create(0) .newHandler((in, out) -> out .sendWebsocket((i, o) -> o.options(opt -> opt.flushOnEach()) .sendString(Flux.just("test") .delayElementsMillis(100) .repeat()) ) ) .block();

Slide 139

Slide 139 text

still a bit low level

Slide 140

Slide 140 text

still a bit low level

Slide 141

Slide 141 text

reactor-kafka topics as Flux

Slide 142

Slide 142 text

reactive API over Kafka Producer / Consumer

Slide 143

Slide 143 text

send(Flux) into Kafka

Slide 144

Slide 144 text

Flux receive() from Kafka

Slide 145

Slide 145 text

(currently in MILESTONE 2)

Slide 146

Slide 146 text

Questions?

Slide 147

Slide 147 text

Thanks!

Slide 148

Slide 148 text

Credits ● Springfield Plant: copyright FOX ● Raised Hand: CC0 (via Pixabay) ● Checklist: CC-By Crispy (via Flickr) ● Robot Devil: copyright FOX ● Volume Knob: CC0 (via Pixabay) ● Camel Shape: CC0 (via Pixabay) ● Dromedary Shape: CC-By-SA USPN,Whidou (via Wikimedia) ● Dam: CC-By-SA Matthew Hatton (via geograph.org.uk) ● Cogs: CC0 (via publicdomainpictures.net) ● Thread Balls: CC0 (via Pixabay) ● The Fortune Teller: Georges de la Tour (public domain) ● Microphone: CC0 (via Pexels) ● End Sands: CC0 (via Pixabay) ● logos: Pivotal, Spring, Twitter and Github logo copyright their respective companies.