Dreaming of Streaming with Reactive Programming

Dreaming of Streaming with Reactive Programming

Reactive programming, Reactive Extensions, Reactive Streams, reactive systems, Reactive Manifesto… Do you know what they mean? Do you know why it matters?

Learn about reactive and why it matters in this new cloud-native era. We’ll use live coding demos to explain how to be reactive and benefit from this brave new streaming world.

14493d3489b1441918bfddfe298415d9?s=128

Edson Yanaga

July 24, 2019
Tweet

Transcript

  1. !1 Dreaming of Streaming with Reactive Programming Edson Yanaga, Red

    Hat @yanaga
  2. !2 A stack to write Java apps Cloud Native Microservices

    Serverless
  3. !3 Developer Joy Quarkus Benefits Supersonic Subatomic Java Unifies imperative

    and reactive Best of breed libraries and standards
  4. System eXtensions Programming Manifesto Streams Asynchrony Resilience Elasticity Back-Pressure Spreadsheets

    Actor Events Messages Data Flows RX Java Responsiveness Fibers Non-Blocking Reactor Spring MicroProfile REACTIVE ALL THE THING
  5. reactive | rēˈaktiv | adjective showing a response to a

    stimulus: pupils are reactive to light. • acting in response to a situation rather than creating or controlling it: a proactive rather than a reactive approach. • having a tendency to react chemically: nitrogen dioxide is a highly reactive gas. • Physiology showing an immune response to a specific antigen. ...
  6. reactive | rēˈaktiv | adjective showing a response to a

    stimulus: pupils are reactive to light. • acting in response to a situation rather than creating or controlling it: a proactive rather than a reactive approach. • having a tendency to react chemically: nitrogen dioxide is a highly reactive gas. • Physiology showing an immune response to a specific antigen. ... • Software application reacting to stimuli such as user inputs, messages, and failures
  7. Events Messages Failures Is Reactive == Event-Driven ?

  8. Build concurrent applications Reactive programming Reactive streams Non-Blocking Asynchronous Event-Driven

    => Concurrent Applications Non-Blocking IO (Netty) Reactive Framework Application Code Callbacks, RX Java, Reactor, Co-routines... } 1 Thread -> n concurrent tasks
  9. FROM EVENT-DRIVEN TO SYSTEMS

  10. Build responsive distributed systems Reactive Systems Elasticity, Resilience, Messages Build

    concurrent applications Reactive programming Reactive streams Non-Blocking Asynchronous TWO SIDES OF THE SAME COIN
  11. REACTIVE => RESPONSIVE

  12. Asynchronous Message Passing REACTIVE => RESPONSIVE

  13. Async != multi-thread Async == never-block

  14. FROM HTTP 
 TO MESSAGING

  15. HTTP => STRONG COUPLING

  16. HTTP => UPTIME COUPLING

  17. CB CB CB CB CB CB THE RISE OF CIRCUIT

    BREAKERS
  18. Non-Blocking Message Passing Elasticity Resilience THE BENEFITS OF MESSAGING

  19. Message Stream Publish/ Subscribe Request-Reply DATA AT REST => DATA

    IN MOTION
  20. Supersonic. Subatomic. REACTIVE.

  21. Messaging / Streaming Quarkus Application Code Container Host / Operating

    System Async HTTP Messaging Quarkus Application Code Container Host / Operating System Messaging Middleware QUARKUS = {HTTP, MESSAGING, STREAMING}
  22. MicroProfile Reactive Messaging Quarkus Application Code Container Host / Operating

    System Async HTTP Messaging Quarkus Application Code Container Host / Operating System Messaging Middleware QUARKUS = {HTTP, MESSAGING, STREAMING}
  23. Coffee Shop Barista Coffee Shop Barista HTTP Board Kafka REACTIVE

    COFFEE SHOP https://github.com/cescoffier/quarkus-coffeeshop-demo Orders, Queue
  24. Coffee Shop Barista HTTP WHAT’S THE PROBLEM WITH SYNCHRONOUS On

    a peak of load • Concurrency limited by the coffee shop thread pool • Must scale up the number of baristas • Require some kind of load-balancer • COLD COFFEE or NO COFFEE On failing barista • Orders are lost • NO COFFEE On slow barista • Coffee Shop gets slow • COLD COFFEE
  25. @POST @Path("/http") public Beverage order(Order order) { return client.order(order); }

    @POST @Path("/async") public CompletionStage<Beverage> order(Order order) { return client.orderAsync(order); } FROM SYNC TO ASYNC HTTP Non-Blocking Thread released @POST @Produces("application/json") @Consumes("application/json") Beverage order(Order order); @POST @Produces("application/json") @Consumes("application/json") CompletionStage<Beverage> orderAsync(Order order);
  26. Coffee Shop Barista DEMO HTTP

  27. Bridge imperative -> reactive DROP HTTP, SEND MESSAGES @Inject @Stream("orders")

    Emitter<String> orders; public Order enqueue(Order order) { order.setOrderId(UUID.randomUUID().toString()); orders.send(jsonb.toJson(order)); return order; } Send “data” to the channel “orders”
  28. @Incoming("orders") @Outgoing("queue") public CompletionStage<String> prepare(String message) { Order order =

    fromJson(message, Order.class); return makeIt(order) .thenApply(beverage -> PreparationState.ready(order, beverage) ); } Process data async* * Many other options available to produce, process or consume data Read from “orders” Write to “queue” PROCESSING MESSAGES
  29. Coffee Shop Barista Board Queue Orders PROGRESS

  30. @Inject @Stream("beverages") Publisher<String> queue; @GET @Produces(MediaType.SERVER_SENT_EVENTS) public Publisher<String> getQueue() {

    return queue; } Inject a stream Returns a stream PUSHING DATA TO SSE
  31. Coffee Shop Barista Board Queue Orders DEMO

  32. mp.messaging.smallrye-kafka.bootstrap.servers=localhost:9092 mp.messaging.smallrye-kafka.key.serializer=...StringSerializer mp.messaging.smallrye-kafka.value.serializer=...StringSerializer mp.messaging.outgoing.queue.connector=smallrye-kafka mp.messaging.outgoing.queue.acks=1 Direction MAPPING CHANNELS TO KAFKA

    TOPICS Channel Name 
 (= topic if not set)
  33. Coffee Shop Barista Board Queue Orders BARISTAS TAKE BREAKS

  34. Coffee Shop Baristas (Julie and Tom) Queue Orders RUSH HOUR!

    Board
  35. SUMMARY

  36. HTTP, Messaging, Streaming Quarkus Non-Blocking IO QUARKUS = {HTTP, MESSAGING,

    STREAMING}
  37. WHAT ABOUT REACTIVE STREAMS? @Stream(“foo”) Emitter @Incoming(“foo”) @Outgoing(“bar”) @Stream(“bar”) Publisher

    @Outoing(“foo”) @Incoming(“bar”) Publisher Processor Subscriber Connector Connector
  38. BRIDGE REACTIVE AND IMPERATIVE @Stream(“foo”) Emitter @Incoming(“foo”) @Outgoing(“bar”) @Stream(“bar”) Publisher

    @Outoing(“foo”) @Incoming(“bar”) Publisher Processor Subscriber Connector Connector
  39. Non-Blocking Message Passing Elasticity Resilience REACTIVE SYSTEMS MADE EASY

  40. Quark us Imperativ e Reactive CRUD, REST, CLI... Reactiv e

    System s Data Streamin g Supersonic, Subatomic, REACTIVE
  41. !41 Thank you.