@alexsotob
Reactive Programming
for Real Use Cases
Alex Soto
Director of Developer Experience Red Hat
(@alexsotob)
@alexsotob
2
Buenas tardes, buenas noches,
señoritas y señores
To be here with you tonight. 
Brings me joy, que alegria
— Miguel
“
@alexsotob
3
Who Am I?
Alex Soto
@alexsotob
System extensions
Programming
Manifesto
Streams
Asynchrony
Resilience
Elasticity
Back-Pressure
Spreadsheets
Actor
Events
Messages
Data Flows
RX Java
Responsiveness
Fibers Non-
Blocking
Reactor
Spring
MicroProfile
REACTIVE ALL THE THING
R-Socket
@alexsotob5
What's this?
The streets are lined with
Little creatures laughing.
— Jack Skellington
“
@alexsotob
Click events on steroids
@alexsotob
C CC C C C
click stream
C CC C CC
double click stream
buffer(100ms)
C C CC
double clicks stream
filter >=2
subscribe executes everything
@alexsotob
Event-Driven => Concurrent Applications
Build concurrent
applications
Reactive
programming
Reactive
streams
Non-Blocking
Asynchronous
Non-Blocking IO (Netty)
Reactive Framework
Application Code
Callbacks, RX Java,
Reactor, Co-routines...
} 1 Thread -> n concurrent
tasks
@alexsotob9
Proceed with
caution!!!.
— Fear
“
@alexsotob
From Event-Driven to Systems
@alexsotob
Reactive => Responsive
RESPONSIVE
ASYNC MESSAGE
PASSING
ELASTICITY RESILIENCE
@alexsotob
Async != multi-thread
Async == never-block
@alexsotob
Asynchronous & Non-Blocking execution model
Ideal
world
Task A Task
B
Task
C
Real
world
Blocking I/O
Asynchronous
execution
@alexsotob
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
@alexsotob
2020
@alexsotob16
On an island that I own
Tan and rested and alone
Surrounded by 
enormous piles of money
— Eugene
“
@alexsotob17
I just don’t see how a world
that makes such a
wonderful things
could be bad?
— Ariel
“
@alexsotob18
Supersonic.
Subatomic.
REACTIVE.
@alexsotob
Quarkus = (Imperative + Reactive)/Reactive
Eclipse Vert.x / Netty
Undertow
(Servlet)
RESTEasy
(JAX-RS) Reactive
Routes
Reactive
Messaging
Reactive
Data Access
EventsMessages
HTTP
@alexsotob
Pagination + Filtering
Beer
Consumer
Beer
Service
getPage(1)
getPage(n)
@alexsotob
Pagination
Flowable<List<Beer>> beerStream = Flowable.generate(() -> 1, (page, emitter) -> {
final List<Beer> beers = beerGateway.getBeers(page);
if (beers.isEmpty()) {
emitter.onComplete();
} else {
emitter.onNext(beers);
}
return page + 1;
});
return beerStream
.flatMap(Flowable::fromIterable);
Initial Page
Get Beers
Emit Beers of Finish
Iterate to the next page
Individual Beer Is Emitted
@alexsotob
Pagination
@GET
@Path("/all")
@Produces(MediaType.APPLICATION_JSON)
public Publisher<Beer> getBeers() {
return beerservice.beers()
.filter(b -> b.getAbv() > 10.0);
}
~Thread
Get Beers Streams
Stream Operation
@alexsotob
Parallel Requests
Beer
Consumer
Beer
Service
getBeer(XXX)
getBeer(YYY)
@alexsotob
Parallel Requests
public Flowable<JsonValue> compareBeers(String beerA, String beerB) {
final Flowable<JsonObject> beerAInfo = beerGateway.getBeer(beerA)
.map(array -> array.get(0))
.map(this::filterContent);
final Flowable<JsonObject> beerBInfo = beerGateway.getBeer(beerB)
.map(array -> array.get(0))
.map(this::filterContent);
return beerAInfo.zipWith(beerBInfo, this::compare);
}
Get Beer Async
Get Important Fields
When Both Operations Finishes
Execute Comparison
@alexsotob25
A hundred thousand things to see
(Hold your breath, it gets better)
— Jasmine & Aladdin
“
@alexsotob26
Don’t you know
there’s part of me that
longs to go…
Into the Unknown
— Elsa
“
@alexsotob
HTTP => Strong Coupling
@alexsotob
HTTP => Uptime Coupling
@alexsotob
HTTP => Strong Coupling
C
B
C
B
C
B
C
B
C
B
C
B
@alexsotob
Messaging
Non-Blocking
Message
Passing
Elasticity
Resilience
@alexsotob
Data at Rest => Data At Motion
Message
Stream
Publish/
Subscribe
Request-Reply
@alexsotob
Quarkus = {HTTP, Messaging, Streaming}
Messaging / Streaming
Quarkus
Application Code
Container Host / Operating System
NIO HTTP Messaging
Quarkus
Application Code
Container Host / Operating System Messaging
Middleware
@alexsotob
Quarkus = {HTTP, Messaging, Streaming}
MicroProfile Reactive Messaging
Quarkus
Application Code
Container Host / Operating System
NIO HTTP Messaging
Quarkus
Application Code
Container Host / Operating System Messaging
Middleware
*
* Kafka or any other messaging system.
@alexsotob
Music Charts
Player Top Music Chart Player Top Music Chart
HTTP
Dashboard
Song Played
*
play music musicplay
@alexsotob
REST to Messaging
@Inject
@Channel("orders")
Emitter<String> orders;
public Order enqueue(Order order) {
orders.send(jsonb.toJson(order));
return order;
}
Send “data” to the
channel “orders”
@alexsotob
Processing Messages
@Incoming("orders")
@Outgoing("queue")
public CompletionStage<String> prepare(String message) {
Order order = fromJson(message, Order.class);
return makeIt(order)
.thenApply(beverage ->
PreparationState.ready(order, beverage)
);
}
Read from “orders”
Write to “queue”
Process data async*
@alexsotob37
A hundred thousand things to see
(Hold your breath, it gets better)
— Jasmine & Aladdin
“
@alexsotob38
Put on your Sunday clothes
there's lots of world
out there.
— Wall-E
“
[https://github.com/lordofthejars/rp4rp]
@alexsotob39
What's the lesson?
What is the take-away?
— Maui
“
@alexsotob40
Quarkus = {HTTP, Messaging, Streaming}
HTTP, Messaging, Streaming
Quarkus
Non-Blocking IO
@alexsotob41
WHAT ABOUT REACTIVE STREAMS?
@Channel(“foo”) Emitter
@Incoming(“foo”)
@Outgoing(“bar”)
@Channel(“bar”) Publisher
@Outoing(“foo”)
@Incoming(“bar”)
Publisher Processor Subscriber
Connector Connector
@alexsotob
Hay un amigo en mí,
cuando salgan a volar,
hay un amigo en mí
— Toy Story
“
@alexsotob
asotobue@redhat.com
http://www.lordofthejars.com/
lordofthejars

Reactive Programming for Real Use Cases