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

Reactor 3

Simon Baslé
February 22, 2017

Reactor 3

Reactor 3, a reactive foundation for the JVM

Introduction to reactive programming, Reactor 3 and a bit of Spring 5.

(note: presentation will be re-uploaded if it is updated, so it might not 100% correspond to the version I used at talks in the past)

Simon Baslé

February 22, 2017
Tweet

More Decks by Simon Baslé

Other Decks in Programming

Transcript

  1. Reactor 3
    a Reactive foundation for
    the JVM

    View full-size slide

  2. About me
    &
    how to get in touch
    @SimonBasle

    View full-size slide

  3. First
    a
    Survey

    View full-size slide

  4. Who Here Uses...

    View full-size slide

  5. Who Here Uses...
    Java 8

    View full-size slide

  6. Who Here Uses...
    Java 8
    RxJava

    View full-size slide

  7. Who Here Uses...
    Java 8
    RxJava
    Reactive Streams

    View full-size slide

  8. Who Here Thinks...
    Reactor is a fork of Atom editor

    View full-size slide

  9. ok bad joke, done

    View full-size slide

  10. 101
    Reactive
    Programming

    View full-size slide

  11. 101
    Reactive
    Programming
    types &
    operators
    Reactor 3

    View full-size slide

  12. 101
    Reactive
    Programming
    types &
    operators
    Reactor 3
    other beasts
    backpressure
    and

    View full-size slide

  13. 101
    Reactive
    Programming
    types &
    operators
    Reactor 3
    debugging
    testing
    and
    other beasts
    backpressure
    and

    View full-size slide

  14. 101
    Reactive
    Programming
    types &
    operators
    Reactor 3
    and Spring
    Reactor
    debugging
    testing
    and
    other beasts
    backpressure
    and

    View full-size slide

  15. 101
    Reactive
    Programming
    types &
    operators
    Reactor 3
    and Spring
    Reactor
    debugging
    testing
    and
    reactor-netty
    other beasts
    backpressure
    and
    reactor-kafka...

    View full-size slide

  16. Reactive Programming 101
    what does it bring to the table?

    View full-size slide

  17. WHY?
    because blocking is evil

    View full-size slide

  18. sync/blocking
    main thread processing
    resumes
    I/O
    ! app does
    nothing

    View full-size slide

  19. sync/blocking
    main thread processing
    resumes
    I/O
    BAD
    ! app does
    nothing

    View full-size slide

  20. async & blocking
    main thread wait &
    join
    ! new
    threads,
    costly
    ! complex

    View full-size slide

  21. async & blocking
    main thread wait &
    join
    ! new
    threads,
    costly
    ! complex
    BAD

    View full-size slide

  22. async & nonblocking
    “event loop”
    in non-blocking
    processing
    chunks
    no more threads
    than needed

    View full-size slide

  23. how do you achieve that
    without losing your mind ?

    View full-size slide

  24. Reactive Programming

    View full-size slide

  25. Composing asynchronous
    & event-based sequences,
    using non-blocking operators


    View full-size slide

  26. without sacrifice

    View full-size slide

  27. without sacrifice
    Callbacks ?
    Futures ?
    easy to block
    hard to compose
    callback hell !
    not readable

    View full-size slide

  28. Pull? Push!
    (or actually a little bit of Both)

    View full-size slide

  29. vs
    Iterable
    -
    Iterator
    Publisher
    -
    Subscriber

    View full-size slide

  30. Data in Flux

    View full-size slide

  31. Publisher Subscriber
    push events
    produces consumes
    feedback

    View full-size slide

  32. interfaces from
    Reactive Streams
    spec
    Publisher Subscriber
    feedback
    consumes
    push events
    produces

    View full-size slide

  33. Publisher Subscriber
    push events
    produces consumes
    feedback
    Subscriber
    onNext(T)
    onComplete();
    onError(Throwable);

    View full-size slide

  34. Publisher Subscriber
    produces consumes
    feedback
    0..N elements
    +
    0..1 (complete | error)

    View full-size slide

  35. Publisher Subscriber
    push events
    produces consumes
    feedback
    backpressure

    View full-size slide

  36. Publisher Subscriber
    push events
    produces consumes
    feedback
    can I have an
    API though?

    View full-size slide

  37. Publisher Subscriber
    push events
    produces consumes
    feedback

    View full-size slide

  38. Reactor 3
    types and operators

    View full-size slide

  39. Flux
    for 0..N elements

    View full-size slide

  40. Mono
    for at most 1 element

    View full-size slide

  41. Reactive Streams
    all the way

    View full-size slide

  42. focus on Java 8

    View full-size slide

  43. focus on Java 8
    Duration, CompletableFuture, Streams

    View full-size slide

  44. an Rx-inspired API
    with a vocabulary of operators similar to RxJava...

    View full-size slide

  45. an Rx-inspired API
    ...but not exactly the same

    View full-size slide

  46. Flux/Mono
    generator
    operator
    operator
    operator
    nothing
    happens
    until you
    subscribe

    View full-size slide

  47. Flux/Mono
    generator
    Subscriber
    operator
    operator
    operator
    nothing
    happens
    until you
    subscribe

    View full-size slide

  48. Flux/Mono
    generator
    Subscriber
    operator
    operator
    operator
    per
    Subscription
    state
    Sub
    Sub
    Sub

    View full-size slide

  49. Flux/Mono
    generator
    Subscriber
    operator
    operator
    operator
    data
    flows
    Sub
    Sub
    Sub

    View full-size slide

  50. Flux.range
    Subscriber
    map
    filter
    buffer

    View full-size slide

  51. Flux.range
    Subscriber
    map
    filter
    buffer

    View full-size slide

  52. Flux.range
    Subscriber
    map
    filter
    buffer

    View full-size slide

  53. Flux.range
    Subscriber
    map
    filter
    buffer

    View full-size slide

  54. Flux.range
    Subscriber
    map
    filter
    buffer

    View full-size slide

  55. Flux.range
    Subscriber
    map
    filter
    buffer

    View full-size slide

  56. Flux.range
    Subscriber
    map
    filter
    buffer

    View full-size slide

  57. Flux.range
    Subscriber
    map
    filter
    buffer

    View full-size slide

  58. Flux.range(5, 3)
    .map(i -> i + 3)
    .filter(i -> i % 2 == 0)
    .buffer(3)

    View full-size slide

  59. Flux.range(5, 3)
    .map(i -> i + 3)
    .filter(i -> i % 2 == 0)
    .buffer(3)
    5, 6, 7 |
    8, 9, 10 |
    8, 10 |
    [8,10]|

    View full-size slide

  60. Flux.from
    Subscriber
    map
    filter
    retry
    Publisher from
    HTTP reactive
    client

    View full-size slide

  61. Flux.from
    Subscriber
    map
    filter
    retry
    Publisher from
    HTTP reactive
    client

    View full-size slide

  62. Flux.from
    Subscriber
    map
    filter
    retry
    Publisher from
    HTTP reactive
    client
    resubscribe

    View full-size slide

  63. go DEEPER!
    async sub-processes with flatMap

    View full-size slide

  64. flatMap(user -> tweetStream(user))

    View full-size slide

  65. flatMap(user -> tweetStream(user))

    View full-size slide

  66. flatMap(user -> tweetStream(user))

    View full-size slide

  67. flatMap(user -> tweetStream(user))

    View full-size slide

  68. & much
    more...

    View full-size slide

  69. threading
    contexts

    View full-size slide

  70. Reactor
    is
    agnostic

    View full-size slide

  71. however it
    facilitates switching

    View full-size slide

  72. Schedulers
    elastic, parallel, single, timer...

    View full-size slide

  73. publishOn
    switch rest of the flux on a thread

    View full-size slide

  74. subscribeOn
    make the subscription and request happen
    on a particular thread

    View full-size slide

  75. Flux/Mono
    generator
    operator
    subscribeO
    n
    operator
    publishOn
    operator
    operator
    Subscriber
    Sub
    Sub
    Sub
    Sub
    Sub
    Sub

    View full-size slide

  76. Flux/Mono
    generator
    operator
    subscribe
    On
    operator
    publish
    On
    operator
    operator
    Subscriber
    Sub
    Sub
    Sub
    Sub
    Sub
    Sub

    View full-size slide

  77. Flux/Mono
    generator
    operator
    subscribeO
    n
    operator
    publishOn
    operator
    operator
    Subscriber
    Sub
    Sub
    Sub
    Sub
    Sub
    Sub

    View full-size slide

  78. Flux/Mono
    generator
    operator
    subscribeO
    n
    operator
    publishOn
    operator
    operator
    Subscriber
    Sub
    Sub
    Sub
    Sub
    Sub
    Sub

    View full-size slide

  79. Flux/Mono
    generator
    operator
    subscribeO
    n
    operator
    publishOn
    operator
    operator
    Subscriber
    Sub
    Sub
    Sub
    Sub
    Sub
    Sub

    View full-size slide

  80. Flux/Mono
    generator
    operator
    subscribeO
    n
    operator
    publishOn
    operator
    operator
    Subscriber
    Sub
    Sub
    Sub
    Sub
    Sub
    Sub

    View full-size slide

  81. Testing & Debugging
    in an asynchronous world

    View full-size slide

  82. Testing a Publisher
    StepVerifier

    View full-size slide

  83. Testing a Publisher
    with Virtual Time support

    View full-size slide

  84. Simulate a source
    TestPublisher

    View full-size slide

  85. Debugging Issues
    stacktraces get hard to decipher

    View full-size slide

  86. usually just show
    where Subscription happens

    View full-size slide

  87. java.lang.IndexOutOfBoundsException: Source emitted more than one item
    at reactor.core.publisher.MonoSingle$SingleSubscriber.onNext(MonoSingle.java:120)
    at
    reactor.core.publisher.FluxOnAssembly$OnAssemblySubscriber.onNext(FluxOnAssembly.java:314)
    ...
    ...
    at reactor.core.publisher.Mono.subscribeWith(Mono.java:2668)
    at reactor.core.publisher.Mono.subscribe(Mono.java:2629)
    at reactor.core.publisher.Mono.subscribe(Mono.java:2604)
    at reactor.core.publisher.Mono.subscribe(Mono.java:2582)
    at reactor.guide.GuideTests.debuggingActivated(GuideTests.java:727)

    View full-size slide

  88. Find where the Flux
    was instantiated (assembly)

    View full-size slide

  89. Checkpoint()
    or full assembly tracing

    View full-size slide

  90. costly!
    Checkpoint()
    or full assembly tracing

    View full-size slide

  91. Assembly trace from producer [reactor.core.publisher.MonoSingle] :
    reactor.core.publisher.Flux.single(Flux.java:5335)
    reactor.guide.GuideTests.scatterAndGather(GuideTests.java:689)
    reactor.guide.GuideTests.populateDebug(GuideTests.java:702)

    View full-size slide

  92. BACKPRESSURE
    and other beasts

    View full-size slide

  93. Publisher Subscriber
    subscribe

    View full-size slide

  94. Publisher Subscriber
    push data as fast as possible

    View full-size slide

  95. Publisher Subscriber
    subscribe
    with small request
    (eg. 1)

    View full-size slide

  96. Publisher Subscriber
    1 onNext

    View full-size slide

  97. Publisher Subscriber
    request more
    (eg. 2)

    View full-size slide

  98. Publisher Subscriber
    2 onNext

    View full-size slide

  99. Publisher Subscriber
    backpressure

    View full-size slide

  100. other ways of dealing with backpressure
    eg. drop, buffer...

    View full-size slide

  101. internal
    optimisations

    View full-size slide

  102. macro FUSION
    avoids unnecessary request back-and-forth

    View full-size slide

  103. micro FUSION
    share internal structures for less allocation

    View full-size slide

  104. lock free operators

    View full-size slide

  105. lock free operators
    and Work Stealing
    CPU 1
    CPU 2
    CPU 3
    CPU 4
    CPU 5

    View full-size slide

  106. Reactor and
    Spring

    View full-size slide

  107. Reactor and
    Spring
    and do I need Spring to use Reactor ?

    View full-size slide

  108. NO philosoraptor you don’t

    View full-size slide

  109. Reactor 3
    is a dependency of
    Spring 5
    not the other way around

    View full-size slide

  110. Java 8
    baseline

    View full-size slide

  111. reactive
    focus

    View full-size slide

  112. new WEB stack
    WebFlux

    View full-size slide

  113. @RestController(“/user”)
    public class UserController {
    @GetMapping(“/{id}”)
    Mono getUser(String id) {...}
    }

    View full-size slide

  114. functional option
    for Routing

    View full-size slide

  115. Spring Data
    reactive repositories

    View full-size slide

  116. @GetMapping(“/{id}”)
    Mono getUser(String id) {
    return reactiveRepo.findOne(id);
    }

    View full-size slide

  117. Reactor and the Network
    reactor-netty

    View full-size slide

  118. reactor-netty
    builds on Netty to provide
    reactive I/O

    View full-size slide

  119. Client / Server

    View full-size slide

  120. Http
    and WebSockets

    View full-size slide

  121. HttpServer.create(0)
    .newHandler((in, out) -> out
    .sendWebsocket((i, o) ->
    o.options(opt -> opt.flushOnEach())
    .sendString(Flux.just("test")
    .delayElementsMillis(100)
    .repeat())
    )
    )
    .block();

    View full-size slide

  122. still a bit low level

    View full-size slide

  123. still a bit low level

    View full-size slide

  124. reactor-kafka
    topics as
    Flux

    View full-size slide

  125. reactive API
    over Kafka Producer / Consumer

    View full-size slide

  126. send(Flux)
    into Kafka

    View full-size slide

  127. Flux receive()
    from Kafka

    View full-size slide

  128. (currently in MILESTONE 2)

    View full-size slide

  129. Credits
    ● Springfield Plant: copyright FOX
    ● Raised Hand: CC0 (via Pixabay)
    ● Checklist: CC-By Crispy (via Flickr)
    ● Robot Devil: copyright FOX
    ● Volume Knob: CC0 (via Pixabay)
    ● Camel Shape: CC0 (via Pixabay)
    ● Dromedary Shape: CC-By-SA USPN,Whidou (via Wikimedia)
    ● Dam: CC-By-SA Matthew Hatton (via geograph.org.uk)
    ● Cogs: CC0 (via publicdomainpictures.net)
    ● Thread Balls: CC0 (via Pixabay)
    ● The Fortune Teller: Georges de la Tour (public domain)
    ● Microphone: CC0 (via Pexels)
    ● End Sands: CC0 (via Pixabay)
    ● logos: Pivotal, Spring, Twitter and Github logo copyright their
    respective companies.

    View full-size slide