Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reactive Spring 5: WebFlux & Reactor

Reactive Spring 5: WebFlux & Reactor

Filip Maelbrancke

October 24, 2018
Tweet

More Decks by Filip Maelbrancke

Other Decks in Programming

Transcript

  1. Imperative programming expression is evaluated once value is assigned to

    a variable Reactive programming is all about responding to value changes
  2. You have probably done some reactive programming, even if you

    didn’t realise it at that time: - Defining cell values in spreadsheets is similar to defining variables in imperative programming - Defining cell expressions in spreadsheets is similar to defining and operating on reactive types Reactive programming
  3. Previous example in spreadsheet: - assign cell B1 with value

    of 2 - assign cell B2 with value of 3 - assign cell B3 with an expression that multiplies B1 value with B2 value - when value of either referenced component in the expression changes -> expression is re-evaluated automagically in B3
  4. Spring 5 Embraced different perspective on modern day web architecture

    Spring WebFlux = parallel web architecture next to the servlet-based Spring MVC
  5. Load test milliseconds (95th percentile) 0 500 1000 1500 2000

    2500 3000 3500 4000 Concurrent users 1 10 100 200 500 1000 2000 Synchronous Reactive (500ms backend service)
  6. Load test requests served per second (parallellism : 100) 0

    25 50 75 100 1 8 32 96 768 Blocking WebFlux (Reactive)
  7. Dutch computer scientist Erik Meijer @Microsoft: C#, LINQ Reactive Extensions

    (RX) Currently Director Of Engineering @ Facebook Bearer of extremely cool tie-dyed shirts reactivex.io
  8. Reactive Manifesto To be reactive, according to The Reactive Manifesto,

    you have to be Responsive, Resilient, Elastic, and Message Driven. The last criteria in this list caused big movement into the asynchronous way of communications.
  9. Goal of Reactive Streams exchange of data across an asynchronous

    boundary while ensuring that the receiving side is not forced to buffer arbitrary amounts of data (back pressure)
  10. Java 9 2015 Start Reactive Streams 1.0 2016 Growth Akka,

    Spring, Pivotal Project Reactor, RxJava, MongoDB, Kafka, … 2017 Java The interfaces available in JDK9’s java.util.concurrent.Flow, are 1:1 semantically equivalent
  11. Reactive core Typed [0|1|N] sequences Non-blocking IPC non-blocking foundation
 interacts

    with Java 8
 functional API, Completable
 Future, Streams reactive composable API
 Flux[N]
 Mono[0/1]
 implements Reactive Extensions suited for microservices architecture
 backpressure-ready network engines
 (HTTP / Websockets / TCP / UDP)
 reactive encoding/decoding Reactor
  12. When you shift to asynchronous code, things can get more

    complicated: - Ugly stack traces - Missing async call chain - Stack pollution Debugging
  13. java.lang.ArithmeticException: / by zero at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:115) at reactor.core.publisher.FluxJust$WeakScalarSubscription.request(FluxJust.java:99)

    at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.BlockingSingleSubscriber.onSubscribe(BlockingSingleSubscriber.java:49) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxJust.subscribe(FluxJust.java:70) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.Flux.subscribe(Flux.java:6873) at mycode.test(MyTest.java:xx) Debugging
  14. Servlet Container Netty, Tomcat, Jetty, Undertow HTTP / Reactive Streams

    spring-webflux Servlet API spring-webmvc Router Functions @Controller, @RequestMapping Spring 5
  15. SNTP time Client time Server 135ms 137ms 298ms 231ms t1

    t2 t0 t3 δ=65ms time offset ϑ = (t1 − t0 ) + (t2 − t3 ) 2 round − trip delay δ = (t3 − t0 ) − (t2 − t1 )
  16. DNS Resolution IP1 IP2 IP3 IP4 SNTP Request 1 NTP

    POOL Least Roundtrip Delay SNTP Request 2 SNTP Request 3 SNTP Request 4 SNTP Request 5 Statistical analysis Sort by clock offset + median NTP Time Best possible approximation of 5 . . .
  17. DNS Resolution IP1 IP2 IP3 IP4 SNTP Request 1 NTP

    POOL Least Roundtrip Delay SNTP Request 2 SNTP Request 3 SNTP Request 4 SNTP Request 5 Statistical analysis Sort by clock offset + median NTP Time Best possible approximation of 5 . . .
  18. Reactor is so so… Reactor is not the holy grail.

    Should be used for streams processing, for general asynchronous non-blocking computation, see coroutines (Spring 5.2+)
  19. Reactor has downsides? Mindshift (also when testing)
 Debugging & stacktraces

    are harder For the DDD people: (small) pollution of your domain with Mono/Flux
  20. request / stream finite stream of many RSocket request /

    response stream of 1 fire and forget channel no response bi-directional streams