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

Introduction to Reactive Stream

Introduction to Reactive Stream

In this talk, I explore what is reactive stream is and how it is beneficial and what are pros and cons.

A N M Bazlur Rahman

October 17, 2022
Tweet

More Decks by A N M Bazlur Rahman

Other Decks in Technology

Transcript

  1. AGENDA Context: A bit of history of Java Concurrency in

    General Reactive Stream: why? Comparison between regular stream vs. reactive stream Demo 2
  2. WHAT PROBLEM DO WE HAVE NOW? • Modern software application

    development requires- • High data scale, we are dealing with too much data • We have high usages of scale • We worry about costs- cloud costs • We don’t want to block our threads • Essentially blocking is our enemy 3
  3. A T Y P I C A L W E

    B A P P L I C A T I O N 4
  4. 7

  5. LET’S IMPROVE IT A BIT • Java 5 introduced Executors

    with • Future • Callable/Runnable 8
  6. 10

  7. BUT NOT THERE YET • Still, Future is quite law

    level • We need to call blocking get() call • It doesn’t have composability options. • We are doing asynchronous call, but in the end, we need blocking call (e.g from main thread) • The other thing is, when you have a new thread, it requires to get data from another threads, in which CPU needs point data to. Thread can run into different core, that could leads to cache corruption etc. Also context switching from one core to another core is expensive. 11
  8. LET’S IMPROVE IT MORE • Java 7 Introduce fork/Join pool

    • It’s an implementation of the ExecutorService • It understands that some task is dependent on other tasks. • It avoids changing threads • It helps us to prevent cache corruption • The assumptions: newly created tasks are likely to have in closer cache. • That indicates that the newly created task should run on the CPU and older tasks on another CPU • Each Thread has its own queue. • It steals tasks from another thread’s Queue from the tail when it has nothing left. • It gives us a huge performance boast, in fact, pretty much all reactive frameworks are built on top of it, even the project loom itself. 12
  9. 14

  10. COMPLETABLEFUTURE • It is quite a big class with more

    than 3000 lines of code • It has more than 50 methods which we can readily use and pretty much serves our all-purpose • A task can be combined, chained, and composed 15
  11. SO FAR SO GOOD • But so far, we have

    discussed how to deal with individual objects. • What do we have, LIVE data, a stream of data? • We can go far with the CompletableFuture to a certain point, but what if – • Producers produce data faster than the consumer can handle. • What do we do? • Drop the data • Buffer it • We can block • Or maybe we implement some communication mechanism between consumer and producer? • Well that’s where Reactive stream comes into play 16
  12. WHAT IS REACTIVE ANYWAY? • It is about creating a

    software component/architecture that supports https://www.reactivemanifesto.org/ 18
  13. FUNCTIONAL PROGRAMMING • Functional programming = declarative + higher-order +

    pure function • It enables us to • Functional composition. • Lazy evaluations • Enforce immutability 19
  14. J A V A S T R E A M

    V S R E A C T I V E S T R E A M Java Stream Reactive Stream Pipeline Pipeline Push Data Push Data Lazy Lazy Exceptions? Nothing we could do here Deal it downstream Error is another form of data Data only 3 channels– 1. Data channel 2. Error channel 3. Complete Zero, one or more data Zero, one or more data Sequential vs Parallel Synchronous vs Asynchronous Single pipeline Multiple subscriber 23
  15. CONS OF CREATIVE STREAM • Learning curve • Debugging is

    difficult • The reading experience is terrible; cognitive loads • Easy to over complicated things • Don’t treat it as a hammer, and don’t hit every nail you find 27
  16. WHAT ABOUT PROJECT LOOM • Well, loom doesn’t’ solve what

    we have here in reactive but only makes the blocking call cheap which is super cool • Allows us to write imperative code easy • It’s just a different concurrency model, less complicated, less learning curve • You do what you have already know, just do with cheap stuff 28