What are the benefits of reactive programming in Java? Dmytro Melnychuk
Bio Software Engineer ● 7 years experience in JVM languages twitter.com/meldmy github.com/meldmy
Agenda ● Synchronous vs asynchronous programming ● The key reactive programming concepts ● Current state of Java-based reactive frameworks and toolkits
Have you ever stayed in such long queue? The first McDonald's in Moscow opened in 1990
When can it happen?
When can it happen? When too many clients need to wait for finalization of their orders!
What is the problem with simple order system? We need to stay in queue until all previous orders complete * https://www.vintag.es/2016/03/old-mcdonalds-historical-photos-of.html
How they resolved a problem with a long queue? * https://www.calvertjournal.com/images/uploads/thumbnails/McDonalds/LON29289.jpg * https://external-preview.redd.it/x0cIVo6wWlM8ryudtVHhMmPwvnUkZxOg1_izN5fgwZ8.jpg?auto=webp&s=209c2937112562108309eb5b4b59165bc46c0fb9
The same problem with synchronous blocking I/O Blocking is waiting! Let’s remember! Waiting is doing nothing!
Let’s avoid doing nothing!
Freezing UI and our backend use blocking I/O
Let’s increase the number of server threads?
Let’s increase the number of server threads?
What's wrong with increasing threads and blocking I/O? ➢ Thread-per-connection approach ➢ Thread Context Switching
Thread-per-connection approach * http://www.coralblocks.com/index.php/thread-concurrency-vs-network-asynchronicity/ 1. Thread creating 2. Thread per connection 3. Waiting thread per Keep-Alive connection 4. Context Switching
Context Switching - cores switch between processes * http://www.coralblocks.com/index.php/thread-concurrency-vs-network-asynchronicity/
What about reactive programming?
What is reactive programming? Reactive programming - is a declarative programming paradigm concerned with data streams, the propagation of change [1] Reactive programming - is about non-blocking, event-driven applications that scale with a small number of threads [2] 1. https://en.wikipedia.org/wiki/Reactive_programming 2. https://spring.io/blog/2016/07/28/reactive-programming-with-spring-5-0-m1
How we can double every int in the array in Java? Imperative approach
How we can double every int in the array in Java?
How we can double every int in the array in Java? Imperative approach Declarative approach
Imperative vs declarative programming Imperative programming Declarative programming How you want to do What you want to do
Reactive programming is programming with data streams
Reactive programming concerned with data streams Data streams Non-blocking I/OBlocking I/O Synchronous Asynchronous java.util.stream.* java.util.concurrent.Flow (JDK 9)
Understand the term of propagation of change var A = 1, B = 2; var C = A + B; A = 3; print(C); //=> 5 What should we know about C behavior? ● C is automatically updated when values of A or B change ● C is a observer that subscribes to an observable A and B
Observable needs a small number of threads to scale
The reactor pattern (single-threaded event loop)
Is reactor pattern faster than one-thread-per-connection model?
Why do you think this happens? *https://dzone.com/articles/spring-boot-20-webflux-reactive-performance-test Spring MVC Spring WebFlux Embedded Tomcat (maxThreads 10000) Netty framework
Reactor pattern makes better use of threads/cores! *https://dzone.com/articles/spring-boot-20-webflux-reactive-performance-test Spring MVC Spring WebFlux Embedded Tomcat (maxThreads 10000) Netty framework Thread-Based Architecture Event-driven architecture
Reactive Java libraries
Reactive Java frameworks and toolkits Project Reactor
Thank you twitter.com/meldmy github.com/meldmy

What are the benefits of reactive programming in java

  • 1.
    What are thebenefits of reactive programming in Java? Dmytro Melnychuk
  • 2.
    Bio Software Engineer ● 7years experience in JVM languages twitter.com/meldmy github.com/meldmy
  • 3.
    Agenda ● Synchronous vsasynchronous programming ● The key reactive programming concepts ● Current state of Java-based reactive frameworks and toolkits
  • 4.
    Have you everstayed in such long queue? The first McDonald's in Moscow opened in 1990
  • 5.
    When can ithappen?
  • 6.
    When can ithappen? When too many clients need to wait for finalization of their orders!
  • 7.
    What is theproblem with simple order system? We need to stay in queue until all previous orders complete * https://www.vintag.es/2016/03/old-mcdonalds-historical-photos-of.html
  • 8.
    How they resolveda problem with a long queue? * https://www.calvertjournal.com/images/uploads/thumbnails/McDonalds/LON29289.jpg * https://external-preview.redd.it/x0cIVo6wWlM8ryudtVHhMmPwvnUkZxOg1_izN5fgwZ8.jpg?auto=webp&s=209c2937112562108309eb5b4b59165bc46c0fb9
  • 9.
    The same problemwith synchronous blocking I/O Blocking is waiting! Let’s remember! Waiting is doing nothing!
  • 10.
  • 11.
    Freezing UI andour backend use blocking I/O
  • 12.
    Let’s increase thenumber of server threads?
  • 13.
    Let’s increase thenumber of server threads?
  • 14.
    What's wrong withincreasing threads and blocking I/O? ➢ Thread-per-connection approach ➢ Thread Context Switching
  • 15.
    Thread-per-connection approach * http://www.coralblocks.com/index.php/thread-concurrency-vs-network-asynchronicity/ 1.Thread creating 2. Thread per connection 3. Waiting thread per Keep-Alive connection 4. Context Switching
  • 16.
    Context Switching -cores switch between processes * http://www.coralblocks.com/index.php/thread-concurrency-vs-network-asynchronicity/
  • 17.
    What about reactiveprogramming?
  • 18.
    What is reactiveprogramming? Reactive programming - is a declarative programming paradigm concerned with data streams, the propagation of change [1] Reactive programming - is about non-blocking, event-driven applications that scale with a small number of threads [2] 1. https://en.wikipedia.org/wiki/Reactive_programming 2. https://spring.io/blog/2016/07/28/reactive-programming-with-spring-5-0-m1
  • 19.
    How we candouble every int in the array in Java? Imperative approach
  • 20.
    How we candouble every int in the array in Java?
  • 21.
    How we candouble every int in the array in Java? Imperative approach Declarative approach
  • 22.
    Imperative vs declarativeprogramming Imperative programming Declarative programming How you want to do What you want to do
  • 23.
  • 24.
    Reactive programming concernedwith data streams Data streams Non-blocking I/OBlocking I/O Synchronous Asynchronous java.util.stream.* java.util.concurrent.Flow (JDK 9)
  • 25.
    Understand the termof propagation of change var A = 1, B = 2; var C = A + B; A = 3; print(C); //=> 5 What should we know about C behavior? ● C is automatically updated when values of A or B change ● C is a observer that subscribes to an observable A and B
  • 26.
    Observable needs asmall number of threads to scale
  • 28.
    The reactor pattern(single-threaded event loop)
  • 29.
    Is reactor patternfaster than one-thread-per-connection model?
  • 30.
    Why do youthink this happens? *https://dzone.com/articles/spring-boot-20-webflux-reactive-performance-test Spring MVC Spring WebFlux Embedded Tomcat (maxThreads 10000) Netty framework
  • 31.
    Reactor pattern makesbetter use of threads/cores! *https://dzone.com/articles/spring-boot-20-webflux-reactive-performance-test Spring MVC Spring WebFlux Embedded Tomcat (maxThreads 10000) Netty framework Thread-Based Architecture Event-driven architecture
  • 32.
  • 33.
    Reactive Java frameworksand toolkits Project Reactor
  • 36.