Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen...

45
Servlet vs Reactive Choosing the Right Stack Rossen Stoyanchev

Transcript of Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen...

Page 1: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Servlet vs ReactiveChoosing the Right Stack

Rossen Stoyanchev

Page 2: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Spring Framework committer

Web development

Spring WebFlux from inception to GA

About me

Page 4: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

(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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Motivation for change

Page 6: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

??

Page 7: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Asynchronicity

Thread pools

Page 8: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Event loopCoroutines

ActorsAsynchronicity

Thread pools

Fibers / Project Loom

Page 9: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Small, fixed # of threads(running)

100s, 1000s threads(blocked)

Servlet Reactive

Page 10: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Non-blocking concurrency

Even Loop

Page 11: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Declarative composition of asynchronous logic

CompletableFuture, ReactiveX, Reactor, …

Page 12: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Java 8 lambdasFunctional programming models

Page 13: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Stack choicesand why choice matters

Page 14: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Servlet Reactive

@Controller @Controller

Page 15: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Servlet Reactive

@Controller

Reactive client

@Controller

Reactive client

Page 16: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Servlet Reactive

Functionalendpoint

@Controller

Reactive client

@Controller

Reactive client

Page 17: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Demo

Page 20: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Stack architecture

Page 21: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Tomcat, Jetty, …

Servlet API

Spring MVC

Blocking I/O

Page 22: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Tomcat, Jetty, …

Servlet API

Blocking I/O

Controller +Reactive client

Async Servlet

Page 24: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Tomcat, Jetty, …

Servlet API

Blocking I/O

Controller +Reactive client

Async Servlet

Page 26: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Reactive, non-blocking

back pressure

Page 29: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Blocking expected

Filter ServletFilter

Tomcat, Jetty, …

Page 30: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Blocking not expected

WebFilter WebHandler

Tomcat, Jetty, … Netty

Even Loop

WebFilter

Page 31: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

WebFilter, WebHandlerMono<Void>

Page 32: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Request

Flux<DataBuffer> getBody()

Page 33: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Response

writeWith(Flux<DataBuffer>)

Page 34: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Codecs

Flux<DataBuffer> Flux<T>

Page 35: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Demo

Page 40: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

ChoosingThe right stack for the job at hand

Page 41: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Spring MVC + Reactive Client

Compose remote service calls

Reactive data (Mongo, Cassandra, Redis)

Response stream

Page 43: Choosing the Right Stack Servlet vs Reactive - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

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 - qconsf.com · Choosing the Right Stack Rossen Stoyanchev. Servlet and Reactive stacks Spring Framework 5 This talk Big shift towards

Thank You

rstoya05

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