SlideShare a Scribd company logo
1 of 33
Download to read offline
© 2022 Thoughtworks | Confidential
Reactive Programming
Mritunjay Dubey & Kumar Sankara Iyer
© 2022 Thoughtworks | Confidential
Table of contents
Setup 03
Threading models and Demo 04
Reactive programming concepts and Spring Webflux 05
Reactive Data types & Operators and hands-on 06
Corner cases, Error handling and hands-on 07
Reactor context demo 10
Demo of end-to-end streams 13
Miscellaneous topics 17
2
© 2022 Thoughtworks | Confidential
Setup
3
© 2022 Thoughtworks | Confidential
Prerequisites for hands-on
1. Install Java 17 (preferred)
2. Use an IDE like Intellij
3. Install Git (good to have)
4. Install Postman (good to have)
5. Clone the code from
https://github.com/reactive-workshop/reactive-building-blocks
6. Run ./gradlew bootRun
7. Import Reactive-Workshop.postman_collectioninto Postman and run the first
API “Get User By User Id”
4
© 2022 Thoughtworks | Confidential
Threading models
5
© 2022 Thoughtworks | Confidential
Thread-per-request model
6
© 2022 Thoughtworks | Confidential
Drawbacks of the
thread-per-request
model
The thread-per-request model is
neither performant not scalable
Context
switching is
expensive
Threads are
blocked while
I/O is in progress
7
A thread takes a
minimum of 1 MB
of stack size
Threads
increase as the
number of
requests
© 2022 Thoughtworks | Confidential
Event-loop model
8
© 2022 Thoughtworks | Confidential
Netty
9
A framework to create asynchronous, event-driven applications
© 2022 Thoughtworks | Confidential
Demo on threading models
10
© 2022 Thoughtworks | Confidential
Reactive programming concepts and Spring
Webflux
11
© 2022 Thoughtworks | Confidential 12
Asynchronous
1.
Reactive streams
There are several implementations of the Reactive streams specification - RxJava,
Reactor, Akka streams, RxJavaScript and so on
A standard for asynchronous stream processing
Non-blocking
2.
Backpressure
3.
12
© 2022 Thoughtworks
© 2022 Thoughtworks | Confidential
Reactive streams - some keywords
13
© 2022 Thoughtworks | Confidential
Spring Webflux
14
A fully Reactive framework that embraces Functional Reactive Programming
© 2022 Thoughtworks | Confidential
Reactive Data
types & Operators
15
© 2022 Thoughtworks | Confidential
Mono
Mono is a Reactive Streams Publisher that emits at most one item and then terminates
16
16
For eg;
© 2022 Thoughtworks | Confidential
Flux
Flux is a Reactive Streams Publisher that emits 0 to N elements and then completes
17
17
For eg;
© 2022 Thoughtworks | Confidential
map
18
18
© 2022 Thoughtworks
Transforms a Flux to another
Flux by applying a
synchronous function to
each item
© 2022 Thoughtworks | Confidential
flatMap
19
19
© 2022 Thoughtworks
Flatten the inner publishers
into a single Flux through
merging, allowing them to
interleave
© 2022 Thoughtworks | Confidential
filter
20
20
© 2022 Thoughtworks
Evaluate each source value
against the given predicate
© 2022 Thoughtworks | Confidential
zip
21
21
© 2022 Thoughtworks
Wait for all sources to emit
one element, then combine
these elements. Continue till
any of the sources complete
© 2022 Thoughtworks | Confidential
Hands-on with
Reactor operators
22
© 2022 Thoughtworks | Confidential
Corner cases and
Error handling
23
© 2022 Thoughtworks | Confidential
switchIfEmpty
24
24
© 2022 Thoughtworks
Switch to an alternative
publisher if this publisher is
empty
© 2022 Thoughtworks | Confidential
onErrorMap
25
25
© 2022 Thoughtworks
Transform a source error to
another error
© 2022 Thoughtworks | Confidential
onErrorResume
26
26
© 2022 Thoughtworks
Subscribe to a fallback
publisher if any error
happens in source
© 2022 Thoughtworks | Confidential
Hands-on with
error handling
27
© 2022 Thoughtworks | Confidential
Reactor context - a demo
28
© 2022 Thoughtworks | Confidential
End-to-end streams - a demo
29
© 2022 Thoughtworks | Confidential
Miscellaneous
30
© 2022 Thoughtworks | Confidential
Reactive
programming is fun!
Building and running on reactive
applications on production requires
Further reading
Learning curve
31
Blocking calls
A word on
Testing
© 2022 Thoughtworks | Confidential
Discussion
32
© 2022 Thoughtworks | Confidential
Thankyou
33

More Related Content

What's hot

C22 Oracle Database を監視しようぜ! by 山下正/内山義夫
C22 Oracle Database を監視しようぜ! by 山下正/内山義夫C22 Oracle Database を監視しようぜ! by 山下正/内山義夫
C22 Oracle Database を監視しようぜ! by 山下正/内山義夫
Insight Technology, Inc.
 

What's hot (20)

C22 Oracle Database を監視しようぜ! by 山下正/内山義夫
C22 Oracle Database を監視しようぜ! by 山下正/内山義夫C22 Oracle Database を監視しようぜ! by 山下正/内山義夫
C22 Oracle Database を監視しようぜ! by 山下正/内山義夫
 
Cours 1 introduction
Cours 1 introductionCours 1 introduction
Cours 1 introduction
 
Android is NOT just 'Java on Linux'
Android is NOT just 'Java on Linux'Android is NOT just 'Java on Linux'
Android is NOT just 'Java on Linux'
 
Rootless Containers & Unresolved issues
Rootless Containers & Unresolved issuesRootless Containers & Unresolved issues
Rootless Containers & Unresolved issues
 
Vulkan 1.0 Quick Reference
Vulkan 1.0 Quick ReferenceVulkan 1.0 Quick Reference
Vulkan 1.0 Quick Reference
 
Embedded Android Workshop with Oreo
Embedded Android Workshop with OreoEmbedded Android Workshop with Oreo
Embedded Android Workshop with Oreo
 
Rapport DVWA: File Upload
Rapport DVWA: File UploadRapport DVWA: File Upload
Rapport DVWA: File Upload
 
groovy and concurrency
groovy and concurrencygroovy and concurrency
groovy and concurrency
 
Presentation linux on power
Presentation   linux on powerPresentation   linux on power
Presentation linux on power
 
Cours d'algorithmique distribuée (2010-2011)
Cours d'algorithmique distribuée (2010-2011)Cours d'algorithmique distribuée (2010-2011)
Cours d'algorithmique distribuée (2010-2011)
 
Programmation sous Android
Programmation sous AndroidProgrammation sous Android
Programmation sous Android
 
Podman rootless containers
Podman rootless containersPodman rootless containers
Podman rootless containers
 
cours-gratuit.com--id-4422.pdf
cours-gratuit.com--id-4422.pdfcours-gratuit.com--id-4422.pdf
cours-gratuit.com--id-4422.pdf
 
P2 éléments graphiques android
P2 éléments graphiques androidP2 éléments graphiques android
P2 éléments graphiques android
 
基礎から学ぶ超並列SQLエンジンImpala #cwt2015
基礎から学ぶ超並列SQLエンジンImpala #cwt2015基礎から学ぶ超並列SQLエンジンImpala #cwt2015
基礎から学ぶ超並列SQLエンジンImpala #cwt2015
 
alphorm.com - Formation Oracle Database 11g DBA 1 (1Z0-052)
alphorm.com - Formation Oracle Database 11g DBA 1 (1Z0-052)alphorm.com - Formation Oracle Database 11g DBA 1 (1Z0-052)
alphorm.com - Formation Oracle Database 11g DBA 1 (1Z0-052)
 
A deep dive into Android OpenSource Project(AOSP)
A deep dive into Android OpenSource Project(AOSP)A deep dive into Android OpenSource Project(AOSP)
A deep dive into Android OpenSource Project(AOSP)
 
Les framework mvc
Les framework mvcLes framework mvc
Les framework mvc
 
Partie 2: Angular
Partie 2: AngularPartie 2: Angular
Partie 2: Angular
 
Cours java
Cours javaCours java
Cours java
 

Similar to Deep-dive into Reactive programming.pdf

Data Engineer's Lunch #86: Building Real-Time Applications at Scale: A Case S...
Data Engineer's Lunch #86: Building Real-Time Applications at Scale: A Case S...Data Engineer's Lunch #86: Building Real-Time Applications at Scale: A Case S...
Data Engineer's Lunch #86: Building Real-Time Applications at Scale: A Case S...
Anant Corporation
 

Similar to Deep-dive into Reactive programming.pdf (20)

Announcing: Native MQTT Integration with HiveMQ and InfluxDB Cloud
Announcing: Native MQTT Integration with HiveMQ and InfluxDB Cloud Announcing: Native MQTT Integration with HiveMQ and InfluxDB Cloud
Announcing: Native MQTT Integration with HiveMQ and InfluxDB Cloud
 
"Concurrent React 18. "I might do it later"", Mykola Yenin
"Concurrent React 18. "I might do it later"", Mykola Yenin "Concurrent React 18. "I might do it later"", Mykola Yenin
"Concurrent React 18. "I might do it later"", Mykola Yenin
 
CodiLime Tech Talk - Wojciech Urbański: Cloud Native
CodiLime Tech Talk - Wojciech Urbański: Cloud NativeCodiLime Tech Talk - Wojciech Urbański: Cloud Native
CodiLime Tech Talk - Wojciech Urbański: Cloud Native
 
CDK - The next big thing - Quang Phuong
CDK - The next big thing - Quang PhuongCDK - The next big thing - Quang Phuong
CDK - The next big thing - Quang Phuong
 
RTP Bluemix Meetup April 20th 2016
RTP Bluemix Meetup April 20th 2016RTP Bluemix Meetup April 20th 2016
RTP Bluemix Meetup April 20th 2016
 
Master Real-Time Streams With Neo4j and Apache Kafka
Master Real-Time Streams With Neo4j and Apache KafkaMaster Real-Time Streams With Neo4j and Apache Kafka
Master Real-Time Streams With Neo4j and Apache Kafka
 
Infrastructure As Code
Infrastructure As CodeInfrastructure As Code
Infrastructure As Code
 
Building Event-Driven Microservices using Kafka Streams (Stathis Souris, Thou...
Building Event-Driven Microservices using Kafka Streams (Stathis Souris, Thou...Building Event-Driven Microservices using Kafka Streams (Stathis Souris, Thou...
Building Event-Driven Microservices using Kafka Streams (Stathis Souris, Thou...
 
Second Presentation.pptx
Second Presentation.pptxSecond Presentation.pptx
Second Presentation.pptx
 
Dealing with large code bases. cd ams meetup
Dealing with large code bases. cd ams meetupDealing with large code bases. cd ams meetup
Dealing with large code bases. cd ams meetup
 
Kubernetes: Increasing velocity without sacrificing quality
Kubernetes: Increasing velocity without sacrificing qualityKubernetes: Increasing velocity without sacrificing quality
Kubernetes: Increasing velocity without sacrificing quality
 
Russ Savage [Ngrok] | InfluxDB QuickStart | InfluxDays NA 2021
Russ Savage [Ngrok] | InfluxDB QuickStart | InfluxDays NA 2021Russ Savage [Ngrok] | InfluxDB QuickStart | InfluxDays NA 2021
Russ Savage [Ngrok] | InfluxDB QuickStart | InfluxDays NA 2021
 
Enabling Devops using Jenkins
Enabling Devops using JenkinsEnabling Devops using Jenkins
Enabling Devops using Jenkins
 
Building Event-Driven Workflows with Knative and Tekton
Building Event-Driven Workflows with Knative and TektonBuilding Event-Driven Workflows with Knative and Tekton
Building Event-Driven Workflows with Knative and Tekton
 
Web Performance & Latest in React
Web Performance & Latest in ReactWeb Performance & Latest in React
Web Performance & Latest in React
 
Introduction to NodeJS
Introduction to NodeJSIntroduction to NodeJS
Introduction to NodeJS
 
Build Enterprise Level Solution using Logic Apps ,Functions and Graph API
Build Enterprise Level Solution using Logic Apps ,Functions and Graph API Build Enterprise Level Solution using Logic Apps ,Functions and Graph API
Build Enterprise Level Solution using Logic Apps ,Functions and Graph API
 
Background Tasks with Worker Service
Background Tasks with Worker ServiceBackground Tasks with Worker Service
Background Tasks with Worker Service
 
2020 03-26 - meet up - zparkio
2020 03-26 - meet up - zparkio2020 03-26 - meet up - zparkio
2020 03-26 - meet up - zparkio
 
Data Engineer's Lunch #86: Building Real-Time Applications at Scale: A Case S...
Data Engineer's Lunch #86: Building Real-Time Applications at Scale: A Case S...Data Engineer's Lunch #86: Building Real-Time Applications at Scale: A Case S...
Data Engineer's Lunch #86: Building Real-Time Applications at Scale: A Case S...
 

Recently uploaded

一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样
一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样
一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样
A
 
一比一原版(Griffith毕业证书)格里菲斯大学毕业证成绩单学位证书
一比一原版(Griffith毕业证书)格里菲斯大学毕业证成绩单学位证书一比一原版(Griffith毕业证书)格里菲斯大学毕业证成绩单学位证书
一比一原版(Griffith毕业证书)格里菲斯大学毕业证成绩单学位证书
c3384a92eb32
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdf
Kira Dess
 

Recently uploaded (20)

SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
Geometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdfGeometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdf
 
一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样
一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样
一比一原版(NEU毕业证书)东北大学毕业证成绩单原件一模一样
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata Model
 
一比一原版(Griffith毕业证书)格里菲斯大学毕业证成绩单学位证书
一比一原版(Griffith毕业证书)格里菲斯大学毕业证成绩单学位证书一比一原版(Griffith毕业证书)格里菲斯大学毕业证成绩单学位证书
一比一原版(Griffith毕业证书)格里菲斯大学毕业证成绩单学位证书
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligence
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
Databricks Generative AI Fundamentals .pdf
Databricks Generative AI Fundamentals  .pdfDatabricks Generative AI Fundamentals  .pdf
Databricks Generative AI Fundamentals .pdf
 
Basics of Relay for Engineering Students
Basics of Relay for Engineering StudentsBasics of Relay for Engineering Students
Basics of Relay for Engineering Students
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
History of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationHistory of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & Modernization
 
Working Principle of Echo Sounder and Doppler Effect.pdf
Working Principle of Echo Sounder and Doppler Effect.pdfWorking Principle of Echo Sounder and Doppler Effect.pdf
Working Principle of Echo Sounder and Doppler Effect.pdf
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdf
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentation
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 

Deep-dive into Reactive programming.pdf

  • 1. © 2022 Thoughtworks | Confidential Reactive Programming Mritunjay Dubey & Kumar Sankara Iyer
  • 2. © 2022 Thoughtworks | Confidential Table of contents Setup 03 Threading models and Demo 04 Reactive programming concepts and Spring Webflux 05 Reactive Data types & Operators and hands-on 06 Corner cases, Error handling and hands-on 07 Reactor context demo 10 Demo of end-to-end streams 13 Miscellaneous topics 17 2
  • 3. © 2022 Thoughtworks | Confidential Setup 3
  • 4. © 2022 Thoughtworks | Confidential Prerequisites for hands-on 1. Install Java 17 (preferred) 2. Use an IDE like Intellij 3. Install Git (good to have) 4. Install Postman (good to have) 5. Clone the code from https://github.com/reactive-workshop/reactive-building-blocks 6. Run ./gradlew bootRun 7. Import Reactive-Workshop.postman_collectioninto Postman and run the first API “Get User By User Id” 4
  • 5. © 2022 Thoughtworks | Confidential Threading models 5
  • 6. © 2022 Thoughtworks | Confidential Thread-per-request model 6
  • 7. © 2022 Thoughtworks | Confidential Drawbacks of the thread-per-request model The thread-per-request model is neither performant not scalable Context switching is expensive Threads are blocked while I/O is in progress 7 A thread takes a minimum of 1 MB of stack size Threads increase as the number of requests
  • 8. © 2022 Thoughtworks | Confidential Event-loop model 8
  • 9. © 2022 Thoughtworks | Confidential Netty 9 A framework to create asynchronous, event-driven applications
  • 10. © 2022 Thoughtworks | Confidential Demo on threading models 10
  • 11. © 2022 Thoughtworks | Confidential Reactive programming concepts and Spring Webflux 11
  • 12. © 2022 Thoughtworks | Confidential 12 Asynchronous 1. Reactive streams There are several implementations of the Reactive streams specification - RxJava, Reactor, Akka streams, RxJavaScript and so on A standard for asynchronous stream processing Non-blocking 2. Backpressure 3. 12 © 2022 Thoughtworks
  • 13. © 2022 Thoughtworks | Confidential Reactive streams - some keywords 13
  • 14. © 2022 Thoughtworks | Confidential Spring Webflux 14 A fully Reactive framework that embraces Functional Reactive Programming
  • 15. © 2022 Thoughtworks | Confidential Reactive Data types & Operators 15
  • 16. © 2022 Thoughtworks | Confidential Mono Mono is a Reactive Streams Publisher that emits at most one item and then terminates 16 16 For eg;
  • 17. © 2022 Thoughtworks | Confidential Flux Flux is a Reactive Streams Publisher that emits 0 to N elements and then completes 17 17 For eg;
  • 18. © 2022 Thoughtworks | Confidential map 18 18 © 2022 Thoughtworks Transforms a Flux to another Flux by applying a synchronous function to each item
  • 19. © 2022 Thoughtworks | Confidential flatMap 19 19 © 2022 Thoughtworks Flatten the inner publishers into a single Flux through merging, allowing them to interleave
  • 20. © 2022 Thoughtworks | Confidential filter 20 20 © 2022 Thoughtworks Evaluate each source value against the given predicate
  • 21. © 2022 Thoughtworks | Confidential zip 21 21 © 2022 Thoughtworks Wait for all sources to emit one element, then combine these elements. Continue till any of the sources complete
  • 22. © 2022 Thoughtworks | Confidential Hands-on with Reactor operators 22
  • 23. © 2022 Thoughtworks | Confidential Corner cases and Error handling 23
  • 24. © 2022 Thoughtworks | Confidential switchIfEmpty 24 24 © 2022 Thoughtworks Switch to an alternative publisher if this publisher is empty
  • 25. © 2022 Thoughtworks | Confidential onErrorMap 25 25 © 2022 Thoughtworks Transform a source error to another error
  • 26. © 2022 Thoughtworks | Confidential onErrorResume 26 26 © 2022 Thoughtworks Subscribe to a fallback publisher if any error happens in source
  • 27. © 2022 Thoughtworks | Confidential Hands-on with error handling 27
  • 28. © 2022 Thoughtworks | Confidential Reactor context - a demo 28
  • 29. © 2022 Thoughtworks | Confidential End-to-end streams - a demo 29
  • 30. © 2022 Thoughtworks | Confidential Miscellaneous 30
  • 31. © 2022 Thoughtworks | Confidential Reactive programming is fun! Building and running on reactive applications on production requires Further reading Learning curve 31 Blocking calls A word on Testing
  • 32. © 2022 Thoughtworks | Confidential Discussion 32
  • 33. © 2022 Thoughtworks | Confidential Thankyou 33