HOW REACTIVE CAN YOU BE?
DomCode 26th September 2017
Reactive Programming or Reactive Systems?
(spoiler: both)
@ticofabA little info about myself - Fabio Tiriticco
Tech Lead /
Software Architect
@ticofab
@ticofab
1. Reactive Programming
2. Reactive Systems
3. Building blocks of Reactive Systems: Actor programming
4. Reactive Programming vs Reactive Systems
Outline
Goal: no more confusion about “reactive”
@ticofab
1.
Reactive Programming
@ticofabThe World is a Streaming Place
Many processes can be modelled as a stream.
@ticofabThe World is a Streaming Place
Many processes can be modelled as a stream.
@ticofabThe World is a Streaming Place
Many processes can be modelled as a stream.
@ticofabThe World is a Streaming Place
@ticofabOne challenge: different speed of producer and consumer
100 op/sec 10 op/sec
Backpressure
==
“Dear sender,
please slow down!”
@ticofabOne challenge: different speed of producer and consumer
Backpressure
==
Belt stops moving
@ticofabOne challenge: different speed of producer and consumer
Backpressure
==
Traffic light signal
@ticofabReactive Programming & its benefits
1. Stream-like processing
2. Easy management of back pressure
3. Conciseness
4. Simplification of parallel / threaded work
“A paradigm where the logic is driven forward
by the availability of new information”
“As soon as there is new input, react to it”
@ticofab
Observable.from(myCats)
Reactive Programming example (RxJava on Android)
List<Cat> myCats;
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.filter(cat -> cat.isWhite())
.map(cat -> cat.fetchPicture())
.map(picture -> Filter.applyFilter(picture))
.subscribe(filteredPicture -> display(filteredPicture));
.onBackpressureBuffer(16)
1. Stream-like processing
2. Conciseness
3. Simplification of threaded work
4. Easy management of back pressure
… …
@ticofab
2.
Reactive Systems
@ticofabGoal
“Reactive Systems strive to increase productivity and make
sure that development and maintenance of components
reduce the accidental complexity to a minimum.”
the ability to react quickly
and appropriately to change
agility
əˈdʒɪlɪti/
Development level
increase productivity
facilitate development
facilitate maintenance
reduce accidental complexity
Company as whole
@ticofabThe Reactive Principles
Define a way of thinking about system architectures in a
modern and distributed environment. In a Reactive System,
the interaction between the parts makes the difference.
@ticofabThe Reactive Manifesto, 2013
A reactive computer system must Trait
React to its users Responsive
React to failure and stay available Resilient
React to varying load conditions Elastic
Its components must react to inputs Message-driven
@ticofabReactive System traits (the Reactive Manifesto)
J. Boner, R. Kuhn, D. Farley, M. Thompson - The Reactive Manifesto
@ticofabFrom Principles to implementation
Reactive
Principles
Reactive
Patterns
Reactive
Tools
“Akka is a toolkit for building highly concurrent,
distributed, and resilient message-driven applications”
@ticofabAkka goes on all the way up to Reactive Systems
@ticofab
4.
Building blocks of Reactive Systems:
Actor Programming
@ticofabSimple Component Pattern
“One component should do only one thing but do it in
full. The aim is to maximise cohesion and minimise
coupling between components.”
@ticofab
Actor 1
Actor 3
Actor 2
• contains state &
behaviour logic
Actor model
Supervisor
Simple Component Pattern
• has a mailbox to
receive and send
messages
• has a supervisor
myActor3.getCounter()
2
2
counter value?
@ticofabThe benefits of Asynchronous Messaging
• Separation between components
• Domain mapping closer to reality
• Error containment - avoid chain failures
Actor 1
Actor 3
Actor 2
Supervisor
2
2
counter value?
@ticofabCounter Actor example
Counter
Actor
Increment
Actor
Increment
Get Value
Current Value
@ticofabLet it Crash Pattern
"Prefer a full component restart to
complex internal failure handling".
• failure conditions WILL occur
• they might be rare and hard to reproduce
• easier to start clean than to try to recover
Embrace failure by design!
@ticofab
Actor supervision example
Actor 1
Actor 2
Supervisor
WhateverException
X
Fix or
Restart
(doing something else…)
Let it Crash Pattern
@ticofab
Actor 1
Actor 2
Supervisor
HorribleException
X
Fix or
Restart
(doing something else…)
Let it Crash Pattern
• The two actors work independently,
which is great for concurrency
• The user of a service does not deal
with the failures in the service
• The service itself is simpler, without
try / catch blocks
@ticofabOld school error handling
ArrayList<Result> results;
try {
results = myDatabase.query(...)
} catch(Exception e) {
e.printStackTrace();
}
getResults(params, function(err,result) {
// did an error occur?
if ( err ) {
// handle the error safely
console.log('4/0=err', err)
} else {
// no error occured, go on
console.log('4/0='+result)
}
})
@ticofab
Scenario:
The machine is out of
coffee beans
Failure!
( not an error )
Let it Crash Pattern
@ticofabLet it Crash Pattern
@ticofabThe CoffeeMachine, implemented
User
Coffee
Machine
Supervisor
Give Me Caffeine
Here is your coffee
@ticofab
4.
Reactive Programming
vs Reactive Systems
@ticofabReactive Programming vs Reactive Systems
What is Good for Applicability Productive for
Reactive
Programming
Implementation
technique
Asynchronous
dataflow
management
Single node or
service
Developers
Reactive Systems
Set of design
principles
Orchestration of
complex systems
The whole system
(from single node to
the total sum)
Architects
@ticofabReactive Programming vs Reactive Systems
@ticofabRecap
Slides: https://goo.gl/yVwByD
Code: https://github.com/ticofab/ActorDemo
1. Reactive Programming
2. Reactive Systems
3. Building blocks of Reactive Systems: Actor programming
4. Reactive Programming vs Reactive Systems
@ticofab
5. (bonus section)
FAQ / Quiz
@ticofabQuiz
Can one say “I used reactive to build my thing” ?
A. Yes
B. No
C. Maybe
“Reactive” is an adjective!
@ticofabQuiz
Can one say “I used Akka to build my reactive thing” ?
A. Yes
B. No
C. Maybe
@ticofabQuiz
Is React.js somehow related to
Reactive programming or Reactive Systems?
A. Yes
B. No
C. Maybe
@ticofabQuiz
Can React.js be used in combination with Reactive Programming?
A. Yes
B. No
C. Maybe
RxJS, Bacon.js etc.
@ticofabQuiz
Why did Facebook chose the name “React.js” ?
A. They hadn’t seen my talk
B. All others 5-letter names were taken
C. Because views are re-rendered as data changes without having
to imperatively make changes to the DOM.
@ticofabQuiz
Do I hate Facebook for choosing such name?
A. Yes
B. Yes
C. YES
All answers are correct.
@ticofabReactive Amsterdam meetup
@ticofabReactive Summit
@ticofab
Questions?
@ticofab
@ticofab
Thanks!
@ticofab

Reactive Programming or Reactive Systems? (spoiler: both)