Slide 1

Slide 1 text

Evolution of java: from object orientation to reactive applications with Webflux KAMILA SANTOS OLIVEIRA @kamilah_santos Global Summit for Java devs'20

Slide 2

Slide 2 text

@kamilah_santos Hi Folks,i’m Kamila Santos Backend Developer github.com/kamilahsantos in/kamila-santos-oliveira/ @kamilah_santos https://dev.to/kamilahsantos

Slide 3

Slide 3 text

@kamilah_santos Communities

Slide 4

Slide 4 text

@kamilah_santos Object-oriented programming, the basics

Slide 5

Slide 5 text

@kamilah_santos Abstraction We need to imagine what that object will do within our system

Slide 6

Slide 6 text

@kamilah_santos Abstraction - identity Identity to the object that we are going to create

Slide 7

Slide 7 text

@kamilah_santos Abstraction - attributes Characteristics of this object, the so-called attributes

Slide 8

Slide 8 text

@kamilah_santos Abstraction - methods This object will be able to perform actions, which we call methods

Slide 9

Slide 9 text

@kamilah_santos Encapsulation "protect / hide" objects from the rest of the application, we use getters and setters and access modifiers to define and access these values

Slide 10

Slide 10 text

@kamilah_santos Polymorphism We changed the inner workings of a method inherited from the parent object

Slide 11

Slide 11 text

@kamilah_santos inheritance daughter class inherits behaviors from parent class

Slide 12

Slide 12 text

@kamilah_santos Reactive Manifest

Slide 13

Slide 13 text

@kamilah_santos https://www.reactivemanifesto.org/

Slide 14

Slide 14 text

@kamilah_santos Responsive The system responds in a timely manner, if possible

Slide 15

Slide 15 text

@kamilah_santos Resilient The system remains responsive to failures

Slide 16

Slide 16 text

@kamilah_santos Elastic The system remains responsive in the face of a variable workload.

Slide 17

Slide 17 text

@kamilah_santos MESSAGE DRIVEN Reactive applications rely on asynchronous message passing to establish a limit between components, ensuring flexible coupling, isolation and transparency

Slide 18

Slide 18 text

@kamilah_santos Reactive streams - Java 9 Flowable API Initiative to provide a standard/guide/rules for asynchronous stream processing with non-blocking back pressure

Slide 19

Slide 19 text

interface Flow.Publisher methods to produce items and control signals @kamilah_santos

Slide 20

Slide 20 text

@kamilah_santos

Slide 21

Slide 21 text

interface Flow.Subscriber methods to receive those messages and signals @kamilah_santos

Slide 22

Slide 22 text

@kamilah_santos

Slide 23

Slide 23 text

interface Flow.Subscription methods to link both the Publisher and the Subscriber. @kamilah_santos

Slide 24

Slide 24 text

@kamilah_santos

Slide 25

Slide 25 text

interface Flow.Processor defines methods to do some advanced operations like chaining transformations of items from publishers to subscribers @kamilah_santos

Slide 26

Slide 26 text

@kamilah_santos

Slide 27

Slide 27 text

class SubmissionPublisher implements Flow.Publisher flexible producer of items, compliant with the Reactive Streams initiative . @kamilah_santos

Slide 28

Slide 28 text

@kamilah_santos

Slide 29

Slide 29 text

Reactive streams aimed at runtime environments (JVM) as well network protocols. https://github.com/reactive-streams/reactive-streams-jvm @kamilah_santos

Slide 30

Slide 30 text

Reactive streams the main goal is define the exchange of stream data across an asynchronous boundary. @kamilah_santos

Slide 31

Slide 31 text

Reactive streams your scope is to find a minimal set of methods, interfaces and protocols that will describe the necessary operations and entities to have asynchronous streams of data with NIO back pressure. @kamilah_santos

Slide 32

Slide 32 text

Functional Features Java 8+ @kamilah_santos

Slide 33

Slide 33 text

@kamilah_santos

Slide 34

Slide 34 text

@kamilah_santos

Slide 35

Slide 35 text

@kamilah_santos

Slide 36

Slide 36 text

@kamilah_santos

Slide 37

Slide 37 text

@kamilah_santos

Slide 38

Slide 38 text

@kamilah_santos

Slide 39

Slide 39 text

Reactive Concepts @kamilah_santos

Slide 40

Slide 40 text

Backpressure Resistance or force opposing the desired flow of data through software. @kamilah_santos

Slide 41

Slide 41 text

Stream Sequence of objects that supports various methods wich can be operated to produce a result @kamilah_santos

Slide 42

Slide 42 text

Stream linked to a data source, a stream is capable of emitting three events: @kamilah_santos

Slide 43

Slide 43 text

onNext() represents some value, go to next value of a list @kamilah_santos

Slide 44

Slide 44 text

onError() there was an error in the execution @kamilah_santos

Slide 45

Slide 45 text

onComplete() in the case of finite events, indicates that it has been completed @kamilah_santos

Slide 46

Slide 46 text

Flux It can emit 0 to N events, also passing through OnNext (), OnComplete () and onError. @kamilah_santos

Slide 47

Slide 47 text

Mono Emits at most one event (0 to 1 event). @kamilah_santos

Slide 48

Slide 48 text

Observable an observable can pass messages asynchronously. @kamilah_santos

Slide 49

Slide 49 text

Subscriber consumes the data received from the subscription @kamilah_santos

Slide 50

Slide 50 text

Publisher produces the data that will be consumed by subscription. @kamilah_santos

Slide 51

Slide 51 text

Subscription connection between a Subscriber and a Publisher @kamilah_santos

Slide 52

Slide 52 text

Cold Observable the sequence of events is only executed if the Observable has an associated subscriber @kamilah_santos

Slide 53

Slide 53 text

Hot Observable it issues events regardless of whether there is an associated subscriber @kamilah_santos

Slide 54

Slide 54 text

Why was webflux created? Spring WebFlux can be defined as a version “parallel” to the already known and widely used Spring MVC (servlet) , having as main difference the support for reactive NIO streams and for supporting the concept of backpressure with Netty server coming by default embedded in its architecture. @kamilah_santos

Slide 55

Slide 55 text

Why was webflux created? From version 5.0 of the Spring Framework we have a reactive part in addition to the Servlet structure that already existed, each module of these is optional, you can use the Servlet part, the reactive part or even both in your applications. @kamilah_santos

Slide 56

Slide 56 text

Why was webflux created? https://docs.spring.io/spring-framework/docs/5.0.0.BUILD-SNAPSHOT/spring-framework-reference/html/w eb-reactive.html @kamilah_santos

Slide 57

Slide 57 text

Spring Webflux was developed because we needed (we developers) non-blocking applications that were able to work with a small number of threads simultaneously and that could be run with a few hardware resources. @kamilah_santos

Slide 58

Slide 58 text

in Servlet 3.1 an NIO API was provided, but its use does not match the rest of the API and all the concepts behind Servlet, which has blocking contracts. @kamilah_santos

Slide 59

Slide 59 text

These factors were decisive for the development of a new API that was used independently of the execution time and in a non-blocking way, which was possible with the servers (Netty for example) that consolidated themselves in the asynchronous and non-blocking operation. @kamilah_santos

Slide 60

Slide 60 text

Another reason is that WebFlux makes it easier to understand and use functional / reactive programming concepts. With the addition of functional features from Java 8 and Flow API in Java 9 that allowed Spring WebFlux to have functional endpoints and annotated controllers in the applications. @kamilah_santos

Slide 61

Slide 61 text

How requests works in the servlet model? @kamilah_santos

Slide 62

Slide 62 text

This model, depending on the volume of requests and the way it was developed, can cause slowness to your application and even an Out Of Memory error (this type of error usually occurs when we keep objects for a long time or try to process a lot of data at once) @kamilah_santos

Slide 63

Slide 63 text

@kamilah_santos

Slide 64

Slide 64 text

With this traditional API design model, we do not support Backpressure, the writing of the API is imperative and as has been said before, it works in a synchronous and blocking way @kamilah_santos

Slide 65

Slide 65 text

@kamilah_santos

Slide 66

Slide 66 text

@kamilah_santos

Slide 67

Slide 67 text

And in Spring WebFlux? @kamilah_santos

Slide 68

Slide 68 text

In this request model, for each item of this Flux that is read and returned from the database, an onNext () is called and when it reaches the last one it receives an “signal” from onComplete () or if an error occurs, it will receive an onError (). @kamilah_santos

Slide 69

Slide 69 text

@kamilah_santos

Slide 70

Slide 70 text

The requests triggered by a client, are received by Netty, which is our non-blocking server, which will be received by the event loop, which will receive this event and dispatch it, the reactive adapter (reactor-netty), will pass this on to the dispacther handler who is in charge of the endpoint to pass this information back to the client, this occurs asynchronously and not blocking. @kamilah_santos

Slide 71

Slide 71 text

@kamilah_santos

Slide 72

Slide 72 text

The reactive adapter is usually provided by a library,here will cover the Reactor project. @kamilah_santos

Slide 73

Slide 73 text

Project Reactor @kamilah_santos

Slide 74

Slide 74 text

It is a library based on Reactive Streams Specifications, it is totally non-blocking and interacts directly with the functional Java API (Stream, Duration and Completable Future), for composing elements using Flux and Mono and suitable (and widely used) in microservices architectures, offering ready backpressure mechanisms for TCP, UDP and HTTP (including web sockets). @kamilah_santos

Slide 75

Slide 75 text

Its operators and schedulers can support a large transfer volume (up to 10 million requests per second, according to Reactor documentation https://projectreactor.io/), it was also the first reactive library to implement some points suggested by reactive streams commons (https://github.com/reactor/reactive-streams-commons), which were later also implemented by RxJava2. @kamilah_santos

Slide 76

Slide 76 text

Its modules are interoperable and abstract reactive streams to facilitate their development, being able to be used with: - Spring Frameworks - Drivers and Clients (for example CloudFoundry Java Client https://github.com/cloudfoundry/cf-java-client) - in protocols / contracts like R2DBC (https://r2dbc.io/) and RSocket (https://rsocket.io/), for example. @kamilah_santos

Slide 77

Slide 77 text

Reactor Core is the main part of this library, the other modules are dependent on it, it provides reactive types that implement a Publisher that provides Flux and Mono @kamilah_santos

Slide 78

Slide 78 text

Reactor Netty is the server of our application is a non-blocking input and output structure (NIO), used for the development of highly scalable servers. @kamilah_santos

Slide 79

Slide 79 text

Reactor Test is responsible for supporting reactive testing and assertions @kamilah_santos

Slide 80

Slide 80 text

Netty it’s possible to work at the socket level and create your own communication protocols @kamilah_santos

Slide 81

Slide 81 text

Netty For these NIO server configurations it is good to have knowledge of threads, event loop, buffers and memory management @kamilah_santos

Slide 82

Slide 82 text

@kamilah_santos

Slide 83

Slide 83 text

Show me the code!! @kamilah_santos

Slide 84

Slide 84 text

Benefits @kamilah_santos

Slide 85

Slide 85 text

- non-blocking, which generates a certain performance gain. - fewer threads, less memory used / spent - clearer view of functional features - processes different forms of request asynchronously @kamilah_santos

Slide 86

Slide 86 text

- follows the principles of the reactive manifest: responsive, resilient, elastic and message-driven applications. - Backpressure - Using Reactor we have: greater readability of the code, a wide variety of operators to manipulate the data and a high level of abstraction. @kamilah_santos

Slide 87

Slide 87 text

Problems @kamilah_santos

Slide 88

Slide 88 text

- A different way of programming (declarative programming) - Difficult to debug (stack trace more complicated to understand). - Not all database drivers are fully ready for reactive programming. @kamilah_santos

Slide 89

Slide 89 text

When use @kamilah_santos

Slide 90

Slide 90 text

- If you have fast producers (faster emission of information), and low consumers (slow and blocking consumers), without backpressure support. - Variable traffic - Constant calls to the database and that database already has a reactive driver, the switch to webflux can decrease latency time. - Many streams, events in the application. - When this service is consumed via mobile and has a high volume of traffic. @kamilah_santos

Slide 91

Slide 91 text

When not use @kamilah_santos

Slide 92

Slide 92 text

- if your application does not have a very high traffic volume, it does not have multiple request sources - If there is no accumulation of requests, it occurs due to blocking processes. - If the dependencies already used in your services are blocking @kamilah_santos

Slide 93

Slide 93 text

Thanks!! My contacts Example @kamilah_santos

Slide 94

Slide 94 text

Thanks!! https://tech.io/playgrounds/929/reactive-programming-with-reactor-3/transform https://www.callicoder.com/reactive-rest-apis-spring-webflux-reactive-mongo/ http://reactivex.io/languages.html https://www.journaldev.com/20723/java-9-reactive-streams https://projectreactor.io/ http://www.trieu.xyz/2019/04/netty-cookbook.html https://dzone.com/articles/build-a-simple-netty-application-with-and-without https://www.baeldung.com/netty https://developer.okta.com/blog/2018/09/21/reactive-programming-with-spring https://speakerdeck.com/olehdokuka/get-reactive-with-project-reactor-and-spring-5 https://speakerdeck.com/kamilahsantos/2020-d0013e50-afdf-4e9c-b411-7f22d2f3d64c https://speakerdeck.com/kamilahsantos/tdc-floripa-melhorando-a-performance-e-legibilidade-de-aplicacoes-java-com-spri ng-web-flux https://www.baeldung.com/spring-webflux https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html https://docs.spring.io/spring-framework/docs/5.0.0.BUILD-SNAPSHOT/spring-framework-reference/html/web-reactive.html @kamilah_santos