Bringing Reactive to Enterprise Developers
Clement Escoffier, Red Hat, @clementplop
What is Reactive?
Do we need this new thing?
reactive | rēˈaktiv |
adjective
showing a response to a stimulus: pupils are reactive to light.
• acting in response to a situation rather than creating or controlling it: a
proactive rather than a reactive approach.
• having a tendency to react chemically: nitrogen dioxide is a highly reactive
gas.
• Physiology showing an immune response to a specific antigen.
...
https://www.oxfordlearnersdictionaries.com/definition/english/reactive
reactive | rēˈaktiv |
adjective
showing a response to a stimulus: pupils are reactive to light.
• acting in response to a situation rather than creating or controlling it: a
proactive rather than a reactive approach.
• having a tendency to react chemically: nitrogen dioxide is a highly reactive
gas.
• Physiology showing an immune response to a specific antigen.
...
• Software application reacting to stimuli such as user inputs, messages,
and failures
What is Reactive?
Do we need this new thing?
Events
Signals
Failures
Is Reactive == Event-Driven ?
Events
Signals
Failures
Is Reactive == Event-Driven ?
EJBs
Message-Driven Beans
CDI Observers
Event-Driven with a different concurrency model
Non-Blocking IO (Netty)
Reactive Framework
Application Code
Callbacks, RX,
Co-routines...
} 1 Thread
Reactive
programming
Non-Blocking IO
Build concurrent
applications
FROM EVENT-DRIVEN TO SYSTEMS
“A distributed system is one in which
the failure of a computer you didn't
even know existed can render your
own computer unusable.”
Leslie Lamport
Build responsive
distributed systems
Reactive
Systems
Elasticity,
Resilience,
Messages
REACTIVE == BUILD BETTER SYSTEMS
Build concurrent
applications
Reactive
programming
Non-Blocking IO
MESSAGES
What’s wrong with HTTP?
HTTP => STRONG COUPLING
HTTP => UPTIME COUPLING
CB
CB
CB
CB
CB
CB
THE RISE OF CIRCUIT BREAKERS
Non-Blocking
Message Passing
Elasticity
Resilience
THE BENEFITS OF MESSAGING
Virtual
Addresses
APPLYING REACTIVE TO DISTRIBUTED SYSTEMS
RESPONSIVE
ASYNC MESSAGE
PASSING
ELASTICITY RESILIENCE
APPLYING REACTIVE TO DISTRIBUTED SYSTEMS
RESPONSIVE
ASYNC MESSAGE
PASSING
ELASTICITY RESILIENCE
Asynchronous
Non-Blocking
Message Passing
Reactive != multi-thread
Reactive == never-block
Containers are about sharing and deployment density
Container Host
Threads
costs memory
cost lots of memory
cost CPU cycles
+ quotas = BOOM
The hidden truth of containers
Asynchronous & Non-Blocking execution model
Ideal
world
Task A Task B Task C
Real
world
Blocking I/O
Asynchronous
execution
Let’s look at
pragmatic
questions
What about
CompletionStage ?
What about
CompletionStage ?
Uni
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/greeting/{name}")
public Uni<String> greeting(@PathParam String name) {
return service.greeting(name);
}
Using Unis in JAX-RS resources / Rest Client
@GET
@Path("/name/{name}")
@Produces("application/json")
Uni<Country> getByName(@PathParam String name);
What about streams?
CompletionStage vs. Publisher
Publisher (Flow)
0..n items
Completion Stage
Eager
0..1 items
Lazy
Back-Pressure
Has operators No Operators
Mutiny: Uni and Multi
Multi
0..n items
Uni
Lazy (subscription)
1 item
Lazy (subscription + request)
Back-Pressure
Operators Operators
Back-Pressure 101
https://twitter.com/impurepics/status/1000466142866149377
JAX-RS returning Multi
@Produces(MediaType.APPLICATION_JSON)
@Path("/greeting/{count}/{name}")
public Multi<String> greetings(@PathParam int count, @PathParam String name) {
return service.greetings(count, name);
}
@Produces(MediaType.SERVER_SENT_EVENTS)
@SseElementType(MediaType.TEXT_PLAIN)
@Path("/stream/{count}/{name}")
public Multi<String> greetings(@PathParam String name) {
return service.infiniteGreetings(name);
}
I heard a lot about Kafka.
Does Kafka make me reactive?
Messaging / Streaming
Quarkus
Application Code
Container Host / Operating System
HTTP Messaging
Quarkus
Application Code
Container Host / Operating System Messaging
Middleware
HTTP, MESSAGING, STREAMING
Coffee Shop Barista Coffee Shop Barista
HTTP
Dashboard
REACTIVE COFFEE SHOP
https://github.com/cescoffier/quarkus-coffeeshop-demo
Orders, Queue
* Also applies to AMQP and other messaging protocols but we want to be “cool”
*
Bridge imperative -> reactive
DROP HTTP, SEND MESSAGES
@Inject @Channel("orders")
Emitter<Order> orders;
@Inject @Channel("queue")
Emitter<Beverage> states;
@POST @Path("/messaging")
public Order messaging(Order order) {
order.setOrderId(getId());
states.send(Beverage.queued(order));
orders.send(order);
return order;
}
Send to the
channel “orders”
@Incoming("orders")
@Outgoing("queue")
public Uni<Beverage> process(Order order) {
return Uni.createFrom(() -> {
Beverage coffee = prepare(order);
return coffee;
});
}
Process data
asynchronously*
* Many other options available to produce, process or consume data
Read from “orders”
Write to “queue”
PROCESSING MESSAGES
37
@Inject
@Channel("beverages")
Multi<String> queue;
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public Multi<String> getQueue() {
return queue;
}
Inject the data from the
channel “beverages”
Returns a stream
PUSHING DATA TO SSE
Is it a Reactive System?
Coffee Shop
Barista
Dashboard
Queue
Orders
BARISTAS TAKE BREAKS
Coffee Shop
Baristas
Queue
Orders
RUSH HOUR!
Dashboard
APPLYING REACTIVE TO DISTRIBUTED SYSTEMS
RESPONSIVE
ASYNC MESSAGE
PASSING
ELASTICITY RESILIENCE
Messaging / Streaming
Quarkus
Application Code
Container Host / Operating System
Reactive
HTTP Server
Messaging
Quarkus
Application Code
Container Host / Operating System Messaging
Middleware
BUT WHAT ABOUT REACTIVE STREAMS?
Reactive
Streams
CONCLUSION
Developer Joy Supersonic Subatomic Java
Unifies
imperative and reactive
Best of breed
libraries and standards
QUARKUS
QUARKUS = (REACTIVE + IMPERATIVE) / REACTIVE
Eclipse Vert.x / Netty
Undertow
(Servlet)
RESTEasy
(JAX-RS)
Reactive
Routes
Reactive
Messaging
Reactive
Data Access
Events
HTTP
Messages
46
https://quarkus.io
@quarkusio
https://quarkusio.zulipchat.com

JakartaOne Livestream CN4J: Bringing Reactive to Enterprise Developers