Go Reactive: Event-Driven, Scalable, Resilient & Responsive Systems
Go Reactive: Event-Driven, Scalable, Resilient & Responsive Systems
Event-Driven,
Scalable,
Resilient &
Responsive
Systems
Jonas Bonr
CTO Typesafe
@jboner
react to load
Scalable
react to load
Scalable
react to load
Scalable
react to load
Scalable
Reactive Applications
Reactive
Readily responsive to a stimulus
- Merriam Webster
Scalable
Resilient
Event-Driven
Event-Driven
The flow of the program is determined by events
- Wikipedia
...leads to
1. Never block
1. Never block
...unless you really have to
Blocking kills scalability (& performance)
Never sit on resources you dont use
Use non-blocking IO
Use lock-free concurrency
2. Go Async
Design for reactive event-driven systems
Gives you
1. lower latency
2. better throughput
3. a more loosely coupled architecture, easier to
extend, evolve & maintain
Amdahls Law
Amdahls Law
e
b
o
t
s
d
e
e
N
m
o
r
f
e
v
i
t
c
a
e
M
R
O
T
T
O
B
o
t
P
O
T
Actors
ShareNOTHING
Isolated lightweight event-based processes
Each actor has a mailbox (message queue)
Communicates through asynchronous &
non-blocking message passing
Location transparent (distributable)
Supervision-based failure management
Examples: Akka & Erlang
Actors in Akka
public class Greeting implements Serializable {
public final String who;
public Greeting(String who) { this.who = who; }
}
!
Actors in Akka
Define the message(s) the Actor
should be able to respond to
Actors in Akka
Define the message(s) the Actor
should be able to respond to
Actors in Akka
Define the message(s) the Actor
should be able to respond to
Agents
Reactive memory cells
Send a update function to the Agent, which
1. adds it to an (ordered) queue, to be
2. applied to the Agent async & non-blocking
Agents in Akka
Futures/Dataflow
Allows you to spawn concurrent computations
and work with the not yet computed results
Write-once, Read-many
Freely sharable
Allows non-blocking composition
Monadic (composes in for-comprehensions)
Build in model for managing failure
Futures in Scala
val result1 = future { ... }
val result2 = future { ... }
val result3 = future { ... }
!
val sum
r1 <r2 <r3 <} yield
= for {
result1
result2
result3
{ r1 + r2 + r3 }
RxJava
getDataFromNetwork()
.skip(10)
.take(5)
.map({ s -> return s + " transformed" })
.subscribe({ println "onNext => " + it })
Scalable
Capable of being easily expanded or upgraded on demand
- Merriam Webster
new normal
new normal
You already have a distributed system,
whether you want it or not
new normal
You already have a distributed system,
whether you want it or not
Mobile
NOSQL DBs
SQL Replication
Cloud Services
Availability
Providing resilience if
one node fails
Availability
Providing resilience if
one node fails
The problem?
The problem?
It is still
Very Hard
No difference
Between a
Slow node
and a
Dead node
The network is
Inherently Unreliable
The network is
Inherently Unreliable
http://aphyr.com/posts/288-the-network-is-reliable
Fallacies
Peter
Deutschs
8 Fallacies
of
Distributed
Computing
Fallacies
Peter
Deutschs
8 Fallacies
of
Distributed
Computing
EVIL
EVIL
EVIL
EVIL
EVIL
EVIL
Instead
Instead
t
i
h
n
a
b
d
e
od n
e
t
i
w
We need
Location Transparency
What is
Location Transparency?
// send message to local actor
ActorRef localGreeter = system.actorOf(
new Props(GreetingActor.class), greeter");
!
localGreeter.tell(Jonas);
What is
Location Transparency?
// send message to local actor
ActorRef localGreeter = system.actorOf(
new Props(GreetingActor.class), greeter");
!
localGreeter.tell(Jonas);
remoteGreeter.tell(Jonas);
What is
Location Transparency?
e
c
n
e
r
e
f
f
i
d
o
localGreeter.tell(Jonas);
remoteGreeter.tell(Jonas);
Partition
for scale
Replicate
for resilience
Share
Nothing
Share
Nothing
Asynchronous
Communication
Share
Nothing
Asynchronous
Communication
Loose
Coupling
Share
Nothing
Asynchronous
Communication
Loose
Coupling
Location
Transparency
Share
Nothing
Asynchronous
Communication
Loose
Coupling
Location
Transparency
No limit to scalability
Share
Nothing
Asynchronous
Communication
Loose
Coupling
Location
Transparency
Close to at least
No limit to scalability
Resilience
The ability of a substance or object to spring back into shape.
The capacity to recover quickly from difficulties.
- Merriam Webster
o
d
n
a
c
e
W
!
!
!
R
E
T
T
E
B
Use Bulkheads
Supervision in Akka
Every single actor has a
default supervisor strategy.
Which is usually sufficient.
But it can be overridden.
Supervision in Akka
Every single actor has a
default supervisor strategy.
Which is usually sufficient.
But it can be overridden.
class Supervisor extends UntypedActor {
private SupervisorStrategy strategy = new OneForOneStrategy(
10,
Duration.parse("1 minute"),
new Function<Throwable, Directive>() {
@Override public Directive apply(Throwable t) {
if (t instanceof ArithmeticException)
return resume();
else if (t instanceof NullPointerException) return restart();
else
return escalate();
}
});
Supervision in Akka
class Supervisor extends UntypedActor {
private SupervisorStrategy strategy = new OneForOneStrategy(
10,
Duration.parse("1 minute"),
new Function<Throwable, Directive>() {
@Override public Directive apply(Throwable t) {
if (t instanceof ArithmeticException)
return resume();
else if (t instanceof NullPointerException) return restart();
else
return escalate();
}
});
Responsive
Quick to respond or react appropriately
- Merriam Webster
Keep
latency consistent
1. Blue sky scenarios
2. Traffic bursts
3. Failures
Keep
latency consistent
1. Blue sky scenarios
2. Traffic bursts
3. Failures
always be responsive
L=W
Queue Length = Arrival Rate * Response Time
L=W
Queue Length = Arrival Rate * Response Time
Response Time = Queue Length / Arrival Rate
L=W
Queue Length = Arrival Rate * Response Time
Response Time = Queue Length / Arrival Rate
L=W
Queue Length = Arrival Rate * Response Time
Response Time = Queue Length / Arrival Rate
Smart Batching
Smart Batching
http://bit.ly/smartbatching
By Martin Thompson
Smart Batching
Smart Batching
http://bit.ly/smartbatching
By Martin Thompson
2. Reactive Composition
2. Reactive Composition
3. Reactive Push
Stream Producer
2. Reactive Composition
3. Reactive Push
Stream Producer
2. Reactive Composition
3. Reactive Push
Stream Producer
2. Reactive Composition
3. Reactive Push
Stream Producer
http://typesafe.com/platform/getstarted
Demo time
http://typesafe.com/platform/getstarted
Responsive
Scalable
Resilient
Event-Driven
http://reactivemanifesto.org
Jonas Bonr
CTO Typesafe
Twitter: @jboner
Go Reactive
Email: [email protected]
Web:
typesafe.com
Twtr: @jboner