Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

the Agenda

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Reactive Programming 101 what does it bring to the table?

Slide 6

Slide 6 text

WHY? because blocking is evil

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

how do you achieve that without losing your mind ?

Slide 11

Slide 11 text

Reactive Programming

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

without sacrifice

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Pull? Push!

Slide 16

Slide 16 text

Pull? Push!

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

vs Iterable - Iterator Publisher - Subscriber

Slide 19

Slide 19 text

Data in Flux

Slide 20

Slide 20 text

Publisher Subscriber push events produces consumes feedback

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Publisher Subscriber push events produces consumes feedback backpressure

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Publisher Subscriber push events produces consumes feedback

Slide 27

Slide 27 text

Reactor 3 types and operators

Slide 28

Slide 28 text

Flux for 0..N elements

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Mono for at most 1 element

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Reactive Streams all the way

Slide 33

Slide 33 text

focus on Java 8

Slide 34

Slide 34 text

focus on Java 8 Duration, CompletableFuture, Streams

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

examples

Slide 42

Slide 42 text

Flux.range Subscriber map filter buffer

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

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

Slide 47

Slide 47 text

Flux.range Subscriber map filter buffer

Slide 48

Slide 48 text

Flux.range Subscriber map filter buffer

Slide 49

Slide 49 text

Flux.range Subscriber map filter buffer

Slide 50

Slide 50 text

Flux.range Subscriber map filter buffer

Slide 51

Slide 51 text

Flux.range Subscriber map filter buffer

Slide 52

Slide 52 text

Flux.range Subscriber map filter buffer

Slide 53

Slide 53 text

Flux.range Subscriber map filter buffer

Slide 54

Slide 54 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 55

Slide 55 text

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

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

go DEEPER! async sub-processes with flatMap

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

flatMap(user -> tweetStream(user))

Slide 62

Slide 62 text

flatMap(user -> tweetStream(user))

Slide 63

Slide 63 text

flatMap(user -> tweetStream(user))

Slide 64

Slide 64 text

flatMap(user -> tweetStream(user))

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

& much more...

Slide 67

Slide 67 text

threading contexts

Slide 68

Slide 68 text

Reactor is agnostic

Slide 69

Slide 69 text

however it facilitates switching

Slide 70

Slide 70 text

Schedulers

Slide 71

Slide 71 text

Schedulers elastic, parallel, single, timer...

Slide 72

Slide 72 text

publishOn switch rest of the flux on a thread

Slide 73

Slide 73 text

subscribeOn make the subscription and request happen on a particular thread

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

Testing & Debugging in an asynchronous world

Slide 81

Slide 81 text

Testing a Publisher StepVerifier

Slide 82

Slide 82 text

Testing a Publisher with Virtual Time support

Slide 83

Slide 83 text

Simulate a source TestPublisher

Slide 84

Slide 84 text

Debugging Issues stacktraces get hard to decipher

Slide 85

Slide 85 text

usually just show where Subscription happens

Slide 86

Slide 86 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 87

Slide 87 text

Find where the Flux was instantiated (assembly)

Slide 88

Slide 88 text

Checkpoint() or full assembly tracing

Slide 89

Slide 89 text

costly! Checkpoint() or full assembly tracing

Slide 90

Slide 90 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 91

Slide 91 text

BACKPRESSURE and other beasts

Slide 92

Slide 92 text

Publisher Subscriber subscribe

Slide 93

Slide 93 text

Publisher Subscriber push data as fast as possible

Slide 94

Slide 94 text

Publisher Subscriber subscribe with small request (eg. 1)

Slide 95

Slide 95 text

Publisher Subscriber 1 onNext

Slide 96

Slide 96 text

Publisher Subscriber request more (eg. 2)

Slide 97

Slide 97 text

Publisher Subscriber 2 onNext

Slide 98

Slide 98 text

Publisher Subscriber backpressure

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

internal optimisations

Slide 101

Slide 101 text

macro FUSION avoids unnecessary request back-and-forth

Slide 102

Slide 102 text

micro FUSION share internal structures for less allocation

Slide 103

Slide 103 text

lock free operators

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

Reactor and Spring

Slide 106

Slide 106 text

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

Slide 107

Slide 107 text

NO philosoraptor you don’t

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

5

Slide 110

Slide 110 text

Java 8 baseline

Slide 111

Slide 111 text

reactive focus

Slide 112

Slide 112 text

new WEB stack WebFlux

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

functional option for Routing

Slide 115

Slide 115 text

Spring Data reactive repositories

Slide 116

Slide 116 text

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

Slide 117

Slide 117 text

Reactor and the Network reactor-netty

Slide 118

Slide 118 text

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

Slide 119

Slide 119 text

Client / Server

Slide 120

Slide 120 text

TCP or udp

Slide 121

Slide 121 text

Http and WebSockets

Slide 122

Slide 122 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 123

Slide 123 text

still a bit low level

Slide 124

Slide 124 text

still a bit low level

Slide 125

Slide 125 text

reactor-kafka topics as Flux

Slide 126

Slide 126 text

reactive API over Kafka Producer / Consumer

Slide 127

Slide 127 text

send(Flux) into Kafka

Slide 128

Slide 128 text

Flux receive() from Kafka

Slide 129

Slide 129 text

(currently in MILESTONE 1)

Slide 130

Slide 130 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.