You are on page 1of 41

Reactive Microservices In Action

Emily Jiang
Java Champion
STSM, IBM

Liberty Microservice Architect, Advocate


MicroProfile Senior Lead @IBM
@emilyfhjiang
Why Reactive?

2
Synchronous calls

3
Two microservices...

A B
Two microservices...

data/client/test/hello data/service/hello

A B
“hello” “hello”
… Interacting using synchronous calls

data/client/test/hello data/service/hello

“hello” “hello”
Asynchronous calls

7
Asynchronous calls (because it’s trendy)
Microservices are like kids...

(b)

(a)
(c)

Need mudguards
MicroProfile Fault-Tolerance

(b)

(a)
(c)
Sorry, I’m out of context...

I’ve lost my thread locals!


(User, Transaction, Correlation Id…)
MicroProfile Context Propagation

Capture the
context

Restore the
context
MicroProfile Context Propagation
Most asynchronous things
AREN’T non-blocking.
You need
asynchrony + non-blocking
to use resources
efficiently
… Is this what you want?

Context
Propagation

Asynchronous, Non-Blocking IO
Fault-Tolerance
… And they lived together happily ever after.

???
!!!
Reactive

18
What Reactive?

19
Reactive all the things ??? Observable Spring

Messages Resilience Reactor


Responsiveness RX Java
Mutiny

System eXtensions
Manifesto Elasticity

Asynchrony Non-Blocking MicroProfile


Actor

Fibers
Streams Back-Pressure
Programming
Events Data Flows RSocket
Spreadsheets
Reactive all the things ??? Observable Spring Quarkus

Responsiveness Messages Resilience RX Java Reactor Mutiny

System eXtensions
Manifesto Elasticity

Asynchrony Non-Blocking MicroProfile


Actor

Fibers
Streams Back-Pressure
Programming
Events Data Flows RSocket
Spreadsheets
21
Reactive Programming

Data Propagation of
Streams changes

22
Reactive Extensions

t Streams
Implemen
Type and
Reactive
Prog. Operators

Data Propagation
Streams of changes

23
Reactive Streams

RX Java,
t Streams
Implemen Reactor,
Type and
Reactive Mutiny,
Prog. Operators
Reactive
Streams Ops

Back
Data Propagation
Pressure
Streams of changes
Protocol

24
Reactive Streams - Java 9+ edition

Observable Stream of <T>

Stream Observer
On new item
On failure
On completion

25
Reactive Streams - Java 9+ edition

The contract between the


observer and the stream
Request items
Stop the observation 26
Reactive Streams - How does that work?

(1)subscribe

Subscriber Publisher

(2) onSubscribe(sub)

27
Reactive Streams - How does that work?

(3) request(x)

Subscriber Publisher

(4) onNext(i1)
(5) onNext(i2)
(6) onComplete()

28
Reactive Streams - How does that work?

Subscriber Processor Publisher

subscribe subscribe
request(x) request(x)

29
Reactive Systems
http://www.reactivemanifesto.org/

Message
Asynchronous
Passing
30
How Reactive?

31
Applying reactive to microservices

Temporal
decoupling
Back-Pressure
Protocol
Using messages to achieve decoupling

Sent to virtual addresses


Can be durable (temporal decoupling)
Use non-blocking IO

A B
MicroProfile Reactive Messaging

Application Code Application Code

MicroProfile MicroProfile

Messaging / Streaming
Async HTTP Messaging

Container Host / Operating System


Container Host / Operating System

MicroProfile
Reactive Messaging
MicroProfile Reactive Messaging

35
MicroProfile Reactive Messaging

Connector
configuration
(protocol specific)
36
MicroProfile Reactive Messaging

Reactive
Streams

37
Reactive architecture

Decoupling, Elasticity, Resilience,


Back-Pressure, Asynchrony, Non-Blocking

A B
Demo
https://github.com/Emily-Jiang/reactive-service-a
https://github.com/Emily-Jiang/reactive-service-b

39
References
• Defining the term “reactive”
https://developer.ibm.com/articles/defining-the-term-reactive/
• Develop reactive microservices with Reactive Messaging
https://developer.ibm.com/articles/develop-reactive-microservices-with-
microprofile/
• Open Liberty guides on Reactive
https://openliberty.io/guides/?search=reactive

• MicroProfile Reactive Messaging


https://github.com/eclipse/microprofile-reactive-messaging
• MicroProfile Fault Tolerance
https://github.com/eclipse/microprofile-fault-tolerance/
• MicroProfile Context Propagation
https://github.com/eclipse/microprofile-context-propagation
Thank you

Emily Jiang
STSM, Liberty Microservice Architect and Advocate
IBM
@emilyfhjiang

Dr. Clement Escoffier


Senior Principal Software Engineer
Red Hat
@clementplop

41

You might also like