SlideShare una empresa de Scribd logo
1 de 27
REACTIVE
PROGRAMMING INTRO
AHMED ABDULAZIZ
OUTLINE
1. Programming Paradigms
2. Blocking and Non-Blocking
3. Reactive Programming in Depth
4. Why and When?
PROGRAMMING
PARADIGMS
IMPERATIVE, REACTIVE AND FUNCTIONAL
IMPERATIVE
PROGRAMMING
Imperative programming is mostly
sequential and follows a natural flow,
hence it is easy to write.
For backend servers, this follows a thread
per request model. Each request means a
new thread on the server. For millions of
requests that can be costly as each thread
means at least 1-2MB of RAM just for the
thread itself.
Also, this means the thread must be
blocked for any operation even that ’s
outside of our application.
1
• Receive Request
2
• Attempt to retrieve data from database
3
• Block thread and waste memory and CPU
until response
4
• Get data and send it to external system
(BSS, CMS, etc.)
5
• Block thread again until response from
external system
REACTIVE PROGRAMMING
Reactive programming is a paradigm that
deals with asynchronous data streams
(sequences of events), which means it
executes modifications to the execution
environment (context) in a certain order.
Using pre-defined threads every request is
assigned to one of the threads and the
threads are NOT BLOCKED by waiting for a
response from DB for example.
It is all about reacting to events, so reacting
to the DB returning with data instead of
actively wait for it.
Functional programming is usually used to
achieve reactivity this is called FRP or
Functional Reactive Programming.
1
• Receive Request
2
• Attempt to retrieve data from database
3
• Don’t block thread and receive another
request while waiting
4
• Get data and send it to external system
(BSS, CMS, etc.)
5
• Process another request while waiting
FUNCTIONAL IMPERATIVE
FUNCTIONAL VS IMPERATIVE
1. No side effects, as in all our processing data should be immutable.
2. Statelessness.
3. Functions are first class citizens, they are independent of each other.
4. Modularity because set of execution steps is an independent function.
FUNCTIONAL PROGRAMMING RULES
BLOCKING AND NON-
BLOCKING
SYNC & BLOCKING
ASYNC & BLOCKING
ASYNC & NON-BLOCKING
REACTIVE
PROGRAMMING IN
DEPTH
With a single thread we can handle a lot of requests, each request is processed in the event loop and we won’t
wait for the response.
It will be sent to a queue (like message queues) and to be processed on receiving an event of completion, the
event of completion is called a callback.
The event loop is a constantly running process that monitors both the callback/task queue and the call stack.
If the call stack is not empty, the event loop waits until it is empty and places the next function from the callback
queue to the call stack. If the callback queue is empty, nothing will happen.
JavaScript is built on this concept, also note that the name event loop and callback can differ from a
language/framework to another.
We can have one thread (JavaScript) or more threads (Java, mostly one per core) with each having their own event
loop.
EVENT LOOPS AND CALLBACKS
EVENT LOOP VIDEO
https://www.youtube.com/watch?v=QEG4Euypgok
ANOTHER EVENT LOOP VIDEO
REACTIVE PROGRAMMING
STREAMS
In RP, any data is considered a stream. So when a
client request all users, a response is returned to
indicate that the request was sent successfully.
A stream is a sequence of ongoing events ordered
in time. It can emit three different things: a value,
an error, or a "completed" signal. We capture
these emitted events only asynchronously.
PUBLISHER SUBSCRIBER
Subscriber (Observer): Data stream consumer, it
observes.
Publisher (Observable): Data stream producer, it is
observable.
Subscription: The connection between the
subscriber and publisher, the subscriber can define
some specs like events capacity and choose when
to end the stream.
The subscriber must be non-blocking to be
reactive.
BACK-PRESSURE
Backpressure is the actual main difference
between a normal asynchronous pub sub relation
and a reactive non-blocking one.
It means that the publisher should never
overwhelm the subscriber with more requests
than it can handle.
The subscriber defines its boundaries to achieve
this and it can keep requesting in batch number
(i.e. 100 responses more).
FUNCTIONAL
MANIPULATION OF
STREAMS (PIPES)
Some reactive libraries allow us to modify the
stream in a functional manner, in some libraries
these are called pipes but they have other names
as well.
What’s important is that they must be purely
functional with no side effect.
Rx: Reactive Extension (Rx) are libraries that enable imperative programming languages to compose
asynchronous and event-based programs by using observable reactive streams. The data stream is
called Flowable (multiple data) and Maybe (single result).
They exist for many languages like Java (RxJava), JavaScript (RxJS), Kotlin (RxKotlin), Swift (RxSwift), .NET
(Rx.NET), Python (RxPy) and so on.
Project Reactor (Spring Webflux): Similar to RxJava but implemented for Spring framework. The stream
is called Flux (multiple data) and Mono (single result).
Vert.x: Another java based reactive framework which is extremely powerful and fast, it has Future
(similar to Promise in JS) for data stream.
It is a complete ecosystem and it’s the most powerful reactive library written in any high level
language. It has an app generator https://start.vertx.io/ similar to spring initializr. Other frameworks
like Quarkus and es4x build upon it to provide reactivity.
FRAMEWORKS & LIBRARIES
WHY AND WHEN?
PROS AND CONS, WHEN TO USE IT, BENCHMARKS.
PROS
Can manage millions of request with a small amount of
threads.
Responsive, resilient, elastic and message driven (Reactive
Manifesto) systems.
Generally higher performance for web applications.
Back-pressure.
Perfect when integrating with stream/pub-sub systems like
Kafka or RabbitMQ.
NoSQL databases and drivers naturally support it.
CONS
Can be more complicated to write, not as natural as regular
imperative code backgrounds.
Non-blocking code is harder to write for people coming
from non-functional Support for reactive JDBC is weak
(R2DBC).
Debugging can be really hard.
Easy to mess. Subscriptions must be closed when not in
use.
Still new, support is not like old thread per request models
like servlets.
PROS AND CONS
USE IT
High-load and multi-user applications:
◦ Proxy Servers and Load Balancers.
◦ Any application that doesn’t do a lot of processing itself but
delegates the high data processing to another system, like
telecom e-care portals as they delegate to OCS, BSS or
message brokers.
◦ Social Networks.
◦ Games.
◦ Real-time data streaming.
DON’T USE IT
High processing/blocking applications:
◦ If your whole application has to block the thread to do high
processing (i.e. PDF generation in BI applications) then you
probably would benefit from thread per request models
WHEN TO USE
Spring Benchmarks
From this link, you can find a comparison of Spring Webflux vs normal Servlet based (Thread per request)
comparison, here is the result (msg/sec), it was done on a single core 1GB RAM server.
Check this link for more info, you can find code implementations here.
Basically, vert.x (and frameworks building on vert.x)are the fastest queries.
Spring JDBC/Data JPA will slow down Webflux full potential.
Pg-client is actually made for Vert.x (by Reactiverse) and it’s the fastest to communicate with
databases in a reactive manner.
There is reactive hibernate to use as an ORM and without a big loss. It can directly return data
streams from Mutiny library (Qurakus) but not return a Flux.
Spring Data has Spring Data R2DBC but it is not as ready as Hibernate Reactive.
POLYGLOT BENCHMARK
Questions?
Thanks!!

Más contenido relacionado

La actualidad más candente

Reactive Programming in Java and Spring Framework 5
Reactive Programming in Java and Spring Framework 5Reactive Programming in Java and Spring Framework 5
Reactive Programming in Java and Spring Framework 5Richard Langlois P. Eng.
 
Being Functional on Reactive Streams with Spring Reactor
Being Functional on Reactive Streams with Spring ReactorBeing Functional on Reactive Streams with Spring Reactor
Being Functional on Reactive Streams with Spring ReactorMax Huang
 
Reactive Card Magic: Understanding Spring WebFlux and Project Reactor
Reactive Card Magic: Understanding Spring WebFlux and Project ReactorReactive Card Magic: Understanding Spring WebFlux and Project Reactor
Reactive Card Magic: Understanding Spring WebFlux and Project ReactorVMware Tanzu
 
Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018
Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018
Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018Scrum Breakfast Vietnam
 
Project Reactor Now and Tomorrow
Project Reactor Now and TomorrowProject Reactor Now and Tomorrow
Project Reactor Now and TomorrowVMware Tanzu
 
Integrating Microservices with Apache Camel
Integrating Microservices with Apache CamelIntegrating Microservices with Apache Camel
Integrating Microservices with Apache CamelChristian Posta
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOPDzmitry Naskou
 
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...Codemotion
 
Apache Kafka Architecture & Fundamentals Explained
Apache Kafka Architecture & Fundamentals ExplainedApache Kafka Architecture & Fundamentals Explained
Apache Kafka Architecture & Fundamentals Explainedconfluent
 
Kafka Streams for Java enthusiasts
Kafka Streams for Java enthusiastsKafka Streams for Java enthusiasts
Kafka Streams for Java enthusiastsSlim Baltagi
 
An Introduction to Apache Kafka
An Introduction to Apache KafkaAn Introduction to Apache Kafka
An Introduction to Apache KafkaAmir Sedighi
 
Node.Js: Basics Concepts and Introduction
Node.Js: Basics Concepts and Introduction Node.Js: Basics Concepts and Introduction
Node.Js: Basics Concepts and Introduction Kanika Gera
 
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013Introduction and Overview of Apache Kafka, TriHUG July 23, 2013
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013mumrah
 
Networking in Java with NIO and Netty
Networking in Java with NIO and NettyNetworking in Java with NIO and Netty
Networking in Java with NIO and NettyConstantine Slisenka
 
Declarative Clients in Spring
Declarative Clients in SpringDeclarative Clients in Spring
Declarative Clients in SpringVMware Tanzu
 
Common issues with Apache Kafka® Producer
Common issues with Apache Kafka® ProducerCommon issues with Apache Kafka® Producer
Common issues with Apache Kafka® Producerconfluent
 

La actualidad más candente (20)

Project Reactor By Example
Project Reactor By ExampleProject Reactor By Example
Project Reactor By Example
 
Reactive Programming in Java and Spring Framework 5
Reactive Programming in Java and Spring Framework 5Reactive Programming in Java and Spring Framework 5
Reactive Programming in Java and Spring Framework 5
 
Being Functional on Reactive Streams with Spring Reactor
Being Functional on Reactive Streams with Spring ReactorBeing Functional on Reactive Streams with Spring Reactor
Being Functional on Reactive Streams with Spring Reactor
 
Reactive Card Magic: Understanding Spring WebFlux and Project Reactor
Reactive Card Magic: Understanding Spring WebFlux and Project ReactorReactive Card Magic: Understanding Spring WebFlux and Project Reactor
Reactive Card Magic: Understanding Spring WebFlux and Project Reactor
 
Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018
Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018
Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018
 
Project Reactor Now and Tomorrow
Project Reactor Now and TomorrowProject Reactor Now and Tomorrow
Project Reactor Now and Tomorrow
 
Integrating Microservices with Apache Camel
Integrating Microservices with Apache CamelIntegrating Microservices with Apache Camel
Integrating Microservices with Apache Camel
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
 
Apache Kafka Architecture & Fundamentals Explained
Apache Kafka Architecture & Fundamentals ExplainedApache Kafka Architecture & Fundamentals Explained
Apache Kafka Architecture & Fundamentals Explained
 
Kafka Streams for Java enthusiasts
Kafka Streams for Java enthusiastsKafka Streams for Java enthusiasts
Kafka Streams for Java enthusiasts
 
An Introduction to Apache Kafka
An Introduction to Apache KafkaAn Introduction to Apache Kafka
An Introduction to Apache Kafka
 
Node.Js: Basics Concepts and Introduction
Node.Js: Basics Concepts and Introduction Node.Js: Basics Concepts and Introduction
Node.Js: Basics Concepts and Introduction
 
Message Broker System and RabbitMQ
Message Broker System and RabbitMQMessage Broker System and RabbitMQ
Message Broker System and RabbitMQ
 
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013Introduction and Overview of Apache Kafka, TriHUG July 23, 2013
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013
 
Networking in Java with NIO and Netty
Networking in Java with NIO and NettyNetworking in Java with NIO and Netty
Networking in Java with NIO and Netty
 
Declarative Clients in Spring
Declarative Clients in SpringDeclarative Clients in Spring
Declarative Clients in Spring
 
Common issues with Apache Kafka® Producer
Common issues with Apache Kafka® ProducerCommon issues with Apache Kafka® Producer
Common issues with Apache Kafka® Producer
 
Kafka 101
Kafka 101Kafka 101
Kafka 101
 

Similar a Reactive programming intro

198970820 p-oooooooooo
198970820 p-oooooooooo198970820 p-oooooooooo
198970820 p-oooooooooohomeworkping4
 
NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginnersEnoch Joshua
 
Atmosphere 2014: Switching from monolithic approach to modular cloud computin...
Atmosphere 2014: Switching from monolithic approach to modular cloud computin...Atmosphere 2014: Switching from monolithic approach to modular cloud computin...
Atmosphere 2014: Switching from monolithic approach to modular cloud computin...PROIDEA
 
101 mistakes FINN.no has made with Kafka (Baksida meetup)
101 mistakes FINN.no has made with Kafka (Baksida meetup)101 mistakes FINN.no has made with Kafka (Baksida meetup)
101 mistakes FINN.no has made with Kafka (Baksida meetup)Henning Spjelkavik
 
5.7 Parallel Processing - Reactive Programming.pdf.pptx
5.7 Parallel Processing - Reactive Programming.pdf.pptx5.7 Parallel Processing - Reactive Programming.pdf.pptx
5.7 Parallel Processing - Reactive Programming.pdf.pptxMohamedBilal73
 
Building a Scalable Architecture for web apps
Building a Scalable Architecture for web appsBuilding a Scalable Architecture for web apps
Building a Scalable Architecture for web appsDirecti Group
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaReactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaAli Muzaffar
 
Software architecture for data applications
Software architecture for data applicationsSoftware architecture for data applications
Software architecture for data applicationsDing Li
 
Handling Data in Mega Scale Systems
Handling Data in Mega Scale SystemsHandling Data in Mega Scale Systems
Handling Data in Mega Scale SystemsDirecti Group
 
Distributed Reactive Services with Reactor & Spring - Stéphane Maldini
Distributed Reactive Services with Reactor & Spring - Stéphane MaldiniDistributed Reactive Services with Reactor & Spring - Stéphane Maldini
Distributed Reactive Services with Reactor & Spring - Stéphane MaldiniVMware Tanzu
 
Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN StackRob Davarnia
 
Shopzilla On Concurrency
Shopzilla On ConcurrencyShopzilla On Concurrency
Shopzilla On ConcurrencyWill Gage
 
Near Real time Indexing Kafka Messages to Apache Blur using Spark Streaming
Near Real time Indexing Kafka Messages to Apache Blur using Spark StreamingNear Real time Indexing Kafka Messages to Apache Blur using Spark Streaming
Near Real time Indexing Kafka Messages to Apache Blur using Spark StreamingDibyendu Bhattacharya
 
Asko Oja Moskva Architecture Highload
Asko Oja Moskva Architecture HighloadAsko Oja Moskva Architecture Highload
Asko Oja Moskva Architecture HighloadOntico
 
Building Killer RESTful APIs with NodeJs
Building Killer RESTful APIs with NodeJsBuilding Killer RESTful APIs with NodeJs
Building Killer RESTful APIs with NodeJsSrdjan Strbanovic
 
TOLL MANAGEMENT SYSTEM
TOLL MANAGEMENT SYSTEMTOLL MANAGEMENT SYSTEM
TOLL MANAGEMENT SYSTEMvishnuRajan20
 

Similar a Reactive programming intro (20)

198970820 p-oooooooooo
198970820 p-oooooooooo198970820 p-oooooooooo
198970820 p-oooooooooo
 
NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginners
 
Atmosphere 2014: Switching from monolithic approach to modular cloud computin...
Atmosphere 2014: Switching from monolithic approach to modular cloud computin...Atmosphere 2014: Switching from monolithic approach to modular cloud computin...
Atmosphere 2014: Switching from monolithic approach to modular cloud computin...
 
101 mistakes FINN.no has made with Kafka (Baksida meetup)
101 mistakes FINN.no has made with Kafka (Baksida meetup)101 mistakes FINN.no has made with Kafka (Baksida meetup)
101 mistakes FINN.no has made with Kafka (Baksida meetup)
 
5.7 Parallel Processing - Reactive Programming.pdf.pptx
5.7 Parallel Processing - Reactive Programming.pdf.pptx5.7 Parallel Processing - Reactive Programming.pdf.pptx
5.7 Parallel Processing - Reactive Programming.pdf.pptx
 
MongoDB
MongoDBMongoDB
MongoDB
 
NoSql Databases
NoSql DatabasesNoSql Databases
NoSql Databases
 
Building a Scalable Architecture for web apps
Building a Scalable Architecture for web appsBuilding a Scalable Architecture for web apps
Building a Scalable Architecture for web apps
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaReactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJava
 
Best node js course
Best node js courseBest node js course
Best node js course
 
Software architecture for data applications
Software architecture for data applicationsSoftware architecture for data applications
Software architecture for data applications
 
Handling Data in Mega Scale Systems
Handling Data in Mega Scale SystemsHandling Data in Mega Scale Systems
Handling Data in Mega Scale Systems
 
Node js internal
Node js internalNode js internal
Node js internal
 
Distributed Reactive Services with Reactor & Spring - Stéphane Maldini
Distributed Reactive Services with Reactor & Spring - Stéphane MaldiniDistributed Reactive Services with Reactor & Spring - Stéphane Maldini
Distributed Reactive Services with Reactor & Spring - Stéphane Maldini
 
Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN Stack
 
Shopzilla On Concurrency
Shopzilla On ConcurrencyShopzilla On Concurrency
Shopzilla On Concurrency
 
Near Real time Indexing Kafka Messages to Apache Blur using Spark Streaming
Near Real time Indexing Kafka Messages to Apache Blur using Spark StreamingNear Real time Indexing Kafka Messages to Apache Blur using Spark Streaming
Near Real time Indexing Kafka Messages to Apache Blur using Spark Streaming
 
Asko Oja Moskva Architecture Highload
Asko Oja Moskva Architecture HighloadAsko Oja Moskva Architecture Highload
Asko Oja Moskva Architecture Highload
 
Building Killer RESTful APIs with NodeJs
Building Killer RESTful APIs with NodeJsBuilding Killer RESTful APIs with NodeJs
Building Killer RESTful APIs with NodeJs
 
TOLL MANAGEMENT SYSTEM
TOLL MANAGEMENT SYSTEMTOLL MANAGEMENT SYSTEM
TOLL MANAGEMENT SYSTEM
 

Último

SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxRTS corp
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 

Último (20)

SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 

Reactive programming intro

  • 2. OUTLINE 1. Programming Paradigms 2. Blocking and Non-Blocking 3. Reactive Programming in Depth 4. Why and When?
  • 4. IMPERATIVE PROGRAMMING Imperative programming is mostly sequential and follows a natural flow, hence it is easy to write. For backend servers, this follows a thread per request model. Each request means a new thread on the server. For millions of requests that can be costly as each thread means at least 1-2MB of RAM just for the thread itself. Also, this means the thread must be blocked for any operation even that ’s outside of our application. 1 • Receive Request 2 • Attempt to retrieve data from database 3 • Block thread and waste memory and CPU until response 4 • Get data and send it to external system (BSS, CMS, etc.) 5 • Block thread again until response from external system
  • 5. REACTIVE PROGRAMMING Reactive programming is a paradigm that deals with asynchronous data streams (sequences of events), which means it executes modifications to the execution environment (context) in a certain order. Using pre-defined threads every request is assigned to one of the threads and the threads are NOT BLOCKED by waiting for a response from DB for example. It is all about reacting to events, so reacting to the DB returning with data instead of actively wait for it. Functional programming is usually used to achieve reactivity this is called FRP or Functional Reactive Programming. 1 • Receive Request 2 • Attempt to retrieve data from database 3 • Don’t block thread and receive another request while waiting 4 • Get data and send it to external system (BSS, CMS, etc.) 5 • Process another request while waiting
  • 7. 1. No side effects, as in all our processing data should be immutable. 2. Statelessness. 3. Functions are first class citizens, they are independent of each other. 4. Modularity because set of execution steps is an independent function. FUNCTIONAL PROGRAMMING RULES
  • 13. With a single thread we can handle a lot of requests, each request is processed in the event loop and we won’t wait for the response. It will be sent to a queue (like message queues) and to be processed on receiving an event of completion, the event of completion is called a callback. The event loop is a constantly running process that monitors both the callback/task queue and the call stack. If the call stack is not empty, the event loop waits until it is empty and places the next function from the callback queue to the call stack. If the callback queue is empty, nothing will happen. JavaScript is built on this concept, also note that the name event loop and callback can differ from a language/framework to another. We can have one thread (JavaScript) or more threads (Java, mostly one per core) with each having their own event loop. EVENT LOOPS AND CALLBACKS
  • 16. REACTIVE PROGRAMMING STREAMS In RP, any data is considered a stream. So when a client request all users, a response is returned to indicate that the request was sent successfully. A stream is a sequence of ongoing events ordered in time. It can emit three different things: a value, an error, or a "completed" signal. We capture these emitted events only asynchronously.
  • 17. PUBLISHER SUBSCRIBER Subscriber (Observer): Data stream consumer, it observes. Publisher (Observable): Data stream producer, it is observable. Subscription: The connection between the subscriber and publisher, the subscriber can define some specs like events capacity and choose when to end the stream. The subscriber must be non-blocking to be reactive.
  • 18. BACK-PRESSURE Backpressure is the actual main difference between a normal asynchronous pub sub relation and a reactive non-blocking one. It means that the publisher should never overwhelm the subscriber with more requests than it can handle. The subscriber defines its boundaries to achieve this and it can keep requesting in batch number (i.e. 100 responses more).
  • 19. FUNCTIONAL MANIPULATION OF STREAMS (PIPES) Some reactive libraries allow us to modify the stream in a functional manner, in some libraries these are called pipes but they have other names as well. What’s important is that they must be purely functional with no side effect.
  • 20. Rx: Reactive Extension (Rx) are libraries that enable imperative programming languages to compose asynchronous and event-based programs by using observable reactive streams. The data stream is called Flowable (multiple data) and Maybe (single result). They exist for many languages like Java (RxJava), JavaScript (RxJS), Kotlin (RxKotlin), Swift (RxSwift), .NET (Rx.NET), Python (RxPy) and so on. Project Reactor (Spring Webflux): Similar to RxJava but implemented for Spring framework. The stream is called Flux (multiple data) and Mono (single result). Vert.x: Another java based reactive framework which is extremely powerful and fast, it has Future (similar to Promise in JS) for data stream. It is a complete ecosystem and it’s the most powerful reactive library written in any high level language. It has an app generator https://start.vertx.io/ similar to spring initializr. Other frameworks like Quarkus and es4x build upon it to provide reactivity. FRAMEWORKS & LIBRARIES
  • 21. WHY AND WHEN? PROS AND CONS, WHEN TO USE IT, BENCHMARKS.
  • 22. PROS Can manage millions of request with a small amount of threads. Responsive, resilient, elastic and message driven (Reactive Manifesto) systems. Generally higher performance for web applications. Back-pressure. Perfect when integrating with stream/pub-sub systems like Kafka or RabbitMQ. NoSQL databases and drivers naturally support it. CONS Can be more complicated to write, not as natural as regular imperative code backgrounds. Non-blocking code is harder to write for people coming from non-functional Support for reactive JDBC is weak (R2DBC). Debugging can be really hard. Easy to mess. Subscriptions must be closed when not in use. Still new, support is not like old thread per request models like servlets. PROS AND CONS
  • 23. USE IT High-load and multi-user applications: ◦ Proxy Servers and Load Balancers. ◦ Any application that doesn’t do a lot of processing itself but delegates the high data processing to another system, like telecom e-care portals as they delegate to OCS, BSS or message brokers. ◦ Social Networks. ◦ Games. ◦ Real-time data streaming. DON’T USE IT High processing/blocking applications: ◦ If your whole application has to block the thread to do high processing (i.e. PDF generation in BI applications) then you probably would benefit from thread per request models WHEN TO USE
  • 24. Spring Benchmarks From this link, you can find a comparison of Spring Webflux vs normal Servlet based (Thread per request) comparison, here is the result (msg/sec), it was done on a single core 1GB RAM server.
  • 25. Check this link for more info, you can find code implementations here. Basically, vert.x (and frameworks building on vert.x)are the fastest queries. Spring JDBC/Data JPA will slow down Webflux full potential. Pg-client is actually made for Vert.x (by Reactiverse) and it’s the fastest to communicate with databases in a reactive manner. There is reactive hibernate to use as an ORM and without a big loss. It can directly return data streams from Mutiny library (Qurakus) but not return a Flux. Spring Data has Spring Data R2DBC but it is not as ready as Hibernate Reactive. POLYGLOT BENCHMARK