s e r v l e t v s r e a c t i v e
play

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


  1. stacks S e r v l e t v s R e a c t i v e in 5 use cases Rossen Stoyanchev

  2. Servlet Stack Reactive Stack ● Servlet container ● Netty, Servlet 3.1+, Undertow ● Servlet API ● Reactive Streams ● Spring MVC ● Spring WebFlux

  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…

  4. Servlet Stack

  5. SERVLET STACK Servlet API Spring Controller Filter Filter container MVC thread

  6. SERVLET STACK Synchronous API Filter , Servlet … void

  7. SERVLET STACK Blocking I/O InputStream , OutputStream

  8. SERVLET STACK servletRequest.startAsync()

  9. SERVLET STACK SERVLET STACK container- Controller Filter Filter Servlet thread-1 … do work or receive event + dispatch() … container- Controller Filter Filter Servlet thread-2

  10. SERVLET STACK startAsync() Controller Servlet API can use reactive clients Container thread pool Input & OutputStream

  11. Concurrency Concurrency Concurrency models Concurrency

  12. Synchronous APIs Non-blocking code “elastic” “parallel” thread pool thread pool 100s, 1000s ~ per CPU core waiting blocked threads busy worker threads

  13. What does it take to not block ?

  14. event loop at the core

  15. event driven architecture message passing

  16. means to compose async logic

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

  18. Reactive Stack

  19. REACTIVE STACK HTTP Server Reactive Event Loop Spring Web Web Controller Server WebFlux Filter Filter Adapter NO BLOCKING ANY TIME

  20. REACTIVE STACK Asynchronous API WebFilter , WebHandler … Mono<Void>

  21. REACTIVE STACK Reactor Mono Reactive Streams Publisher 0..1 elements

  22. REACTIVE STACK Non-blocking read : Flux<DataBuffer> getBody()

  23. REACTIVE STACK Non-blocking write : writeWith(Flux<DataBuffer>)

  24. REACTIVE STACK Reactor Flux Reactive Streams Publisher 0..N elements

  25. Reactive Streams back pressure request(1) request(1) Flux onNext(item) onNext(item)

  26. REACTIVE Composition of async logic STACK A B C HTTP Server Event Loop Spring Controller WebFilter WebFlux F E D request(n) Actual processing A B C D E F Mono Mono Mono Mono Flux onNext(item)

  27. Use Case #1 Reactive data repository

  28. Demo

  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

  30. Flux<T> : finite collection or infinite stream?

  31. Use media type to decide

  32. “application/json” finite collection (JSON array)

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

  34. Use Case #2 R e s p o n s e s t r e a m with back pressure

  35. “text/event-stream”, “application/stream+json” infinite stream

  36. Back pressure: request(n), write, flush, repeat

  37. HTTP GET with streaming response Simply return reactive type ( Flux , Observable ) from @Controller Back pressure on Spring MVC and WebFlux

  38. SERVLET STACK … Back pressure against blocking OutputStream request(1) request(n) Data Servlet Servlet Spring Controller Flux Flux Repository Container API MVC Blocking write on onNext(T) onNext(T) MvcAsync thread pool

  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

  40. Response streaming on reactive stack request(n) request(n) request(n) Server HTTP Data Spring Controller Flux Flux Flux Server Adapter Repo WebFlux Non-blocking write back pressure onNext(T) onNext(T) onNext(T) to socket

  41. Demo

  42. Use Case #3 remote service Reactive o r c h e s t r a t i o n

  43. Demo

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

  45. Use Case #4 Reactive request input

  46. Back pressure to socket No reading until reactive demand signalled from upstream Non-blocking Reactive stack only territory !

  47. HTTP POST with data @RequestBody argument with reactive type ( Mono , Single ) Reactive type is not required

  48. Use Case #5 Data Ingestion w i t h back pressure

  49. HTTP POST with stream of data Media type indicates infinite stream is expected Non-blocking streaming + back pressure

  50. Data ingestion on reactive stack Non-blocking read onNext(T) onNext(T) back pressure from socket HTTP Server Spring Controller Flux Flux Server Adapter WebFlux request(n) request(n)

  51. 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

  52. 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

  53. Q & A @rstoya05

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend