Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

High Performance Microservices with Ratpack and Spring Boot

1,899 views

Published on

SpringOne Platform 2016
Speaker: Dan Woods

Ratpack and Spring Boot are a match made in microservice heaven. Each brings its respective strengths to the task of building a compelling service for the cloud. Spring Boot's incorporation of the Spring and Spring Cloud ecosystems make it an excellent choice for building service components; Ratpack's nature as a reactive web framework and its emphasis on performance and efficiency make it a premiere choice for a durable web tier. When the two frameworks join forces, developers become unstoppable. This talk will showcase Ratpack and Spring Boot's complementary integrations, and demonstrate, through live coding, building a high performance microservice.

Published in: Technology
  • Be the first to comment

High Performance Microservices with Ratpack and Spring Boot

  1. 1. RATPACK WEB FRAMEWORK DAN WOODS @DANVELOPER
  2. 2. SENIOR SOFTWARE ENGINEER WORKING ON CLOUD & DEVOPS TOOLING
  3. 3. O'REILLY AUTHOR, 2016 LEARNING RATPACK
  4. 4. SUPPORT YOUR COMMUNITY.ALL ROYALTIES FOR LEARNING RATPACK GO DIRECTLY TO GR8LADIES HTTP://GR8LADIES.ORG
  5. 5. ROUGH AGENDA > Brief overview > Execution model > Registries > Registries + Spring > Microservice example > Performance numbers
  6. 6. OVERVIEW
  7. 7. Ratpack is a high throughput, non-blocking, asynchronous, reactive web framework for the JVM
  8. 8. Built on Java 8, has first class support Groovy
  9. 9. Realistically, works great with every JVM language
  10. 10. @Grab('io.ratpack:ratpack-groovy:1.4.0-rc-2') import static ratpack.groovy.Groovy.ratpack ratpack { handlers { get { render "Hello World!" } } }
  11. 11. package app; import ratpack.server.RatpackServer public class Main { public static void main(String args[]) throws Exception { RatpackServer.start(spec -> spec .handlers(chain -> chain .get(ctx -> ctx.render("Hello World!")) ) ); } }
  12. 12. No opinions about how you build your application
  13. 13. Make common things as easy as possible
  14. 14. Explicit, no convention-over-configuration
  15. 15. First class concerns: performance, developer experience, packaging
  16. 16. EXECUTION MODEL
  17. 17. Asynchronous programming is hard!
  18. 18. Traditionally there is a temporal disconnect between calling an async function and getting its response. public void asyncDbCall(Long productId, Consumer callback) { Product product = ... async db call + future + wait for future callback.apply(product); }
  19. 19. Bad things happen! final List products = new ArrayList(); Long productId = request.pathTokens.productId; db.asyncDbCall(productId, product -> products.add(product)); Long nextProductId = productId+1; db.asyncDbCall(nextProductId, product -> products.add(product)); response.send(products);
  20. 20. There is no guarantee as to what order async calls will be executed! final List products = new ArrayList(); String productId = request.pathTokens.productId; db.asyncDbCall(productId, product -> products.add(product)); // call may return first, maybe not! String nextProductId = productId+1; db.asyncDbCall(nextProductId, product -> products.add(product)); // call may return second, may not! response.send(products); // what even is the value of `products` here!
  21. 21. Luckily, Ratpack's execution model provides deterministic processing of asynchronous calls
  22. 22. An Execution in Ratpack is the equivalent construct to a continuation
  23. 23. Ratpack Promise type denotes a frame in the continuation (a.k.a "execution segments")
  24. 24. Execution segments are guaranteed to execute in their given order.
  25. 25. RATPACK MAINTAINS A SMALL THREAD POOL FOR ALL ITS PROCESSING (TYPICALLY 2 * NUM CPU CORES)
  26. 26. While an execution segment is processing, its thread is able to handle other processing and requests
  27. 27. Ratpack ensures the resources given to your application are being efficiently utilized
  28. 28. Ratpack's RxJava integration provides a scheduler that fits Observable into the execution model
  29. 29. Provides an implementation of Reactive Streams, which also fits into the execution model
  30. 30. REGISTRIES
  31. 31. Abstraction over dependency injection
  32. 32. Provides a basic component binding infrastructure
  33. 33. REGISTRIES ARE EVERYWHERE IN RATPACK
  34. 34. CAN BE BACKED BY ANY COMPONENT- PROVIDING SYSTEM SPRING BOOT AND GUICE NATIVELY SUPPORTED
  35. 35. Can quickly prototype without any DI system public static void main(String[] args) throws Exception { RatpackServer.start(spec -> spec .registryOf(r -> r.add(new MyAsyncDbService())) // bind my service in the registry .handlers(chain -> chain .get(ctx -> { MyAsyncDbService db = ctx.get(MyAsyncDbService.class); // retrieve it later // ... }) ) ); }
  36. 36. Integrate with Spring Boot when you're ready! @SpringBootApplication // Spring Boot!! public class Main { @Bean public MyAsyncDbService myAsyncDbService() { // bind it as a bean! return new MyAsyncDbService(); } public static void main(String[] args) { // turn Spring Boot into a Ratpack registry! Registry registry = ratpack.spring.Spring.spring(Main.class); RatpackServer.start(spec -> spec .registry(registry) .handlers(chain -> chain .get(ctx -> { MyAsyncDbService db = ctx.get(MyAsyncDbService.class); // retrieve your service! // ... }) ) ) } }
  37. 37. Architect your application using all the helpful aspects of Spring Boot, while using Ratpack under the hood for high throughput, reactive, async, non-blocking processing
  38. 38. MICROSERVICE EXAMPLE CODE: HTTPS://GITHUB.COM/DANVELOPER/S1P-HIGH-PERF-MICROSERVICES
  39. 39. PERFORMANCE NUMBERS
  40. 40. A quick tirade about performance testing...
  41. 41. Generalizing performance numbers for all use cases is impossible
  42. 42. The numbers provided should be used as a guide, not the word of law
  43. 43. "YOU SHOULDN'T TEST ON THE CLOUD!!"
  44. 44. Unrealistic. Modern performance testing needs to be about how much burst traffic can be sustained, without your service falling over, before you can horizontally scale it.
  45. 45. PERFORMANCE NUMBERS HTTPS://GITHUB.COM/DANVELOPER/S1P-HIGH-PERF-MICROSERVICES/BLOB/MASTER/README.MD
  46. 46. FUTURE POSSIBILITIES
  47. 47. Spring 5 will be all reactive; will be possible to integrate Spring controllers into Ratpack
  48. 48. Provide a scheduler to Project Reactor so that Flex and Mono types can utilize the execution model
  49. 49. Questions?

×