45
Servlet vs Reactive Choosing the Right Stack Rossen Stoyanchev

Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

  • Upload
    others

  • View
    26

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Servlet vs ReactiveChoosing the Right Stack

Rossen Stoyanchev

Page 2: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Servlet and Reactive stacksSpring Framework 5

This talk

Big shift towards asyncLearn about the options, make choices

Page 3: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Spring Framework committer

Web development

Spring WebFlux from inception to GA

About me

Page 4: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

(immediately after this session)

Also featuring:

Ask Me Anything

Phil Webb → Spring Boot lead

Stephane Maldini → Reactor lead

Page 5: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Motivation for change

Page 6: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

??

Page 7: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Asynchronicity

Thread pools

Page 8: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Event loopCoroutines

ActorsAsynchronicity

Thread pools

Fibers / Project Loom

Page 9: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Small, fixed # of threads(running)

100s, 1000s threads(blocked)

Servlet Reactive

Page 10: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Non-blocking concurrency

Even Loop

Page 11: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Declarative composition of asynchronous logic

CompletableFuture, ReactiveX, Reactor, …

Page 12: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Java 8 lambdasFunctional programming models

Page 13: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Stack choicesand why choice matters

Page 14: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Servlet Reactive

@Controller @Controller

Page 15: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Servlet Reactive

@Controller

Reactive client

@Controller

Reactive client

Page 16: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Servlet Reactive

Functionalendpoint

@Controller

Reactive client

@Controller

Reactive client

Page 17: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Servlet Reactive

Functionalendpoint

@Controller

Reactive client

@Controller

Reactive client

Tomcat Jetty

Servlet 3.1 container

Tomcat Jetty

Servlet container

Page 18: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Servlet Reactive

Functionalendpoint

@Controller

Reactive client

@Controller

Reactive client

Netty

Undertow

Tomcat Jetty

Servlet 3.1 container

Tomcat Jetty

Servlet container

Page 19: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Demo

Page 20: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Stack architecture

Page 21: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Tomcat, Jetty, …

Servlet API

Spring MVC

Blocking I/O

Page 22: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

History of Servlet API

1997 1.0

… …

2009 3.0 Async Servlet

2009 3.1 Servlet non-blocking I/O

… …

Page 23: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Tomcat, Jetty, …

Servlet API

Blocking I/O

Controller +Reactive client

Async Servlet

Page 24: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

History of Servlet API

1997 1.0

… …

2009 3.0 Async Servlet

2009 3.1 Servlet non-blocking I/O

… …

Page 25: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Tomcat, Jetty, …

Servlet API

Blocking I/O

Controller +Reactive client

Async Servlet

Page 26: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Tomcat, Jetty, … NettyTomcat, Jetty, …

Servlet API

Blocking I/O Servlet 3.1

Non-blocking I/O

Reactor, Reactive Streams

ReactorNetty

Spring Web API

Controller +Reactive client

Async Servlet

Page 27: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Tomcat, Jetty, … NettyTomcat, Jetty, …

Servlet API

Blocking I/O

Spring WebFluxSpring MVC

Non-blocking I/O

Reactor, Reactive Streams

Spring Web API

Page 28: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Reactive, non-blocking

back pressure

Page 29: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Blocking expected

Filter ServletFilter

Tomcat, Jetty, …

Page 30: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Blocking not expected

WebFilter WebHandler

Tomcat, Jetty, … Netty

Even Loop

WebFilter

Page 31: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

WebFilter, WebHandlerMono<Void>

Page 32: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Request

Flux<DataBuffer> getBody()

Page 33: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Response

writeWith(Flux<DataBuffer>)

Page 34: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Codecs

Flux<DataBuffer> Flux<T>

Page 35: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

WebFilter WebHandlerWebFilter

MonoMonoMono Mono

onNext(T)

request(n)

A B C

DE

A B C D E

Page 36: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

HTTPServer

ReactiveAdapter

FluxSpring

WebFluxFlux Controller Flux Web

Client

Non-blocking write

WebFlux Response

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

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

Page 37: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

ServerReactiveAdapter

FluxSpring

WebFluxFlux Controller Flux

DataRepo

Non-blocking read

WebFlux Request

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

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

Page 38: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

ServerServlet

APISpring MVC

Flux Controller FluxWebClient

Blocking write

Spring MVC thread

request(1) request(1)

onNext(T) onNext(T)

Spring MVC Response

Page 39: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Demo

Page 40: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

ChoosingThe right stack for the job at hand

Page 41: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Spring MVC

If it ain’t broken, don’t fix it

Imperative logic is the easiest to write and debug

Check application dependencies (JDBC, JPA)

Page 42: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Spring MVC + Reactive Client

Compose remote service calls

Reactive data (Mongo, Cassandra, Redis)

Response stream

Page 43: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Spring WebFlux

Use cases → easy to stream up or down

Scale → efficient at I/O

Programming model → functional endpoints

Page 44: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Spring WebFlux Performance

Efficient scale with less hardware resources

It’s not about speed

Latency is key to exhibit differences

Page 45: Choosing the Right Stack Servlet vs Reactive...Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards async Learn about the options, make choices Servlet Reactive

Thank You

rstoya05

https://github.com/rstoyanchev/demo-reactive-spring