SlideShare a Scribd company logo
Reactive Applications in Java
Alex Mrynskyi
Palo Alto Networks
Our Team
Agenda
● The Problem
● What is Reactive Applications?
● Project Reactor 101
● How to Start with Reactive?
Why Reactive?
Today’s Challenges
Today applications are deployed on everything from mobile devices to cloud-based
clusters running thousands of multi-core processors. Users expect millisecond
response times and 100% uptime. Data is measured in Petabytes. Today's demands are
simply not met by yesterday’s software architectures.
Reactive Matifesto, 2014
The Rise of Microservices
The Rise of Microservices
Our Challenges
● Continuously process huge number of events, messages, files
● I/O bound workloads (HTTP requests, DB access, etc)
● Rate limiting from external systems
● Many microservices that interact with each other
Synchronous vs Asynchronous
Event Loop
Little's Law
Little's law, Wikipedia
long-term average number L of customers in a stationary system is equal to
the long-term average effective arrival rate λ multiplied by the average time
W that a customer spends in the system
Little's Law in Practice
Stop Rate Limiting! Capacity Management Done Right" by Jon Moore, Youtube
The number of active workers must be at least the average arrival rate of tasks multiplied by the average time to process
those tasks
workers >= tasks per second x time to process task
or
workers >= throughput x latency
or
throughput <= workers / latency
Web MVC with Blocking I/O
WebFlux with Non-Blocking I/O
Web MVC with Blocking I/O (multiple threads)
Web MVC vs WebFlux
Source - Hands-On Reactive Programming in Spring 5
Conclusion - WebFlux is much more efficient with regard to throughput, latency, and CPU usage.
Reactive Applications
Photo by Brayden Law from Pexels
Reactive Glossary
“Reactive” has become an overloaded term and is now being associated with several different
things to different people
● Reactive programming is a paradigm in which declarative code is issued to construct
asynchronous processing pipelines
● Reactive streams is an initiative that was created to provide a standard to unify reactive
extensions and deal with asynchronous stream processing with non-blocking backpressure
● Reactive systems—as defined by the Reactive Manifesto—is a set of architectural design
principles for building modern systems that are well prepared to meet the increasing demands
that applications face today
● Reactive programming and Reactive streams are all useful tools to design and build Reactive
systems
Reactive programming vs. Reactive systems
Reactive Manifesto
Reactive Systems
● Responsive - low latency
● Resilient - stay responsive on failures
● Elastic - scale as needed
● Message Driven - async messages for communication between components
Reactive Streams
Photo by Avery Nielsen-Webb from Pexels
Reactive Streams API
public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {}
public interface Publisher<T> {
public void subscribe(Subscriber<? super T> s);
}
public interface Subscriber<T> {
public void onSubscribe(Subscription s);
public void onNext(T t);
public void onError(Throwable t);
public void onComplete();
}
public interface Subscription {
public void request(long n);
public void cancel();
}
Don’t try it at home!!!
Reactive Signals
Photo by Avery Nielsen-Webb from Pexels
Reactive Streams Implementations
● RxJava (Netflix)
● Reactor (Pivotal)
● Vert.x (RedHat)
● Akka Streams (Typesafe)
Project Reactor
Photo by Avery Nielsen-Webb from Pexels
Project Reactor
Photo by Avery Nielsen-Webb from Pexels
● Reactor is a fourth-generation reactive library, based on the Reactive Streams
specification, for building non-blocking applications on the JVM
● Fully non-blocking and backpressure-ready network runtimes, including local
TCP/HTTP/UDP clients and servers, based on the robust Netty framework
● Foundation of the reactive stack in the Spring ecosystem and is featured in projects
such as Spring WebFlux, Spring Data, and Spring Cloud Gateway
Reactive Stack in Spring Boot
Web on Reactive Stack
● WebFlux is a non-blocking web stack to handle concurrency with a small number of
threads and scale with fewer hardware resources
● Uses Reactor Netty by default
● Supports two programming models
○ Annotated Controllers: Consistent with Spring MVC and based on the same annotations from the spring-web module
○ Functional Endpoints: Lambda-based, lightweight, and functional programming model
Reactive Libraries and Clients
● MongoDB, Redis, and Cassandra all have native reactive support in Spring Data
● Many relational databases (Postgres, Microsoft SQL Server, MySQL, H2, and Google
Spanner) have reactive support via R2DBC
● Reactor Kafka and Reactor RabbitMQ for messaging
● AWS SDK v2 is fully asynchronous and could be easily wrapped
● WebClient with functional, fluent API based on Reactor
● Resilience4j for Circuit Breaker, Rate Limiter, Retry or Bulkhead in a reactive way
Project Reactor 101
Photo by Avery Nielsen-Webb from Pexels
Photo by Ekaterina Belinskaya from Pexels
Flux
An Asynchronous Sequence of 0-N Items
Mono
An Asynchronous 0-1 Result
Marble Diagrams
Photo by Avery Nielsen-Webb from Pexels
Appendix B: How to read marble diagrams?
Operators
● .map, .filter, .flatMap, .take, .buffer, .subscribe, ....
● Not a part of Reactive Streams specification
Appendix A: Which operator do I need?
Assembly vs Subscription
Photo by Avery Nielsen-Webb from Pexels
Nothing Happens Until You Subscribe
Flight of the Flux 1 - Assembly vs Subscription
● Calling methods on a Flux or Mono (the operators) doesn’t immediately trigger the
behavior. This declarative phase is called assembly time.
● To trigger data to flow we you need to subscribe to the declared pipeline
Demo Time
Photo by Avery Nielsen-Webb from Pexels
Photo by PRAPHAPHAN WONGSAWAN from Pexels
flatMap
Photo by Avery Nielsen-Webb from Pexels
“flatMap Pack”
Photo by Avery Nielsen-Webb from Pexels
● .flatMap()/.flatMapMany() - transforms the elements asynchronously into Publishers,
then flatten these inner publishers into a single Flux through merging
● .concatMap() - transforms the elements asynchronously into Publishers, then flatten
these inner publishers into a single Flux, sequentially and preserving order using
concatenation
● .flatMapSequential() - transforms the elements asynchronously into Publishers, then
flatten these inner publishers into a single Flux, but merge them in the order of their
source element
Threading Model
Photo by Avery Nielsen-Webb from Pexels
● considered to be concurrency-agnostic
● Schedulers.parallel()
○ non-blocking operations
○ fixed size
○ number of of threads = number CPU cores
● Schedulers.boundedElastic()
○ usually used to offload blocking operations
○ creates new worker pools as needed and reuses idle ones
○ number of of threads = number of CPU cores x 10
● Schedulers.single()
○ a single, reusable thread
Flight of the Flux 3 - Hopping Threads and Schedulers
Wrapping Blocking Code
Photo by Avery Nielsen-Webb from Pexels
● Reactor offers two means of switching the execution context (or Scheduler) in a
reactive chain: publishOn and subscribeOn
Error Handling and Resiliency
Photo by Avery Nielsen-Webb from Pexels
● .retry()/.retryWhen() - retry subscription on failure
● .repeat()/.repeatWhen() - repeat (resubscribe) on empty result
● .defaultIfEmpty()/.switchIfEmpty() - fallback on empty
● .onErrorResume()/.onErrorReturn() - fallback on error
● .timeout(Duration) - cancel the subscription and fail if no items emitted
A.5. Handling Errors
Testing
Photo by Avery Nielsen-Webb from Pexels
Debug and Troubleshooting
Photo by Avery Nielsen-Webb from Pexels
Flight of the Flux 2 - Debugging Caveats
● Stack traces in Reactive world could veeeeeeery long and not informative
Debug and Troubleshooting
Photo by Avery Nielsen-Webb from Pexels
Flight of the Flux 2 - Debugging Caveats
● Integrate ReactorDebugAgent - a Java agent which helps debugging exceptions in
your application without paying a runtime cost (unlike Hooks.onOperatorDebug())
○ Hooks.onOperatorDebug() is still really useful in tests
● Use .log() and .checkpoint() operators in development to understand the flow
● Integrate BlockHound into tests to detect blocking code (tests only !!!)
Reactive “Laws”
Photo by Avery Nielsen-Webb from Pexels
● NOTHING happens until you subscribe
○ Idialy subscribe only once
● NEVER block parallel scheduler
○ Run blocking code on a separate Scheduler (i.e. boundedElastic)
● STOP thinking in threads
○ Think about concurrency instead
Learning Resources - Intro
Photo by Avery Nielsen-Webb from Pexels
● Video Flight of the Flux: A Look at Reactor Execution Model
● Blog series Flight of the Flux 1 - Assembly vs Subscription
● Video Avoiding Reactor Meltdown
● Video Do’s and Don’ts: Avoiding First-Time Reactive Programmer Mines - must
● The introduction to Reactive Programming you've been missing - not java but very
good explanation of the idea behind reactive programming
Learning Resources - Hands-on
Photo by Avery Nielsen-Webb from Pexels
● Introduction to Reactive Programming or the same on github GitHub -
reactor/lite-rx-api-hands-on: Lite Rx API Hands-On with Reactor Core 3. Just get
repo and fix all unit tests
● Head-First Reactive Workshop GitHub -
reactor/head-first-reactive-with-spring-and-reactor
Learning Resources - Hardcore
Photo by Avery Nielsen-Webb from Pexels
3-video series about Reactor internals
● https:/
/www.youtube.com/watch?v=OdSZ6mOQDcY
● https:/
/www.youtube.com/watch?v=noeWdjO4fyU
● https:/
/www.youtube.com/watch?v=cVKhFPiebSs

More Related Content

What's hot (20)

PDF
[Webinar]: Working with Reactive Spring
Knoldus Inc.
 
PPT
IBM Websphere MQ Basic
PRASAD BHATKAR
 
PPTX
Workshop Spring - Session 1 - L'offre Spring et les bases
Antoine Rey
 
PDF
Resilience4J
Knoldus Inc.
 
PDF
Java 17
Mutlu Okuducu
 
PDF
Decomposing applications for deployability and scalability #springone2gx #s12gx
Chris Richardson
 
PPTX
IBM Websphere introduction and installation for beginners
Shubham Gupta
 
PDF
Introduction to Docker
Aditya Konarde
 
PPTX
Microservices
SmartBear
 
PPTX
Design Patterns - 01 Introduction and Decorator Pattern
eprafulla
 
KEY
Event Driven Architecture
Stefan Norberg
 
PDF
What is Apache Kafka and What is an Event Streaming Platform?
confluent
 
PDF
Clean architecture
Lieven Doclo
 
PPTX
Apache tomcat
Shashwat Shriparv
 
PPTX
Laravel
tanveerkhan62
 
PPTX
Virtualization, Containers, Docker and scalable container management services
abhishek chawla
 
PPTX
Introduction to Apache Kafka
Jeff Holoman
 
PDF
Reactive Card Magic: Understanding Spring WebFlux and Project Reactor
VMware Tanzu
 
PPTX
Project Orleans - Actor Model framework
Neil Mackenzie
 
PDF
Application Architecture: The Next Wave | MuleSoft
MuleSoft
 
[Webinar]: Working with Reactive Spring
Knoldus Inc.
 
IBM Websphere MQ Basic
PRASAD BHATKAR
 
Workshop Spring - Session 1 - L'offre Spring et les bases
Antoine Rey
 
Resilience4J
Knoldus Inc.
 
Java 17
Mutlu Okuducu
 
Decomposing applications for deployability and scalability #springone2gx #s12gx
Chris Richardson
 
IBM Websphere introduction and installation for beginners
Shubham Gupta
 
Introduction to Docker
Aditya Konarde
 
Microservices
SmartBear
 
Design Patterns - 01 Introduction and Decorator Pattern
eprafulla
 
Event Driven Architecture
Stefan Norberg
 
What is Apache Kafka and What is an Event Streaming Platform?
confluent
 
Clean architecture
Lieven Doclo
 
Apache tomcat
Shashwat Shriparv
 
Laravel
tanveerkhan62
 
Virtualization, Containers, Docker and scalable container management services
abhishek chawla
 
Introduction to Apache Kafka
Jeff Holoman
 
Reactive Card Magic: Understanding Spring WebFlux and Project Reactor
VMware Tanzu
 
Project Orleans - Actor Model framework
Neil Mackenzie
 
Application Architecture: The Next Wave | MuleSoft
MuleSoft
 

Similar to Reactive Applications in Java (20)

PPTX
Reactive solutions using java 9 and spring reactor
OrenEzer1
 
PDF
Reactive Programming in Java and Spring Framework 5
Richard Langlois P. Eng.
 
PDF
Guide to Spring Reactive Programming using WebFlux
Inexture Solutions
 
PPTX
Reactive programming intro
Ahmed Ehab AbdulAziz
 
PDF
Spring Framework 5.0による Reactive Web Application #JavaDayTokyo
Toshiaki Maki
 
PDF
Reactive systems
Naresh Chintalcheru
 
PPTX
Reactive programming for java developers
Constantin Popa
 
PPTX
Reactive programming with Spring Webflux.pptx
João Esperancinha
 
PPTX
Reactive programming
SUDIP GHOSH
 
PDF
Reactive&amp;reactor
Geng-Dian Huang
 
PPTX
From Streams to Reactive Streams
Oleg Tsal-Tsalko
 
PDF
Springone2gx 2014 Reactive Streams and Reactor
Stéphane Maldini
 
PPTX
From Web to Flux @DevoxxBE 2023.pptx
Victor Rentea
 
PDF
NCUG 2019: Super charge your API’s with Reactive streams
Frank van der Linden
 
PDF
Project Reactor Now and Tomorrow
VMware Tanzu
 
PDF
reactive_programming_for_java_developers.pdf
Akshitkumar437417
 
PDF
Workshop: Event-sourced system through Reactive Streams
sterkje
 
PPTX
Workshop: Event-sourced system through Reactive Streams
Kristof Van Sever
 
PDF
Spring 5 Webflux - Advances in Java 2018
Trayan Iliev
 
PDF
Intro to Reactive Programming
Stéphane Maldini
 
Reactive solutions using java 9 and spring reactor
OrenEzer1
 
Reactive Programming in Java and Spring Framework 5
Richard Langlois P. Eng.
 
Guide to Spring Reactive Programming using WebFlux
Inexture Solutions
 
Reactive programming intro
Ahmed Ehab AbdulAziz
 
Spring Framework 5.0による Reactive Web Application #JavaDayTokyo
Toshiaki Maki
 
Reactive systems
Naresh Chintalcheru
 
Reactive programming for java developers
Constantin Popa
 
Reactive programming with Spring Webflux.pptx
João Esperancinha
 
Reactive programming
SUDIP GHOSH
 
Reactive&amp;reactor
Geng-Dian Huang
 
From Streams to Reactive Streams
Oleg Tsal-Tsalko
 
Springone2gx 2014 Reactive Streams and Reactor
Stéphane Maldini
 
From Web to Flux @DevoxxBE 2023.pptx
Victor Rentea
 
NCUG 2019: Super charge your API’s with Reactive streams
Frank van der Linden
 
Project Reactor Now and Tomorrow
VMware Tanzu
 
reactive_programming_for_java_developers.pdf
Akshitkumar437417
 
Workshop: Event-sourced system through Reactive Streams
sterkje
 
Workshop: Event-sourced system through Reactive Streams
Kristof Van Sever
 
Spring 5 Webflux - Advances in Java 2018
Trayan Iliev
 
Intro to Reactive Programming
Stéphane Maldini
 
Ad

Recently uploaded (20)

PDF
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
PDF
Why Are More Businesses Choosing Partners Over Freelancers for Salesforce.pdf
Cymetrix Software
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PDF
How Agentic AI Networks are Revolutionizing Collaborative AI Ecosystems in 2025
ronakdubey419
 
PDF
AI Image Enhancer: Revolutionizing Visual Quality”
docmasoom
 
PDF
Enhancing Security in VAST: Towards Static Vulnerability Scanning
ESUG
 
PPTX
Presentation about Database and Database Administrator
abhishekchauhan86963
 
PDF
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
PDF
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
PPTX
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
PDF
Troubleshooting Virtual Threads in Java!
Tier1 app
 
PPT
Brief History of Python by Learning Python in three hours
adanechb21
 
PDF
Supabase Meetup: Build in a weekend, scale to millions
Carlo Gilmar Padilla Santana
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PDF
System Center 2025 vs. 2022; What’s new, what’s next_PDF.pdf
Q-Advise
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
PDF
What companies do with Pharo (ESUG 2025)
ESUG
 
PDF
Infrastructure planning and resilience - Keith Hastings.pptx.pdf
Safe Software
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PDF
AWS_Agentic_AI_in_Indian_BFSI_A_Strategic_Blueprint_for_Customer.pdf
siddharthnetsavvies
 
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
Why Are More Businesses Choosing Partners Over Freelancers for Salesforce.pdf
Cymetrix Software
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
How Agentic AI Networks are Revolutionizing Collaborative AI Ecosystems in 2025
ronakdubey419
 
AI Image Enhancer: Revolutionizing Visual Quality”
docmasoom
 
Enhancing Security in VAST: Towards Static Vulnerability Scanning
ESUG
 
Presentation about Database and Database Administrator
abhishekchauhan86963
 
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
Troubleshooting Virtual Threads in Java!
Tier1 app
 
Brief History of Python by Learning Python in three hours
adanechb21
 
Supabase Meetup: Build in a weekend, scale to millions
Carlo Gilmar Padilla Santana
 
Presentation about variables and constant.pptx
kr2589474
 
System Center 2025 vs. 2022; What’s new, what’s next_PDF.pdf
Q-Advise
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
What companies do with Pharo (ESUG 2025)
ESUG
 
Infrastructure planning and resilience - Keith Hastings.pptx.pdf
Safe Software
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
AWS_Agentic_AI_in_Indian_BFSI_A_Strategic_Blueprint_for_Customer.pdf
siddharthnetsavvies
 
Ad

Reactive Applications in Java

  • 1. Reactive Applications in Java Alex Mrynskyi Palo Alto Networks
  • 3. Agenda ● The Problem ● What is Reactive Applications? ● Project Reactor 101 ● How to Start with Reactive?
  • 5. Today’s Challenges Today applications are deployed on everything from mobile devices to cloud-based clusters running thousands of multi-core processors. Users expect millisecond response times and 100% uptime. Data is measured in Petabytes. Today's demands are simply not met by yesterday’s software architectures. Reactive Matifesto, 2014
  • 6. The Rise of Microservices
  • 7. The Rise of Microservices
  • 8. Our Challenges ● Continuously process huge number of events, messages, files ● I/O bound workloads (HTTP requests, DB access, etc) ● Rate limiting from external systems ● Many microservices that interact with each other
  • 11. Little's Law Little's law, Wikipedia long-term average number L of customers in a stationary system is equal to the long-term average effective arrival rate λ multiplied by the average time W that a customer spends in the system
  • 12. Little's Law in Practice Stop Rate Limiting! Capacity Management Done Right" by Jon Moore, Youtube The number of active workers must be at least the average arrival rate of tasks multiplied by the average time to process those tasks workers >= tasks per second x time to process task or workers >= throughput x latency or throughput <= workers / latency
  • 13. Web MVC with Blocking I/O
  • 15. Web MVC with Blocking I/O (multiple threads)
  • 16. Web MVC vs WebFlux Source - Hands-On Reactive Programming in Spring 5 Conclusion - WebFlux is much more efficient with regard to throughput, latency, and CPU usage.
  • 17. Reactive Applications Photo by Brayden Law from Pexels
  • 18. Reactive Glossary “Reactive” has become an overloaded term and is now being associated with several different things to different people ● Reactive programming is a paradigm in which declarative code is issued to construct asynchronous processing pipelines ● Reactive streams is an initiative that was created to provide a standard to unify reactive extensions and deal with asynchronous stream processing with non-blocking backpressure ● Reactive systems—as defined by the Reactive Manifesto—is a set of architectural design principles for building modern systems that are well prepared to meet the increasing demands that applications face today ● Reactive programming and Reactive streams are all useful tools to design and build Reactive systems Reactive programming vs. Reactive systems
  • 20. Reactive Systems ● Responsive - low latency ● Resilient - stay responsive on failures ● Elastic - scale as needed ● Message Driven - async messages for communication between components
  • 21. Reactive Streams Photo by Avery Nielsen-Webb from Pexels
  • 22. Reactive Streams API public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {} public interface Publisher<T> { public void subscribe(Subscriber<? super T> s); } public interface Subscriber<T> { public void onSubscribe(Subscription s); public void onNext(T t); public void onError(Throwable t); public void onComplete(); } public interface Subscription { public void request(long n); public void cancel(); } Don’t try it at home!!!
  • 23. Reactive Signals Photo by Avery Nielsen-Webb from Pexels
  • 24. Reactive Streams Implementations ● RxJava (Netflix) ● Reactor (Pivotal) ● Vert.x (RedHat) ● Akka Streams (Typesafe)
  • 25. Project Reactor Photo by Avery Nielsen-Webb from Pexels
  • 26. Project Reactor Photo by Avery Nielsen-Webb from Pexels ● Reactor is a fourth-generation reactive library, based on the Reactive Streams specification, for building non-blocking applications on the JVM ● Fully non-blocking and backpressure-ready network runtimes, including local TCP/HTTP/UDP clients and servers, based on the robust Netty framework ● Foundation of the reactive stack in the Spring ecosystem and is featured in projects such as Spring WebFlux, Spring Data, and Spring Cloud Gateway
  • 27. Reactive Stack in Spring Boot
  • 28. Web on Reactive Stack ● WebFlux is a non-blocking web stack to handle concurrency with a small number of threads and scale with fewer hardware resources ● Uses Reactor Netty by default ● Supports two programming models ○ Annotated Controllers: Consistent with Spring MVC and based on the same annotations from the spring-web module ○ Functional Endpoints: Lambda-based, lightweight, and functional programming model
  • 29. Reactive Libraries and Clients ● MongoDB, Redis, and Cassandra all have native reactive support in Spring Data ● Many relational databases (Postgres, Microsoft SQL Server, MySQL, H2, and Google Spanner) have reactive support via R2DBC ● Reactor Kafka and Reactor RabbitMQ for messaging ● AWS SDK v2 is fully asynchronous and could be easily wrapped ● WebClient with functional, fluent API based on Reactor ● Resilience4j for Circuit Breaker, Rate Limiter, Retry or Bulkhead in a reactive way
  • 30. Project Reactor 101 Photo by Avery Nielsen-Webb from Pexels Photo by Ekaterina Belinskaya from Pexels
  • 33. Marble Diagrams Photo by Avery Nielsen-Webb from Pexels Appendix B: How to read marble diagrams?
  • 34. Operators ● .map, .filter, .flatMap, .take, .buffer, .subscribe, .... ● Not a part of Reactive Streams specification Appendix A: Which operator do I need?
  • 35. Assembly vs Subscription Photo by Avery Nielsen-Webb from Pexels Nothing Happens Until You Subscribe Flight of the Flux 1 - Assembly vs Subscription ● Calling methods on a Flux or Mono (the operators) doesn’t immediately trigger the behavior. This declarative phase is called assembly time. ● To trigger data to flow we you need to subscribe to the declared pipeline
  • 36. Demo Time Photo by Avery Nielsen-Webb from Pexels Photo by PRAPHAPHAN WONGSAWAN from Pexels
  • 37. flatMap Photo by Avery Nielsen-Webb from Pexels
  • 38. “flatMap Pack” Photo by Avery Nielsen-Webb from Pexels ● .flatMap()/.flatMapMany() - transforms the elements asynchronously into Publishers, then flatten these inner publishers into a single Flux through merging ● .concatMap() - transforms the elements asynchronously into Publishers, then flatten these inner publishers into a single Flux, sequentially and preserving order using concatenation ● .flatMapSequential() - transforms the elements asynchronously into Publishers, then flatten these inner publishers into a single Flux, but merge them in the order of their source element
  • 39. Threading Model Photo by Avery Nielsen-Webb from Pexels ● considered to be concurrency-agnostic ● Schedulers.parallel() ○ non-blocking operations ○ fixed size ○ number of of threads = number CPU cores ● Schedulers.boundedElastic() ○ usually used to offload blocking operations ○ creates new worker pools as needed and reuses idle ones ○ number of of threads = number of CPU cores x 10 ● Schedulers.single() ○ a single, reusable thread Flight of the Flux 3 - Hopping Threads and Schedulers
  • 40. Wrapping Blocking Code Photo by Avery Nielsen-Webb from Pexels ● Reactor offers two means of switching the execution context (or Scheduler) in a reactive chain: publishOn and subscribeOn
  • 41. Error Handling and Resiliency Photo by Avery Nielsen-Webb from Pexels ● .retry()/.retryWhen() - retry subscription on failure ● .repeat()/.repeatWhen() - repeat (resubscribe) on empty result ● .defaultIfEmpty()/.switchIfEmpty() - fallback on empty ● .onErrorResume()/.onErrorReturn() - fallback on error ● .timeout(Duration) - cancel the subscription and fail if no items emitted A.5. Handling Errors
  • 42. Testing Photo by Avery Nielsen-Webb from Pexels
  • 43. Debug and Troubleshooting Photo by Avery Nielsen-Webb from Pexels Flight of the Flux 2 - Debugging Caveats ● Stack traces in Reactive world could veeeeeeery long and not informative
  • 44. Debug and Troubleshooting Photo by Avery Nielsen-Webb from Pexels Flight of the Flux 2 - Debugging Caveats ● Integrate ReactorDebugAgent - a Java agent which helps debugging exceptions in your application without paying a runtime cost (unlike Hooks.onOperatorDebug()) ○ Hooks.onOperatorDebug() is still really useful in tests ● Use .log() and .checkpoint() operators in development to understand the flow ● Integrate BlockHound into tests to detect blocking code (tests only !!!)
  • 45. Reactive “Laws” Photo by Avery Nielsen-Webb from Pexels ● NOTHING happens until you subscribe ○ Idialy subscribe only once ● NEVER block parallel scheduler ○ Run blocking code on a separate Scheduler (i.e. boundedElastic) ● STOP thinking in threads ○ Think about concurrency instead
  • 46. Learning Resources - Intro Photo by Avery Nielsen-Webb from Pexels ● Video Flight of the Flux: A Look at Reactor Execution Model ● Blog series Flight of the Flux 1 - Assembly vs Subscription ● Video Avoiding Reactor Meltdown ● Video Do’s and Don’ts: Avoiding First-Time Reactive Programmer Mines - must ● The introduction to Reactive Programming you've been missing - not java but very good explanation of the idea behind reactive programming
  • 47. Learning Resources - Hands-on Photo by Avery Nielsen-Webb from Pexels ● Introduction to Reactive Programming or the same on github GitHub - reactor/lite-rx-api-hands-on: Lite Rx API Hands-On with Reactor Core 3. Just get repo and fix all unit tests ● Head-First Reactive Workshop GitHub - reactor/head-first-reactive-with-spring-and-reactor
  • 48. Learning Resources - Hardcore Photo by Avery Nielsen-Webb from Pexels 3-video series about Reactor internals ● https:/ /www.youtube.com/watch?v=OdSZ6mOQDcY ● https:/ /www.youtube.com/watch?v=noeWdjO4fyU ● https:/ /www.youtube.com/watch?v=cVKhFPiebSs