Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016
-
Upload
alvaro-sanchez-mariscal -
Category
Software
-
view
645 -
download
1
Transcript of Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016
![Page 1: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/1.jpg)
Efficient HTTP applications on the JVM with Ratpack
Álvaro Sánchez-MariscalSoftware Engineer, Grails committer
Object Computing, Inc
Platinum Sponsor
#VoxxedBerlin @alvaro_sanchez
![Page 2: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/2.jpg)
Álvaro Sánchez-Mariscal Software Engineer Grails Development Team @alvaro_sanchez [email protected]
![Page 3: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/3.jpg)
![Page 5: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/5.jpg)
Agenda• Introduction.
• The handlers chain.
• The context.
• Ratpack modules.
• Asynchronous & Non Blocking model.
• Q&A.
![Page 6: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/6.jpg)
Introduction
![Page 7: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/7.jpg)
Slides, code & workshop• Slides:
• http://bit.ly/ratpack-101-slides
• Companion code:
• http://bit.ly/ratpack-101-code
• Workshop:
• http://bit.ly/ratpack-101-workshop
![Page 8: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/8.jpg)
Before we start
• Raise your hand if you have used:
• Java.
• Groovy.
• Spock.
• Ratpack?
![Page 9: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/9.jpg)
Introduction• Ratpack is:
• A set of Java 8 libraries…
• … lightweight, un-opinionated, Netty-based...
• … for writing non-blocking HTTP applications…
• … focused on performance and efficiency…
• … with an emphasis on testability..
• … embracing Java 8 or Groovy as programming languages.
![Page 10: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/10.jpg)
When to use Ratpack?
• For microservices.
• For high performance applications.
• For lightweight services (no container, batteries included).
![Page 11: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/11.jpg)
If you are writing microservices, why
do you use macroframeworks?
![Page 12: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/12.jpg)
Ratpack is developer friendly
• Starts up in milliseconds.
• Development-time reloading (in ms) with Gradle.
• First-class support for functional testing.
![Page 13: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/13.jpg)
Demo
1. Groovy script.
2. Gradle build with Lazybones.
![Page 14: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/14.jpg)
The handlers chain
![Page 15: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/15.jpg)
Handlers• An incoming request is passed through the
handler chain.
• A handler can:
• Respond to the request.
• Delegate to the next handler in the chain.
• Insert more handlers in the chain and delegate to them.
![Page 16: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/16.jpg)
Handlers• Each handler is given a Context instance.
• To interact with the request / response objects.
• To access the registry.
• Etc.
• Note that Ratpack is not based on the Servlet API.
![Page 17: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/17.jpg)
Demo
3. Handlers.
![Page 18: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/18.jpg)
The context
![Page 19: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/19.jpg)
The Context• The Context provides:
• Access to the HTTP Request and Response objects.
• Interaction with other handlers through next()
and insert() methods.
• Access to objects in the registry.
![Page 20: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/20.jpg)
The registry
• The context is also a Registry of objects.
• Handlers can store objects into the registry for use by downstream handlers.
![Page 21: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/21.jpg)
Demo
4. Working with the context.
![Page 22: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/22.jpg)
Ratpack modules
![Page 23: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/23.jpg)
Ratpack modules
• Ratpack can integrate with Google Guice for:
• Dependency injection for services, etc.
• Officially supported modules.
• Reusing your own components as your modules.
![Page 24: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/24.jpg)
Official modules• Benchmarks (JMH).
• Metrics (Dropwizard).
• H2 Database.
• Handlebars.
• HikariCP JDBC pool.
• Hystrix.
• New Relic.
• Pac4J.
• RxJava.
• HTTP sessions.
• Spring Boot.
• Thymeleaf.
![Page 25: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/25.jpg)
Demo
5. Using Guice and Jackson in Ratpack.
![Page 26: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/26.jpg)
Asynchronous and non-blocking model
![Page 27: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/27.jpg)
The classic approach• A thread-per-request.
• There is a relatively large thread pool.
• Each request is assigned a thread from the pool.
• If the request handling code performs blocking I/O, the thread sleeps until the result is received.
• Overhead: thread management, memory consumption.
![Page 28: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/28.jpg)
Ratpack’s value proposition• HTTP IO is event-driven (non blocking).
• Powered by Netty.
• Handler chain is organised as a pipeline of asynchronous functions.
• Small compute thread pool.
• Blocking operations are executed in a separate pool.
• Integration with existing libraries like RxJava.
![Page 29: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/29.jpg)
Async model• Ratpack guarantees a deterministic flow.
• Promises are executed in the order they are defined.
• Once the promises are resolved, they are executed in compute threads.
• Async functions can be composed.
• To avoid the callback hell.
![Page 30: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/30.jpg)
Demo
6. Asynchronous model.
![Page 31: Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016](https://reader031.fdocuments.us/reader031/viewer/2022022203/587143601a28ab55588b4ee5/html5/thumbnails/31.jpg)
Álvaro Sánchez-Mariscal Software Engineer
Grails Development Team @alvaro_sanchez
Danke!