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

Reactive Applications in Java

  • 1.
    Reactive Applications inJava Alex Mrynskyi Palo Alto Networks
  • 2.
  • 3.
    Agenda ● The Problem ●What is Reactive Applications? ● Project Reactor 101 ● How to Start with Reactive?
  • 4.
  • 5.
    Today’s Challenges Today applicationsare 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 ofMicroservices
  • 7.
    The Rise ofMicroservices
  • 8.
    Our Challenges ● Continuouslyprocess 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
  • 9.
  • 10.
  • 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 inPractice 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 withBlocking I/O
  • 14.
  • 15.
    Web MVC withBlocking I/O (multiple threads)
  • 16.
    Web MVC vsWebFlux 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 byBrayden Law from Pexels
  • 18.
    Reactive Glossary “Reactive” hasbecome 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
  • 19.
  • 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 byAvery Nielsen-Webb from Pexels
  • 22.
    Reactive Streams API publicinterface 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 byAvery Nielsen-Webb from Pexels
  • 24.
    Reactive Streams Implementations ●RxJava (Netflix) ● Reactor (Pivotal) ● Vert.x (RedHat) ● Akka Streams (Typesafe)
  • 25.
    Project Reactor Photo byAvery Nielsen-Webb from Pexels
  • 26.
    Project Reactor Photo byAvery 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 inSpring Boot
  • 28.
    Web on ReactiveStack ● 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 andClients ● 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 Photoby Avery Nielsen-Webb from Pexels Photo by Ekaterina Belinskaya from Pexels
  • 31.
  • 32.
  • 33.
    Marble Diagrams Photo byAvery 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 Photoby 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 byAvery Nielsen-Webb from Pexels Photo by PRAPHAPHAN WONGSAWAN from Pexels
  • 37.
    flatMap Photo by AveryNielsen-Webb from Pexels
  • 38.
    “flatMap Pack” Photo byAvery 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 byAvery 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 Photoby 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 andResiliency 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 AveryNielsen-Webb from Pexels
  • 43.
    Debug and Troubleshooting Photoby 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 Photoby 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 byAvery 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