More Related Content Similar to Deep-dive into Reactive programming.pdf (20) Deep-dive into Reactive programming.pdf1. © 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
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
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
9. © 2022 Thoughtworks | Confidential
Netty
9
A framework to create asynchronous, event-driven applications
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
14. © 2022 Thoughtworks | Confidential
Spring Webflux
14
A fully Reactive framework that embraces Functional Reactive Programming
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
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
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