don't block your mobiles and internet of things · •elastic (scalable) - ”!e system stays...
TRANSCRIPT
![Page 1: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/1.jpg)
| CALLISTAENTERPRISE.SE
DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS
MAGNUS LARSSON, PÄR WENÅKER, ANDERS ASPLUND
2014-10-23
Use non blocking I/O for scalable and resilient server applications
![Page 2: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/2.jpg)
• The Big Picture
• Demonstration
• Details
• Experiences from a real life projects
• Summary & next step
AGENDA
![Page 3: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/3.jpg)
INTRODUCTION
3
The Big Picture
![Page 4: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/4.jpg)
THE SCALABILITY CHALLENGE…
4 Source: http://www.theconnectivist.com/2014/05/ infographic-the-growth-of-the-internet-of-things/
![Page 5: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/5.jpg)
…SERVICES ARE CONNECTED…
5 Source: http://techblog.netflix.com/2013/01/ announcing-ribbon-tying-netflix-mid.html
![Page 6: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/6.jpg)
…SERVICES FAILS…
6 Source: http://techblog.netflix.com/2013/01/ announcing-ribbon-tying-netflix-mid.html
![Page 7: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/7.jpg)
WATCH OUT FOR THE DOMINO EFFECT!
7 Source: http://techblog.netflix.com/2013/01/ announcing-ribbon-tying-netflix-mid.html
![Page 8: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/8.jpg)
WATCH OUT FOR THE DOMINO EFFECT!
8 Source: http://techblog.netflix.com/2013/01/ announcing-ribbon-tying-netflix-mid.html
Resilience is as important as scalability
![Page 9: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/9.jpg)
• http://www.reactivemanifesto.org
THE REACTIVE MANIFESTO
9
![Page 10: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/10.jpg)
• Responsive - ”The system responds in a timely manner if at all possible. Responsiveness is the
cornerstone of usability and utility, but more than that, responsiveness means that problems may be detected quickly and dealt with effectively. Responsive systems focus on providing rapid and consistent response times, establishing reliable upper bounds so they deliver a consistent quality of service. This consistent behaviour in turn simplifies error handling, builds end user confidence, and encourages further interaction.”
THE REACTIVE MANIFESTO
10
![Page 11: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/11.jpg)
• Elastic (scalable) - ”The system stays responsive under varying workload. Reactive Systems can
react to changes in the input rate by increasing or decreasing the resources allocated to service these inputs. This implies designs that have no contention points or central bottlenecks, resulting in the ability to shard or replicate components and distribute inputs among them. Reactive Systems support predictive, as well as Reactive, scaling algorithms by providing relevant live performance measures. They achieve elasticity in a cost-effective way on commodity hardware and software platforms.”
THE REACTIVE MANIFESTO
11
![Page 12: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/12.jpg)
• Resilient - ” The system stays responsive in the face of failure. This applies not only to
highly-available, mission critical systems — any system that is not resilient will be unresponsive after a failure. Resilience is achieved by replication, containment, isolation and delegation. Failures are contained within each component, isolating components from each other and thereby ensuring that parts of the system can fail and recover without compromising the system as a whole. Recovery of each component is delegated to another (external) component and high-availability is ensured by replication where necessary. The client of a component is not burdened with handling its failures.”
THE REACTIVE MANIFESTO
12
![Page 13: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/13.jpg)
• Message driven - ” Reactive Systems rely on asynchronous message-passing to establish a
boundary between components that ensures loose coupling, isolation, location transparency, and provides the means to delegate errors as messages. Employing explicit message-passing enables load management, elasticity, and flow control by shaping and monitoring the message queues in the system and applying back-pressure when necessary. Location transparent messaging as a means of communication makes it possible for the management of failure to work with the same constructs and semantics across a cluster or within a single host. Non-blocking communication allows recipients to only consume resources while active, leading to less system overhead.”
THE REACTIVE MANIFESTO
13
![Page 14: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/14.jpg)
HOW DO WE FULFILL THE REACTIVE MANIFEST0???
14
?
![Page 15: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/15.jpg)
TECHNICAL CHALLENGES…
15
• Large scale of concurrent requests
• Load balancing • Circuit breaker
• Large number of services • Distributed configuration • Service registration and
discovery
• Responsive services require more efficient protocols than HTTP
• WebSockets • IoT protocols
(http://iot.eclipse.org/) • MQTT, CoAP, LWM2M…
• Large number of connected services • Configurable routing • Resilient service-to-service calls
![Page 16: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/16.jpg)
THE FUNDAMENT OF REACTIVE SYSTEMS
16
Asynchronous & Non Blocking I/O
![Page 17: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/17.jpg)
• Asynchronous processing ≠ non blocking I/O
• Asynchronous processing means that a thread hands over processing to another thread
• Non blocking I/O means that a thread is not waiting for external resources, such as databases or another services
➜Asynchronous processing can either use blocking I/O or non blocking I/O!
- Stay tuned for examples…
…at least for the scope of this presentation… ASYNCHRONOUS PROCESSING VS NON BLOCKING I/O
17
![Page 18: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/18.jpg)
• No!!! • A short history lesson.. - Non blocking I/O has been supported in operating systems for ever
IS NON-BLOCKING I/O NEW?
18
2002 Java SE v1.4 New I/O (NIO) – fundamental support for non blocking I/O in the Java platform
… Netty, Jetty, … Early adopting web server and frameworks improved the NIO support
2006 Akka, … Application frameworks that use specific non blocking web servers & frameworks, e.g. Netty and Jetty
2009 Servlet 3.0 specification A portable specification for non blocking I/O based HTTP – services!
2010 NING asynch-http-client A non blocking I/O HTTP - client
2012 Spring MVC 3.2 Spring support for non blocking I/O based HTTP – services. Based on Servlet 3.0 but much easier to use!!!
2013 Servlet 3.1 specification Enhanced non blocking I/O based HTTP – services
2013 Spring Framework 4.0 Includes Spring MVC as a core part of the Framework
![Page 19: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/19.jpg)
TRADITIONAL BLOCKING I/O
19
![Page 20: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/20.jpg)
TRADITIONAL BLOCKING I/O
20
Slow external resource, e.g. a database or another service
![Page 21: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/21.jpg)
TRADITIONAL BLOCKING I/O
21
The precious thread is busy doing nothing at all…
At high load or temp outage of external resources thread-pools are quickly filled up
resulting in all kinds of problems…
![Page 22: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/22.jpg)
NON-BLOCKING I/O
22
![Page 23: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/23.jpg)
NON-BLOCKING I/O
23
No threads are locked while waiting for slow external resources!
![Page 24: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/24.jpg)
WHAT ABOUT BLOCKING RESOURCE API’S??? (E.G. JDBC)
24
![Page 25: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/25.jpg)
BLOCKING RESOURCE API’S, E.G. JDBC
25
Have we simply moved the bottleneck one step?
![Page 26: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/26.jpg)
Blocking Resource
Servlet Container
Service #1
.
.
.
Service #2
Service #3
Service #n
Non-blocking Resource
Blocking Resource API
Non-blocking Resource API
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
IN A MIXED ENVIRONMENT PROBLEMS CAN BE MITIGATED!
![Page 27: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/27.jpg)
Thread Pool
Thread Pool
Blocking Resource
Servlet Container
Service #1
.
.
.
Service #2
Service #3
Service #n
Non-blocking Resource
Blocking Resource API
Non-blocking Resource API
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
IN A MIXED ENVIRONMENT PROBLEMS CAN BE MITIGATED!
Thread Pool
![Page 28: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/28.jpg)
Thread Pool
Thread Pool
Thread Pool
Blocking Resource
Servlet Container
Service #1
.
.
.
Service #2
Service #3
Service #n
Non-blocking Resource
Blocking Resource API
Non-blocking Resource API
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
IN A MIXED ENVIRONMENT PROBLEMS CAN BE MITIGATED
A drained thread pool for a blocking resource API
only affects parts of the services
![Page 29: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/29.jpg)
INTRODUCTION
29
DEMONSTRATION
![Page 30: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/30.jpg)
AN EXAMPLE OF POTENTIAL PROBLEMS WITH BLOCKING I/O
• National reference architecture • Standardized protocols • Standardized message formats • Service catalog for routing • In operation since 2010
- > 2000 connected care units - > 500 000 messages/day (8h)
National Healthcare Service Platform
30
![Page 31: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/31.jpg)
VIEW FROM THE RUNNING SYSTEM IN PRODUCTION
31
![Page 32: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/32.jpg)
HIGH LEVEL ARCHITECTURE…
32
Router
Routing Database
Service Consumer
Service
Service
Service
Service
Service
Consumer
Consumer
Consumer
Consumer
Consumer
![Page 33: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/33.jpg)
SIMULATION OF THE ENVIRONMENT
33
Teststub- Service
non-blocking-!processing!
set-default-!processing-time!
Router
Routing Database
blocking-!routing!
non-blocking-!routing!
Test- Consumer
load!
publish-!data!
$ curl "http://localhost:9100/load?! port=9080&! uri=router-blocking&! minMs=3000&! maxMs=6000&! tps=50"!
![Page 34: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/34.jpg)
SAMPLE OUTPUT FROM A LOAD TEST
34
Teststub- Service
Router
Response Time
Processing Time
![Page 35: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/35.jpg)
• Normal load is - 20 – 50 reqs/s - Service Provider response times: 3-6 s - Default request timeout: 10 s
• Start with 20 reqs/s • Step up to 50 reqs/s • If ok - Add a increase of load, 65 reqs/s - Add a minor problem, increase response times by 1s - What happens? - Why?
• Switch to non blocking I/O and go unleashed!!!
DEMO
35
![Page 36: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/36.jpg)
MAIN
36
THE DETAILS…
![Page 37: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/37.jpg)
• Servlet 3.0, JSR 315, dec 2009, Java EE 6 - Async Request Handling - Threads allocated to connection when needed - AsyncContext - Requires servlets and filters to declare
that they are async enabled - A portable API for non blocking I/O based HTTP services!
» Web apps can be moved between e.g. Tomcat and Jetty
• Servlet 3.1, JSR 340, apr 2013, Java EE 7 - Async I/O - Allow writing and reading large responses and
requests without blocking
SERVLET 3.X
37
![Page 38: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/38.jpg)
EXAMPLE SERVLET 3.0
38
@WebServlet(name="...", urlPatterns={"/..."}, asyncSupported=true) public class MyServlet extends HttpServlet { ExecutorService executorService = ...; public void doGet(HttpServletRequest request, HttpServletResponse response) { AsyncContext aCtx = request.startAsync(request, response); executorService.submit(() -> { doSomeLongRunningWork(aCtx); aCtx.dispatch("/WEB-INF/jsp/page.jsp"); }); } }
Enable async
Start an async context
Dispatch work on a separat thread
Dispatch back to servlet container and complete
async context
![Page 39: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/39.jpg)
• Simplifies development of HTTP services
• Annotation driven
• Inversion of Control
• Takes care of all the mess details with good default values - Convention over Configuration - Any default behavior can be overridden, e.g. using an annotation - E.g. marshal/unmarshal to json or xml, handling of http headers…
• Plays nicely with Spring Boot
INTRO TO SPRING MVC
39
![Page 40: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/40.jpg)
1. Blocking vs non blocking services
2. Asynch call to a non blocking resource a. Callback vs anonymous inner class vs Java 8 Lambda expressions
3. Asynch call to a blocking resource
4. Patterns a. Router, Aggregator and Routing Slip
5. Routing slip - example of “the callback hell”
• All examples comes from the git-repo in the blog: - http://callistaenterprise.se/blogg/teknik/2014/04/22/
c10k-developing-non-blocking-rest-services-with-spring-mvc/ - Look in the master branch
SPRING MVC – CODE EXAMPLES
40
![Page 41: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/41.jpg)
1. SPRING MVC – BLOCKING I/O VS. NON BLOCKING I/O
41
@RestController!public class MyController {!! @RequestMapping("/block")! public R block(...) {! ...! return new R(...);! }!}!
BLOCKING I/O
![Page 42: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/42.jpg)
1. SPRING MVC –BLOCKING I/O VS. NON BLOCKING I/O
42
@RestController!public class MyController {!! @RequestMapping("/block")! public R block(...) {! ...! return new R(...);! }!}!
@RestController!public class ProcessingController {!! @RequestMapping("/non-block")! public DeferredResult<R> nonBlock(...) {!! DeferredResult<R> dr = ! new DeferredResult<>();! dispatch(new MyTask(dr, ...));! return dr;! }!}
public class MyTask extends MyCallback {!! private DeferredResult<R> deferredResult;! public MyTask(DeferredResult<R> dr, ...) {! this.df = df;! }! public void done() {! df.setResult(new R(...));! }!}!
BLOCKING I/O NON BLOCKING I/O
![Page 43: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/43.jpg)
1. SPRING MVC –BLOCKING I/O VS. NON BLOCKING I/O
43
@RestController!public class MyController {!! @RequestMapping("/block")! public R block(...) {! ...! return new R(...);! }!}!
@RestController!public class ProcessingController {!! @RequestMapping("/non-block")! public DeferredResult<R> nonBlock(...) {!! DeferredResult<R> dr = new DeferredResult<>();! dispatch(new MyTask(dr, ...));! return dr;! }!}
public class MyTask implements MyCallback {!! private DeferredResult<R> deferredResult;! public MyTask(DeferredResult<R> dr, ...) {! this.df = df;! }! public void done() {! df.setResult(new R(...));! }!}!
BLOCKING I/O NON BLOCKING I/O
CALLBACK MODEL
DeferredResult IS THE KEY SPRING MVC ABSTRACTION!
![Page 44: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/44.jpg)
• Non Blocking I/O HTTP calls with Ning async-http-client 2.A ASYNCH CALL TO A NON BLOCKING RESOURCE
44
private static final AsyncHttpClient asyncHttpClient = new AsyncHttpClient();!!@RequestMapping("/router-non-blocking-callback")!public DeferredResult<String> nonBlockingRouter(...) {!! DeferredResult<String> dr = new DeferredResult<String>();! asyncHttpClient.prepareGet(getUrl(...)).execute(new MyCallback(dr));! return dr;!}!
public class MyCallback extends AsyncCompletionHandler<Response> {!! private DeferredResult<String> dr;! public MyCallback(DeferredResult<String> dr) {! this.dr = dr;! }!! public Response onCompleted(Response response) {! dr.setResult(response.getResponseBody());! }!! public void onThrowable(Throwable t){...}!}!
![Page 45: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/45.jpg)
2.B ANONYMOUS INNER CLASS
45
@RequestMapping("/router-non-blocking-anonymous")!public DeferredResult<String> nonBlockingRouter(...) {! ! final DeferredResult<String> dr = new DeferredResult<>();! ! asyncHttpClient.prepareGet(getUrl(..)).execute(! ! new AsyncCompletionHandler<Response>() {!! public Response onCompleted(Response response) {! dr(response.getResponseBody());! }!! public void onThrowable(Throwable t){...}! });!! return dr;!}!
![Page 46: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/46.jpg)
2.C JAVA 8 AND LAMBDAS
46
@RequestMapping("/router-non-blocking-lambda")!public DeferredResult<String> nonBlockingRouter(...) {!! final DeferredResult<String> dr = new DeferredResult<>();!! asyncHttpClient.execute(getUrl(...),! (response) -> {! dr.setResult(response.getResponseBody());! },! (throwable) -> {...}! );!! return dr;!}!
![Page 47: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/47.jpg)
3. ASYNCH CALL TO A BLOCKING RESOURCE
47
@Autowired!@Qualifier("dbThreadPoolExecutor")!private TaskExecutor dbThreadPoolExecutor;!!@RequestMapping("/aggregate-non-blocking-callback")!public DeferredResult<String> nonBlockingAggregator(...) {!! DeferredResult<String> dr= new DeferredResult<String>();! dbThreadPoolExecutor.execute(new DbLookupRunnable(dr, ...));! return dr;!}!
public class DbLookupRunnable implements Runnable {!! private DeferredResult<String> dr;! public DbLookupRunnable(DeferredResult<String> dr, ...) {! this.deferredResult = dr;! }! public void run() {! // Perform blocking database operation! ...! dr.setResult(responseFromDatabaseOperation)! }!}!
![Page 48: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/48.jpg)
4.A PATTERNS - ROUTER
48
Router
Routing Database
Service Consumer
Service
Service
Service
Service
Service
Consumer
Consumer
Consumer
Consumer
Consumer
![Page 49: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/49.jpg)
4.B PATTERNS - AGGREGATOR
49
Aggregator
Lookup Database
Service Consumer
Service
Service
Service
Service
Service
Consumer
Consumer
Consumer
Consumer
Consumer
Calls to the service are made in parallel
![Page 50: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/50.jpg)
4.C PATTERNS – ROUTING SLIP
50
Routing Slip
Processing Step #1
Consumer
Processing Step #2
Processing Step #3
Processing Step #n
Service
Service Processing steps are: • Blocking or non-blocking • Internal only or use
external services • Unknown to its numbers
Service
Service
Service
Service
Consumer
Consumer
Consumer
![Page 51: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/51.jpg)
• Perform 5 sequential Non Blocking I/O calls…
5. ROUTING SLIP - EXAMPLE OF “THE CALLBACK HELL”
51
@RequestMapping("/routing-slip-non-blocking-lambda")!public DeferredResult<String> nonBlockingRoutingSlip() throws IOException {!! final DeferredResult<String> dr = new DeferredResult<>();!! // Send request #1! ListenableFuture<Response> execute = asyncHttpClient.execute(getUrl(1),! (Response r1) -> {! processResult(r1.getResponseBody()); // Process response #1!
// HOW TO SEND REQUEST #2 ???!
![Page 52: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/52.jpg)
• Perform 5 sequential Non Blocking I/O calls…
5. ROUTING SLIP - EXAMPLE OF “THE CALLBACK HELL”
52
@RequestMapping("/routing-slip-non-blocking-lambda")!public DeferredResult<String> nonBlockingRoutingSlip() throws IOException {!! final DeferredResult<String> dr = new DeferredResult<>();!! // Send request #1! ListenableFuture<Response> execute = asyncHttpClient.execute(getUrl(1),! (Response r1) -> {! processResult(r1.getResponseBody()); // Process response #1! asyncHttpClient.execute(getUrl(2), // Send request #2! (Response r2) -> {! processResult(r2.getResponseBody()); // Process response #2! asyncHttpClient.execute(getUrl(3), // Send request #3! (Response r3) -> {! processResult(r3.getResponseBody()); // Process response #3! asyncHttpClient.execute(getUrl(4), // Send request #4! (Response r4) -> {! processResult(r4.getResponseBody()); // Process response #4! asyncHttpClient.execute(getUrl(5), // Send request #5! (Response r5) -> {! processResult(r5.getResponseBody()); // Process response #5! // Get the total result and set it on the deferred result! dr.setResult(getTotalResult());! ...
![Page 53: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/53.jpg)
• Perform 5 sequential Non Blocking I/O calls…
5. ROUTING SLIP - EXAMPLE OF “THE CALLBACK HELL”
53
@RequestMapping("/routing-slip-non-blocking-lambda")!public DeferredResult<String> nonBlockingRoutingSlip() throws IOException {!! final DeferredResult<String> dr = new DeferredResult<>();!! // Send request #1! ListenableFuture<Response> execute = asyncHttpClient.execute(getUrl(1),! (Response r1) -> {! processResult(r1.getResponseBody()); // Process response #1! asyncHttpClient.execute(getUrl(2), // Send request #2! (Response r2) -> {! processResult(r2.getResponseBody()); // Process response #2! asyncHttpClient.execute(getUrl(3), // Send request #3! (Response r3) -> {! processResult(r3.getResponseBody()); // Process response #3! asyncHttpClient.execute(getUrl(4), // Send request #4! (Response r4) -> {! processResult(r4.getResponseBody()); // Process response #4! asyncHttpClient.execute(getUrl(5), // Send request #5! (Response r5) -> {! processResult(r5.getResponseBody()); // Process response #5! // Get the total result and set it on the deferred result! dr.setResult(getTotalResult());! ...
This is not OK!
![Page 54: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/54.jpg)
• Wrap up with a long list of nested returns…
• This is a very simple example of composite non blocking I/O services, the “callback hell” can get much much worse!!!
5. ROUTING SLIP - EXAMPLE OF “THE CALLBACK HELL”
54
dr.setResult(getTotalResult());! return r5;! });! return r4;! });! return r3;! });! return r2;! });! return r1;! });! return deferredResult;!}
![Page 55: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/55.jpg)
MAIN
55
Experiences from a real life projects
![Page 56: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/56.jpg)
EXPERIENCES FROM REAL LIFE PROJECTS – API-GW
56
API-GW
![Page 57: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/57.jpg)
EXPERIENCES FROM REAL LIFE PROJECTS – API-GW
57
API-GW
Processors
Filters
Client
Service
• API-GW
- A sequence of processors. - Every processor may be async and
potentially non blocking
![Page 58: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/58.jpg)
EXPERIENCES FROM REAL LIFE PROJECTS – API-GW
58
• API-GW
- State engine managing the processing steps
![Page 59: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/59.jpg)
EXPERIENCES FROM REAL LIFE PROJECTS - FINDINGS
59
• Logging
- Logback - MDC (Mapped Diagnostic Contexts) » The MDC manages contextual information on a per thread basis
- Log request over multiple threads? » Child threads inherit a copy of the MDC context » Manually move MDC context between threads
![Page 60: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/60.jpg)
EXPERIENCES FROM REAL LIFE PROJECTS - FINDINGS
60
• Servlet Filters
- Filter and Servlet in same thread (Servlet spec) » Non-blocking request from filter?
- Outbound filter » Executes as soon as new thread is dispatched from servlet
![Page 61: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/61.jpg)
SUMMARY
61
Summary and next step
![Page 62: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/62.jpg)
• We have seen… - Requirements of improved scalability and resilience driven by
increased number of connected devices (mobile devices and IoT) - The Reactive Manifesto to the rescue! - Non blocking I/O as a foundation - Dramatic differences can be demonstrated between
blocking and non blocking solutions - Servlet 3.0 is the key to portable solutions - Spring MVC provides a really simple programming model - …however, the callback hell is waiting for you…
• Next time we will explain what we can do to eliminate the callback hell using reactive frameworks…
SUMMARY
62
![Page 63: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/63.jpg)
PREVIEW – ”THE WAY OUT OF CALLBACK HELL…”
63
final DeferredResult<String> deferredResult = new DeferredResult<>();!!Subscription subscription = Observable.<List<String>>just(new ArrayList<>())! .flatMap(result -> doAsyncCall(result, 1, this::processResult))! .flatMap(result -> doAsyncCall(result, 2, this::processResult))! .flatMap(result -> doAsyncCall(result, 3, this::processResult))! .flatMap(result -> doAsyncCall(result, 4, this::processResult))! .flatMap(result -> doAsyncCall(result, 5, this::processResult))! .subscribe(v -> deferredResult.setResult(getTotalResult(v)));
final DeferredResult<String> dr = new DeferredResult<>();!!ListenableFuture<Response> execute = asyncHttpClient.execute(getUrl(1),! (Response r1) -> {! processResult(r1.getResponseBody()); ! asyncHttpClient.execute(getUrl(2), ! (Response r2) -> {! processResult(r2.getResponseBody());! asyncHttpClient.execute(getUrl(3), ! ...! (Response r5) -> {! processResult(r5.getResponseBody());! dr.setResult(getTotalResult());
![Page 64: DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS · •Elastic (scalable) - ”!e system stays responsive under varying workload.Reactive Systems can react to changes in the input](https://reader030.vdocuments.site/reader030/viewer/2022041017/5ec973f19259b21db32def27/html5/thumbnails/64.jpg)
Q&A
64
?