Slide 1

Slide 1 text

INTRODUCTION TO REACTIVE STREAM Bazlur Rahman | @bazlur_rahman

Slide 2

Slide 2 text

AGENDA Context: A bit of history of Java Concurrency in General Reactive Stream: why? Comparison between regular stream vs. reactive stream Demo 2

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

A T Y P I C A L W E B A P P L I C A T I O N 4

Slide 5

Slide 5 text

AN EXAMPLE 5

Slide 6

Slide 6 text

CLASSICAL IMPLEMENTATION Using Plain oldThread: 6

Slide 7

Slide 7 text

7

Slide 8

Slide 8 text

LET’S IMPROVE IT A BIT • Java 5 introduced Executors with • Future • Callable/Runnable 8

Slide 9

Slide 9 text

A SIGNIFICANT IMPROVEMENT 9

Slide 10

Slide 10 text

10

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

LET’S INTRODUCE COMPOSSIBILITY Java 8 Introduce CompletableFuture, which is awesome 13

Slide 14

Slide 14 text

14

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

REACTIVE STREAM • It introduces a non Blocking Backpressure for asynchronous stream processing 17

Slide 18

Slide 18 text

WHAT IS REACTIVE ANYWAY? • It is about creating a software component/architecture that supports https://www.reactivemanifesto.org/ 18

Slide 19

Slide 19 text

FUNCTIONAL PROGRAMMING • Functional programming = declarative + higher-order + pure function • It enables us to • Functional composition. • Lazy evaluations • Enforce immutability 19

Slide 20

Slide 20 text

REACTIVE PROGRAMMING • Reactive programming is functional programming++ 20

Slide 21

Slide 21 text

STREAM PROGRAMMING BASICS 21

Slide 22

Slide 22 text

DEMO 01 • Basic Reactive Stream 22

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

DEMO 02 24

Slide 25

Slide 25 text

REACTIVE STREAM API Publishers Subscribers Subscriptions Processors https://www.reactive-streams.org/ 25

Slide 26

Slide 26 text

REACTIVE STREAM IMPLEMENTATION • RxJava • Reactor • Akka Stream • Ratpack • Vert.x 26

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

THANK YOU 29