Programming Async web apps with Ratpack

By - Prakash Balodi

I Am

Software Engineer @ Quintype Inc.

Prakash Balodi

Agenda

  • Intro to Ratpack
  • Architecture
  • Going Non-blocking with Ratpack
  • Promises

Ratpack is - 

  • A Micro web framework

  • Small and lightweight 

  • Does not implement Servlet API

  • Written in Java 8 (and requires Java 8)

Ratpack is - 

  • Non blocking

  • Un-opinionated 

  • Has first class Groovy Support

@Grapes([
  @Grab('io.ratpack:ratpack-groovy:1.1.1'),
  @Grab('org.slf4j:slf4j-simple:1.7.12')
])
import ...

ratpack {
    handlers {
        get {
            render "Hello World!"
        }
        get(":name") {
            render "Hello $pathTokens.name!"
        }
    }
}

Ratpack Hello world

Architecture

  • Ratpack is designed as a set of  libraries
  • does not advertise any specific dev paradigm
  • No explicit build tools (uses Gradle)
  • Goal is not to be full stack, but to provide choices and being flexible

Non blocking with Ratpack

  • Not based on Servlet Model
  • Has different thread pool for Blocking calls
  • Async at both levels - 
    • HTTP IO is event driven / non blocking driven by Netty
    • Request handling is organised as a pipeline of asynchronous functions

Deterministic execution 

  • Traditionally async programming does not have a consistent path of execution
  • Ratpack changes that !
  • Makes thread executions deterministic
  • This allows for programs that are consistent 

Deterministic execution, HOW? 

  • Handlers invoke blocking threads after the corresponding compute threads have finished executing.
  • Then blocking threads are executed 
  • the output of these blocking threads is then executed back in a compute thread.
  • A blocking thread won't execute unless it has a callback in a compute thread.

Promises in Ratpack

  • Promise is a representation of potential value.
  • All Ratpack blocking APIs use promise internally.

Promise Transformations

 

  • Lot of times we need to specify operations on promises before they are realised
  • Process transformers help with these operations.
  • Most common examples include - map, flatmap, transform

PitFalls (things to take care of)

  • Exception handling is not straightforward.
  • No ThreadLocals. 
  • Integrating some third party libraries can be hard.

Moving Promises outside of Handlers

  • We can use promises outside of Handlers.
  • Helps in structuring code better.
  • And makes more re-usable code.

Thanks!

Ratpack

By Prakash Balodi

Ratpack

  • 3,451