Introduction to Reactive Streams and Reactor 2.5

40
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ (Rx + Reactive Streams + Spring) % Java 8 = Reactor 2.5 Stéphane Maldini @smaldlini

Transcript of Introduction to Reactive Streams and Reactor 2.5

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

(Rx + Reactive Streams + Spring) % Java 8 = Reactor 2.5

Stéphane Maldini @smaldlini

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Stéphane Maldini

• Survive in London • Reactive Engineering @ • Project Reactor lead • Reactive Streams &

Reactive Streams Commons contributor • Works on Spring Framework 5

upcoming Reactive support • @smaldini on Twitter

2

+

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Do you need to be “reactive” ? If you ask then you probably don’t.

3

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

How much faster is it ? It’s slower than your usual imperative system.

4

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 5

Is it good at anything beyond pretending ? Yes. It scales with the request volume.

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 6

But remember it changes the way you think code. It’s definitely a michelin star lunch.

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Swipe Right - it’s a match

• Mobile/IoT backend API • Server-to-Server communications (HTTP…) • Unreliable clients • Big Data • Mutualized Resources (Cloud…) • User Interfaces

7

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Reactive, what the sh*t?

8

More details on http://fr.slideshare.net/StphaneMaldini/intro-to-reactive-programming-52821416

• Reactive is used to broadly define event-driven systems

• Reactive Manifesto defines qualities of reactive systems

• Reactive programming: moving imperative logic to async, non-blocking, functional-style code, in particular when interacting with “time consuming” resources

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

For reactive programming, we need tools : ☐ Reactive Streams ☐ Reactive APIs

9

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Reactive Streams

• Reactive Streams is a protocol for asynchronous stream processing with non-blocking backpressure

• De facto standard for interop between reactive libraries

• To be included in Java 9 as java.util.concurrent.Flow

10

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Reactive Streams principle

11

Publisher SubscriberData

Demand

• Max(InflightData) <= demand • No data sent without demand • Demand can be unbounded

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Reactive Streams is 3+1 interfaces (+ a TCK)

1215

onSubscribe

onNext* (onError|onComplete)?

Publisher Subscriber

Subscription request(n) cancel()

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Reactive Streams offers Quality of Service for the JVM Bounded hardware use and flow prioritization

13

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Imperative vs Reactive Streams ?

14

User rickSanchez = userRepository.findUser(“rick”);

Blocking, not returning until done !

Might throw exceptions

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

If this blocks, and runs on a serving HTTP request ? If the calling HTTP requests come from another “Microservice” ? If that Microservice is also calling in a blocking way ? If you solve scalability issues by scaling out ? If you agreed to pay the scalability tax, how far can it help you ?

Thinking about the big picture

15

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Imperative vs Reactive Streams ?

16

Publisher<User> rickSanchez = userRepository.findUser(“rick”);

rickSanchez.subscribe(new Subscriber<User>(){ … });

Might send 0, 1 or N Users ! Non Blocking - on demand data emission

Callback for start, result, error or complete

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

How practical is Publisher ?

17

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Do you want to implement Publisher yourself ? Probably not, usually.

18

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

For reactive programming, we need tools : ☑ Reactive Streams ☐ Reactive APIs

19

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

• Transform, Combine, Timebox, Aggregate, Consume… • On the JVM:

• Reactor 2.5 is 4th generation* and based on Reactive Streams • RxJava 1.x: 2nd generation* and most used implementation • Akka Stream 2.x: Lightbend 3rd generation* Reactive API

• Also for other languages, for example RxJS, MostJS

Reactive APIs

20

* Based on http://akarnokd.blogspot.fr/2016/03/operator-fusion-part-1.html

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Reactor 2.5 ecosystem

21

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Reactor Core 2.5

• Cross collaboration with Dávid Karnok (RxJava) and some Spring Framework committers

• Natively based on Reactive Streams, RSC* and Java 8+ • 2 new rich Publisher types : Flux & Mono • Strong focus on efficiency • Ever-improving debugging, logging, testing capabilities

* ReactiveStreamsCommons is a research effort about reactive flows

22

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Flux (0..N elements) with ReactiveX compliant API

23

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Mono (0..1 element)

24

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Imperative vs Reactor ?

25

Mono<User> rickSanchez = userRepository.findUser(“rick”);

rickSanchez.consume(this::userHandler, this::errorHandler)

Single Typed Publisher !

Start the Mono and consume its result or error.

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Mono, a single-data-at-most API

26

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Flux, classic Rx patterns for 0..N events

27

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Visual (Marble) doc for Flux & Mono

28

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Reactor Web Console (preview)

29

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Concurrency Types Cheat Sheet

30

No value Single value Multiple values

Blocking void T Future<T>

Iterable<T> Collection<T> Stream<T>

Non-blocking

CompletableFuture<Void> CompletableFuture<T> CompletableFuture<List<T>>

Reactive Streams

Publisher<Void> Publisher<T> Publisher<T>

RxJava Observable<Void> Completable (1.1.1)

Observable<T> Single<T> (1.0.13)

Observable<T>

Reactor Mono<Void> Mono<T> Flux<T>

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

https://spring.io/blog/2016/04/19/understanding-reactive-types

31

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Reactive Spring

32

• Spring projects are going reactive • Reactor Core is the reactive foundation • RxJava support provided out of the box • You will be able to choose your web engine:

Tomcat, Jetty, Undertow or Netty • Most impact on Web and Data support (IO intensive)

• Spring Reactive experiment • Spring Reactive Playground sample application

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Well known Controller example

33

@RestControllerpublicclassUserController{ privateBlockingRepository<User>repository;

@RequestMapping(path="/save-capitalized",method=RequestMethod.POST) publicvoidsaveCapitalized(@RequestBodyList<User>users){ users.forEach(u->u.setName(u.getName().toUpperCase())); repository.save(users); }}

publicinterfaceBlockingRepository<T>{ voidsave(List<T>elements); Iterable<T>findAll();}

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Controller with Reactive types

34

@RestControllerpublicclassUserController{

privateReactiveRepository<User>repository;

@RequestMapping(path="/save-capitalized",method=RequestMethod.POST) publicMono<Void>saveCapitalized(@RequestBodyFlux<User>users){ returnrepository.save(users.map(u->newUser(u.getName().toUpperCase())); }}

publicinterfaceReactiveRepository<T>{ Mono<Void>save(Publisher<T>elements);

Flux<T>findAll();}

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Blocking vs Reactive: memory consumption

35

Mem

ory

cons

umpt

ion

Time

Blocking Reactive

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Blocking vs Reactive: streaming updates

36

Num

ber o

f use

rs s

aved

in

the

data

base

Time

Blocking Reactive

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Controller with Reactive return values

37

@RestControllerpublicclassUserController{

privateReactiveRepository<User>repository;

@RequestMapping(path="/",method=RequestMethod.GET) publicFlux<User>findAll(){ returnrepository.findAll(); }}

• Optimized serialization when using Flux instead of List • Also perfectly suitable for Server-Sent Events

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Reactive HTTP client with Mono

38

importstaticorg.springframework.web.client.reactive.HttpRequestBuilders.*;importstaticorg.springframework.web.client.reactive.WebResponseExtractors.*;

Mono<Person>result=webClient.perform(get("http://localhost:8080/person").header("X-Test-Header","testvalue").accept(MediaType.APPLICATION_JSON)).extract(body(Person.class));

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Reactive HTTP client with Flux

39

importstaticorg.springframework.web.client.reactive.HttpRequestBuilders.*;importstaticorg.springframework.web.client.reactive.WebResponseExtractors.*;

Flux<Person>response=webClient.perform(get("http://localhost:8080/persons").accept(MediaType.APPLICATION_JSON)).extract(bodyStream(Person.class));

Works for: • JSON array [{"foo":"bar"},{"foo":"baz"}]

• JSON Streaming {"foo":"bar"}{"foo":"baz"}

• SSE with something like .extract(sseStream(Person.class))

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Do you want to learn and play at the same time ? Get Factorio on Steam for a few coins

40