Reactive All the Way Down
with Ratpack, RxGroovy, React, and
RabbitMQ
Steve Pember
CTO, ThirdChannel
Greach, 2018
@svpember
Reactive All the Way Down
with Ratpack, RxGroovy, Groovy,
rxJava, React, and RabbitMQ
Steve Pember
CTO, ThirdChannel
Greach, 2018
@svpember
@svpember
Agenda
• Reactive ?
@svpember
Agenda
• Reactive ?
• Anatomy of a Reactive Service
@svpember
Agenda
• Reactive ?
• Anatomy of a Reactive Service
• Technology Choices for a Reactive Service
@svpember
Agenda
• Reactive ?
• Anatomy of a Reactive Service
• Technology Choices for a Reactive Service
• Demo
Advantages of Reactive
A Good Kind of Manifesto
–Johnny Appleseed
“Type a quote here.”
@svpember
Reactive Services…
• Are Fast and Efficient
@svpember
Reactive Services…
• Are Fast and Efficient
• Resist and are Resilient to Failure
@svpember
Reactive Services…
• Are Fast and Efficient
• Resist and are Resilient to Failure
• Scale Horizontally to Meet Current Demand
@svpember
Reactive Services…
• Are Fast and Efficient
• Resist and are Resilient to Failure
• Scale Horizontally to Meet Current Demand
• Are Driven by Asynchronous Messages / Events
@svpember
@svpember
@svpember
Advantages of Reactive
• Highly optimized usage of resources -> optimal throughput
You’re not
Netflix.
Probably
@svpember
Advantages of Reactive
• Highly optimized usage of resources -> optimal throughput
• Fast
@svpember
Advantages of Reactive
• Highly optimized usage of resources -> optimal throughput
• Fast
• Reduces Synchronous Communications
@svpember
Advantages of Reactive
• Highly optimized usage of resources -> optimal throughput
• Fast
• Reduces Synchronous Communications
• Increased Error Protection
@svpember
@svpember
@svpember
Advantages of Reactive
• Highly optimized usage of resources -> optimal throughput
• Fast
• Reduces Synchronous Communications
• Increased Error Protection
• “Simpler” testing
@svpember
@svpember
Advantages of Reactive
• Highly optimized usage of resources -> optimal throughput
• Fast
• Reduces Synchronous Communications
• Increased Error Protection
• “Simpler” testing
• Promotes a functional programming style
@svpember
Advantages of Reactive
• Highly optimized usage of resources -> optimal throughput
• Fast
• Reduces Synchronous Communications
• Increased Error Protection
• “Simpler” testing
• Promotes a functional programming style
• Ease of Development
Anatomy of a Reactive Service
@svpember
@svpember
Layers
• Front-End
Don’t Write Imperative JS
@svpember
Layers
• Front-End
• HTTP
@svpember
HTTP
• C10k problem
• Nonblocking I/O
• Many popular web servers support async or reactive handlers
• Ratpack’s been doing it for years now
@svpember
Layers
• Front-End
• HTTP
• Service Layer
@svpember
Service Layer
• Write Async Code!
@svpember
Service Layer
• Write Async Code!
• java.util.concurrent
@svpember
Service Layer
• Write Async Code!
• java.util.concurrent
• GPars!
@svpember
Service Layer
• Write Async Code!
• java.util.concurrent
• GPars!
• Project Reactor
@svpember
Service Layer
• Write Async Code!
• java.util.concurrent
• GPars!
• Project Reactor
• Reactive Streams
@svpember
Service Layer
• Write Async Code!
• java.util.concurrent
• GPars!
• Project Reactor
• Reactive Streams
• Create an internal Message Bus
@svpember
Layers
• Front-End
• HTTP
• Service Layer
• Messaging / Intra-Service comms
@svpember
Messaging
• Non Blocking I/O is vital!
• Need an Asynchronous Message Broker
• Need a dedicated Thread Pool (ExecutorService)
• Message Flow:
@svpember
@svpember
@svpember
Layers
• Front-End
• HTTP
• Service Layer
• Messaging / Intra-Service comms
• Persistance
@svpember
Reactive JDBC Driver is Coming!
Tech Choices at Each Level
We prefer Libraries
Front-End
@svpember
React
• View Library for JS
• Define Components that receive props and maintain state
React favors pushing
properties ‘down’ through
layers of components
@svpember
React
• View Library for JS
• Define Components that receive props and maintain state
• + Encourages reusable components
@svpember
Stateful Components (“Containers”)
• Maintains internal state of Component
• Written as a class, with a variety of internal functions (e.g. click Handlers)
• Not intended for re-use
• May receive external State as props
• Passes Props to Child Components
• Simple Render functions that may concern layout
• Children can be both Containers and Presentational
• Refrain from ‘raw’ HTML (JSX), CSS, render() should use Presentational Components
• Little-To-No Business Logic
@svpember
Stateless Components (“Presentational”)
• No State. Only receives Props.
• Written as a single function (representing the render())
• Ideal if Presentational components are reusable
• Potentially complex render functions. note: calling render() only affects
DOM if props have changed for a component
• No Business Logic
@svpember
React
• View Library for JS
• Define Components that receive props and maintain state
• + Encourages reusable components
• + Shadow / Virtual DOM
@svpember
React
• View Library for JS
• Define Components that receive props and maintain state
• + Encourages reusable components
• + Shadow / Virtual DOM
• + Reacts to property changes
@svpember
React
• View Library for JS
• Define Components that receive props and maintain state
• + Encourages reusable components
• + Shadow / Virtual DOM
• + Reacts to property changes
• + Has a massive ecosystem
React-Friendly Libraries for
additional functionality
@svpember
Redux
• Library for handling global state, derived from Flux
• One Immutable Store, but can allow for hierarchy (i.e. not flat shape)
• Strict, Unidirectional Data Flow
• Reducers, Actions, ActionCreators
• Intended for Synchronous change operations
• Hooks for Middleware / plugins
@svpember
React
• View Library for JS
• Define Components that receive props and maintain state
• + Encourages reusable components
• + Shadow / Virtual DOM
• + Reacts to property changes
• + Has a massive ecosystem
• - APIs change frequently
@svpember
React
• View Library for JS
• Define Components that receive props and maintain state
• + Encourages reusable components
• + Shadow / Virtual DOM
• + Reacts to property changes
• + Has a massive ecosystem
• - APIs change frequently
• - Forces you to think in React
@svpember
React: Use If…
• You need a powerful View layer for your frontend
• You appreciate the usefulness of composed, reusable View components
• You have the mental agility to follow and keep up with the latest Javascript
trends
HTTP
–Johnny Appleseed
“Type a quote here.”
@svpember
Ratpack
• NIO HTTP Library
• + Built on top of Netty
• + Fast, lightweight
• + Non-opinionated
• + Excellent Testing support
• + Excellent Gradle support
• - Taking a while to adopt rxJava2
• - It’s I/O thread pool is Unbounded
• - Non-obvious how to access registry / DI during a test
@svpember
When to use Ratpack
• You need NIO http support, but don’t want a full framework
• You need to handle high levels of concurrent users
• You’re comfortable going to a conference with many folks from the Grails
team and talking about an alternative solution
Service Layer
@svpember
RxJava
• Reactive Streams implementation on the JVM, designed for communication across
boundaries
• + Makes Async / parallel programming ‘easy’
• + Promotes a highly functional, stream-oriented style
• + Promotes stream-oriented programming
• + Backpressure
• + Composable & Cancelable
• + Ideal for inputs of potentially infinite amounts of data
• - Learning curve is steep / new programming paradigm
• - can swallow exceptions if not careful
@svpember
@svpember
@svpember
When to use RxJava
• You need to write Async code
• You have 1000s+ or potentially infinite events to process
• You want to feel stupid
• You comfortable going to a Groovy conference and telling people you don’t
use GPars
Messaging
@svpember
RabbitMQ
• High Throughput Message Broker
• + Complex and Powerful routing mechanisms
• + High Availability, clustering, back pressure
• + Durability / failure tolerance
• + Easy to deploy / cluster
• - not as fast as other message brokers
• - Written in Erlang
@svpember
When to use RabbitMQ:
• You need Async message handling
• You like having simple consumers and smart routing
• You don’t mind being looked down upon by Kafka fans
Persistance
Come on, Async JDBC!
Overview and Demo
@svpember
@svpember
Images
• I’m sorry (Rain): https://imgur.com/gallery/J27yj3k
• Spartans: www.300themovie.com/
• Colbert: https://giphy.com/gifs/stephen-colbert-report-
hopeful-4KxeicCUTvhrW
• The Flash: https://www.bleedingcool.com/2016/12/27/cws-flash-today-
tomorrow-beyond/
• React Virtual Dom: https://www.youtube.com/watch?v=UH6dz6IGbK4
@svpember
Links
• Repo for the demo: https://github.com/spember/reactive-ratpack-demo

Reactive All the Way Down the Stack