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
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
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.
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
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!!!
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
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
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
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