rossen stoyanchev - qcon new york€¦ · reactive spring reactive starters in spring boot 2.0...

55
Rossen Stoyanchev Servlet vs Reactive 5 use cases stacks in

Upload: others

Post on 20-May-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Rossen Stoyanchev

Servlet vs Reactive

5 use cases

stacks

in

Page 2: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Servlet Stack

● Servlet container

● Servlet API

● Spring MVC

Reactive Stack

● Netty, Servlet 3.1+, Undertow

● Reactive Streams

● Spring WebFlux

Page 3: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Reactive SpringReactive starters in Spring Boot 2.0

Spring Framework 5 WebFlux endpoints + reactive WebClient

Reactive Spring Data Kay repositories

Spring Security

and more…

Page 4: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Servlet Stack

Page 5: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

FilterFilter ControllerSpring MVC

Servlet API container

thread

SERVLET STACK

Page 6: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Synchronous APIFilter, Servlet … void

SERVLET STACK

Page 7: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Blocking I/OInputStream, OutputStream

SERVLET STACK

Page 8: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

servletRequest.startAsync()

SERVLET STACK

Page 9: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Filter ControllerFilter Servlet

SERVLET STACK SERVLET STACK

Filter ControllerFilter Servlet

… do work or receive event + dispatch()…

container-thread-1

container-thread-2

Page 10: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Servlet API

Containerthread pool

Controller

SERVLET STACK

can usereactive clients

Input & OutputStream

startAsync()

Page 11: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

ConcurrencymodelsConcurrencyConcurrencyConcurrency

Page 12: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

“elastic”thread pool

“parallel”thread pool

100s, 1000swaiting blocked threads

~ per CPU corebusy worker threads

Synchronous APIs Non-blocking code

Page 13: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

What does it take to not block ?

Page 14: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

event loop at the core

Page 15: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

event driven architecturemessage passing

Page 16: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

means to compose async logic

Page 17: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

bonus:back pressure (a.k.a flow control)

Page 18: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Reactive Stack

Page 19: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Spring WebFlux

WebFilter

NOBLOCKING

ANYTIME

ControllerReactiveServerAdapter

WebFilter

HTTP ServerEvent Loop

REACTIVE STACK

Page 20: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Asynchronous APIWebFilter, WebHandler…

Mono<Void>

REACTIVE STACK

Page 21: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

REACTIVE STACK

Reactor Mono Reactive Streams Publisher

0..1 elements

Page 22: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Non-blocking read: Flux<DataBuffer> getBody()

REACTIVE STACK

Page 23: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Non-blocking write:writeWith(Flux<DataBuffer>)

REACTIVE STACK

Page 24: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

REACTIVE STACK

Reactor Flux Reactive Streams Publisher

0..N elements

Page 25: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

request(1)

onNext(item)

Flux

Reactive Streams back pressure

request(1)

onNext(item)

Page 26: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

request(n)

onNext(item)

FMonoEFluxDMonoCMonoB

ControllerSpring

WebFluxWebFilter

Composition of async logic

MonoA

A B C

EF D

Actual processing

HTTP ServerEvent Loop

REACTIVE STACK

Page 27: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

repository

Use Case #1

dataReactive

Page 28: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Demo

Page 29: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

HTTP GET with reactive data repositoryDesigned to work on both Spring MVC and Spring WebFlux

Simply return reactive type (Flux, Observable) from @Controller

Page 30: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Flux<T>:

finite collection or infinite stream?

Page 31: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Use media type to decide

Page 32: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

“application/json”

finite collection (JSON array)

Page 33: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

No back pressure:

Flux#collectToList

(request all + buffer)

Page 34: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Use Case #2

back pressurewith

Response stream

Page 35: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

“text/event-stream”,“application/stream+json”

infinite stream

Page 36: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Back pressure:

request(n),write, flush,

repeat

Page 37: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

HTTP GET with streaming responseSimply return reactive type (Flux, Observable) from @Controller

Back pressure on Spring MVC and WebFlux

Page 38: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Servlet Container

Servlet API

Spring MVC

Flux

Back pressure against blocking OutputStream

Controller FluxDataRepository

onNext(T)onNext(T)Blocking write on

MvcAsync thread pool

request(n)request(1)

SERVLET STACK …

Page 39: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Servlet 3.1 non-blocking I/O ?

Unlike Servlet 3.0 async, Servlet 3.1 non-blocking is hard to retrofit

Requires deeper change

Mutually exclusive with rest of the Servlet API

Page 40: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

HTTPServer

ServerAdapter

Flux SpringWebFlux

Flux

Response streaming on reactive stack

Controller Flux DataRepo

onNext(T)onNext(T)onNext(T)

Non-blocking write back pressure

to socket

request(n)request(n)request(n)

Page 41: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Demo

Page 42: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Use Case #3remote service

orchestration

Reactive

Page 43: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Demo

Page 44: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Orchestrate non-blocking, nested remote service calls with ease

Similar to reactive data access

Spring MVC and Spring WebFlux

Reactive WebClient

Page 45: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories
Page 46: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories
Page 47: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Use Case #4

request inputReactive

Page 48: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Back pressure to socketNo reading until reactive demand signalled from upstream

Non-blocking

Reactive stack only territory !

Page 49: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

HTTP POST with data@RequestBody argument with reactive type (Mono, Single)

Reactive type is not required

Page 50: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Use Case #5

Data Ingestion

back pressurewith

Page 51: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Media type indicates infinite stream is expected

Non-blocking streaming + back pressure

HTTP POST with stream of data

Page 52: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

HTTPServer

ServerAdapter

Flux SpringWebFlux

Flux

Data ingestion on reactive stack

Controller

onNext(T)onNext(T)Non-blocking read

back pressurefrom socket

request(n)request(n)

Page 53: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Servlet stack summary

Reactive data repository

Streaming to the response with back pressure

Reactive orchestration of remote services

Reactive request input

Data ingestion with back pressure

Page 54: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Reactive stack summary

Reactive data repository

Streaming to the response with back pressure

Reactive orchestration of remote services

Reactive request input

Data ingestion with back pressure

Page 55: Rossen Stoyanchev - QCon New York€¦ · Reactive Spring Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories

Q & A@rstoya05