asynchronous programming using completablefutures in java
TRANSCRIPT
![Page 1: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/1.jpg)
Asynchronous programming using CompletableFutures
![Page 2: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/2.jpg)
Agenda
• What is asynchronous programming?
• Why should I care?
• How to use CompletableFuture for asynchronous calls?
• Does it worth it?
![Page 3: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/3.jpg)
What is asynchronous programming?
![Page 4: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/4.jpg)
Latency Numbers
0.5
25
100
0 20 40 60 80 100 120
ns
Main memory reference
Mutex lock/unlock
L1 Cache Reference
![Page 5: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/5.jpg)
Latency Numbers
100
10,000
150,000
0 20000 40000 60000 80000 100000 120000 140000 160000
ns
Read 4K randomly from SSD
Send 1K over 1 Gbps
Main memory reference
![Page 6: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/6.jpg)
Latency Numbers
150,000
250,000
500,000
10,000,000
0 2,000,000 4,000,000 6,000,000 8,000,000 10,000,000 12,000,000
ns
Disk seek
Round trip within samedatacenter
Read 1 MB sequentially frommemory
Read 4K randomly from SSD
![Page 7: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/7.jpg)
The Hollywood Principle
Don’t call us,
we’ll call you
![Page 8: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/8.jpg)
Why should I care?
![Page 9: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/9.jpg)
Why should I care?
• Process scheduling in modern kernels is pretty mature
• Re-implementing it in user-space is an anti-pattern
• Java relies on kernel process scheduling
![Page 10: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/10.jpg)
It’s about
• Controlling thread lifecycle
• The Nr. of threads and context switches
• I/O operation optimization
![Page 11: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/11.jpg)
Non blocking I/O
Why should I map number of threads to number of connections?
It doesn’t make sense.
![Page 12: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/12.jpg)
The Selector
Client 1
Client 2
Selector
Handler
Handler
![Page 13: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/13.jpg)
Anatomy of a typical Java application
? ?
Your code goes here
![Page 14: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/14.jpg)
How to use CompletableFuturefor asynchronous calls?
![Page 15: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/15.jpg)
A Simple Example
![Page 16: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/16.jpg)
Combining Asynchronous Calls
![Page 17: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/17.jpg)
Using Executors Independently
![Page 18: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/18.jpg)
We Have Exception Handling!
![Page 19: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/19.jpg)
![Page 20: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/20.jpg)
Tomcat 8
HelloService
Small I/O
Scenario 1
![Page 21: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/21.jpg)
Tomcat 8
HelloService
125ms
Scenario 2
In-memory
![Page 22: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/22.jpg)
Tomcat 8
HelloService
125ms
Scenario 3
![Page 23: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/23.jpg)
Tomcat 8
HelloService
Small I/O
Scenario 4
4 threads
![Page 24: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/24.jpg)
Pros & Cons
Pros
• Combinable executors
• Exception handling
• Fast enough execution
• Java language support
• Easy
Cons
• You have to know what you’re doing
• Can be blocking
• Can be slower if Executor scheduling is a bottleneck
![Page 25: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/25.jpg)
Design Considerations
• Blocking 1000 threads because of service failure? Not a smart decision
• Queueing 1000 request-timeouts because of same reasons? Bad idea.• Bit better choice: less pressure on downstream services
• Choose threading and scheduling mechanism wisely• ForkJoinPool is most of the time a bad choice
• Monitor and purge request queues in case of a specific failure rate• Hystrix offers similar mechanism
![Page 26: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/26.jpg)
Questions?
![Page 27: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/27.jpg)
References
GitHub exampleshttps://github.com/gitaroktato/completablefuture-examples
CPU Schedulinghttps://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html
CompletableFuture & Spring Examplehttps://spring.io/guides/gs/async-method/
Non-blocking IO in Javahttps://dnhome.wordpress.com/2012/06/28/java-non-block-io-new-io/
![Page 28: Asynchronous programming using CompletableFutures in Java](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a64cce67f8b9a735d8b49fd/html5/thumbnails/28.jpg)
References
CompletableFuture API Examples
https://www.slideshare.net/jpaumard/asynchronous-api-in-java8-how-to-use-completablefuture
Grizzly NIO frameworkhttps://grizzly.java.net/documentation.html
Scalable IO in Javahttps://www.slideshare.net/giridhar510/scalable-io-in-java
Non-blocking IO in Javahttps://dnhome.wordpress.com/2012/06/28/java-non-block-io-new-io/
RxJava – Multithreadinghttps://praveer09.github.io/technology/2016/02/29/rxjava-part-3-multithreading/