S e r v l e t v s R e a c t i v e in 5 use cases - - PowerPoint PPT Presentation

s e r v l e t v s r e a c t i v e
SMART_READER_LITE
LIVE PREVIEW

S e r v l e t v s R e a c t i v e in 5 use cases - - PowerPoint PPT Presentation

stacks S e r v l e t v s R e a c t i v e in 5 use cases Rossen Stoyanchev Servlet Stack Reactive Stack Servlet container Netty, Servlet 3.1+, Undertow Servlet API Reactive Streams Spring MVC Spring


slide-1
SLIDE 1

Rossen Stoyanchev

S e r v l e t v s R e a c t i v e

5 use cases

stacks

in

slide-2
SLIDE 2

Servlet Stack

  • Servlet container
  • Servlet API
  • Spring MVC

Reactive Stack

  • Netty, Servlet 3.1+, Undertow
  • Reactive Streams
  • Spring WebFlux
slide-3
SLIDE 3

Reactive Spring

Reactive starters in Spring Boot 2.0 Spring Framework 5 WebFlux endpoints + reactive WebClient Reactive Spring Data Kay repositories Spring Security and more…

slide-4
SLIDE 4

Servlet Stack

slide-5
SLIDE 5

Filter Filter Controller Spring MVC

Servlet API

container thread

SERVLET STACK

slide-6
SLIDE 6

Synchronous API Filter, Servlet … void

SERVLET STACK

slide-7
SLIDE 7

Blocking I/O InputStream, OutputStream

SERVLET STACK

slide-8
SLIDE 8

servletRequest.startAsync()

SERVLET STACK

slide-9
SLIDE 9

Filter Controller Filter Servlet

SERVLET STACK SERVLET STACK

Filter Controller Filter Servlet

… do work or receive event + dispatch()…

container- thread-1 container- thread-2

slide-10
SLIDE 10

Servlet API Container thread pool Controller

SERVLET STACK

can use reactive clients

Input & OutputStream startAsync()

slide-11
SLIDE 11

Concurrency models Concurrency Concurrency Concurrency

slide-12
SLIDE 12

“elastic” thread pool “parallel” thread pool

100s, 1000s waiting blocked threads ~ per CPU core busy worker threads

Synchronous APIs Non-blocking code

slide-13
SLIDE 13

What does it take to not block ?

slide-14
SLIDE 14

event loop at the core

slide-15
SLIDE 15

event driven architecture message passing

slide-16
SLIDE 16

means to compose async logic

slide-17
SLIDE 17

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

slide-18
SLIDE 18

Reactive Stack

slide-19
SLIDE 19

Spring WebFlux Web Filter NO BLOCKING ANY TIME Controller Reactive Server Adapter Web Filter

HTTP Server Event Loop

REACTIVE STACK

slide-20
SLIDE 20

Asynchronous API WebFilter, WebHandler… Mono<Void>

REACTIVE STACK

slide-21
SLIDE 21

REACTIVE STACK

Reactor Mono Reactive Streams Publisher 0..1 elements

slide-22
SLIDE 22

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

REACTIVE STACK

slide-23
SLIDE 23

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

REACTIVE STACK

slide-24
SLIDE 24

REACTIVE STACK

Reactor Flux Reactive Streams Publisher 0..N elements

slide-25
SLIDE 25

request(1)

  • nNext(item)

Flux

Reactive Streams back pressure

request(1)

  • nNext(item)
slide-26
SLIDE 26

request(n)

  • nNext(item)

F

Mono

E

Flux

D

Mono

C

Mono

B

Controller Spring WebFlux WebFilter

Composition of async logic

Mono

A A B C E F D Actual processing

HTTP Server Event Loop

REACTIVE STACK

slide-27
SLIDE 27

repository

Use Case #1

data Reactive

slide-28
SLIDE 28

Demo

slide-29
SLIDE 29

HTTP GET with reactive data repository

Designed to work on both Spring MVC and Spring WebFlux Simply return reactive type (Flux, Observable) from @Controller

slide-30
SLIDE 30

Flux<T>:

finite collection or infinite stream?

slide-31
SLIDE 31

Use media type to decide

slide-32
SLIDE 32

“application/json”

finite collection (JSON array)

slide-33
SLIDE 33

No back pressure: Flux#collectToList (request all + buffer)

slide-34
SLIDE 34

Use Case #2

back pressure

with

R e s p

  • n

s e s t r e a m

slide-35
SLIDE 35

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

infinite stream

slide-36
SLIDE 36

Back pressure:

request(n), write, flush, repeat

slide-37
SLIDE 37

HTTP GET with streaming response

Simply return reactive type (Flux, Observable) from @Controller Back pressure on Spring MVC and WebFlux

slide-38
SLIDE 38

Servlet Container Servlet API Spring MVC Flux

Back pressure against blocking OutputStream

Controller Flux Data Repository

  • nNext(T)
  • nNext(T)

Blocking write on MvcAsync thread pool

request(n) request(1)

SERVLET STACK …

slide-39
SLIDE 39

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

slide-40
SLIDE 40

HTTP Server Server Adapter Flux Spring WebFlux Flux

Response streaming on reactive stack

Controller Flux Data Repo

  • nNext(T)
  • nNext(T)
  • nNext(T)

Non-blocking write back pressure to socket

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

slide-41
SLIDE 41

Demo

slide-42
SLIDE 42

Use Case #3

remote service

  • r

c h e s t r a t i

  • n

Reactive

slide-43
SLIDE 43

Demo

slide-44
SLIDE 44

Orchestrate non-blocking, nested remote service calls with ease Similar to reactive data access Spring MVC and Spring WebFlux

Reactive WebClient

slide-45
SLIDE 45
slide-46
SLIDE 46
slide-47
SLIDE 47

Use Case #4

request input Reactive

slide-48
SLIDE 48

Back pressure to socket

No reading until reactive demand signalled from upstream Non-blocking Reactive stack only territory !

slide-49
SLIDE 49

HTTP POST with data

@RequestBody argument with reactive type (Mono, Single) Reactive type is not required

slide-50
SLIDE 50

Use Case #5

Data Ingestion back pressure

w i t h

slide-51
SLIDE 51

Media type indicates infinite stream is expected Non-blocking streaming + back pressure

HTTP POST with stream of data

slide-52
SLIDE 52

HTTP Server Server Adapter Flux Spring WebFlux Flux

Data ingestion on reactive stack

Controller

  • nNext(T)
  • nNext(T)

Non-blocking read back pressure from socket

request(n) request(n)

slide-53
SLIDE 53

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

slide-54
SLIDE 54

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

slide-55
SLIDE 55

Q & A

@rstoya05