SlideShare a Scribd company logo
1 of 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!!

More Related Content

What's hot

Reactive Stream Processing with Akka Streams
Reactive Stream Processing with Akka StreamsReactive Stream Processing with Akka Streams
Reactive Stream Processing with Akka Streams
Konrad Malawski
 

What's hot (20)

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
 
Intro to Reactive Programming
Intro to Reactive ProgrammingIntro to Reactive Programming
Intro to Reactive Programming
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 
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
 
Svelte JS introduction
Svelte JS introductionSvelte JS introduction
Svelte JS introduction
 
Asynchronous javascript
 Asynchronous javascript Asynchronous javascript
Asynchronous javascript
 
Angular & RXJS: examples and use cases
Angular & RXJS: examples and use casesAngular & RXJS: examples and use cases
Angular & RXJS: examples and use cases
 
JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
 
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
 
gRPC: The Story of Microservices at Square
gRPC: The Story of Microservices at SquaregRPC: The Story of Microservices at Square
gRPC: The Story of Microservices at Square
 
Reactive Stream Processing with Akka Streams
Reactive Stream Processing with Akka StreamsReactive Stream Processing with Akka Streams
Reactive Stream Processing with Akka Streams
 
gRPC Overview
gRPC OverviewgRPC Overview
gRPC Overview
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJS
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
Producer Performance Tuning for Apache Kafka
Producer Performance Tuning for Apache KafkaProducer Performance Tuning for Apache Kafka
Producer Performance Tuning for Apache Kafka
 
RxJS Evolved
RxJS EvolvedRxJS Evolved
RxJS Evolved
 
Introduction to Node JS.pdf
Introduction to Node JS.pdfIntroduction to Node JS.pdf
Introduction to Node JS.pdf
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 
Introducing envoy-based service mesh at Booking.com
Introducing envoy-based service mesh at Booking.comIntroducing envoy-based service mesh at Booking.com
Introducing envoy-based service mesh at Booking.com
 
Use Node.js to create a REST API
Use Node.js to create a REST APIUse Node.js to create a REST API
Use Node.js to create a REST API
 

Similar to Reactive programming intro

Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN Stack
Rob Davarnia
 
Asko Oja Moskva Architecture Highload
Asko Oja Moskva Architecture HighloadAsko Oja Moskva Architecture Highload
Asko Oja Moskva Architecture Highload
Ontico
 

Similar to 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
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Recently uploaded (20)

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 

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