Reactive programming wwiitthh SSccaallaa aanndd AAkkkkaa 
Piyush Mishra 
Software Consultant 
Knoldus Software LLP
AAggeennddaa 
● Reactive programming and why we need it. 
● Brief introduction to Scala 
● Brief introduction to Akka 
● Reactive applications. 
● Four principles of reactive applications.
PPrreesseenntt DDaayy 
HHooww ddoo wwee wwrriittee aapppplliiccaattiioonnss ttooddaayy?? 
A Foo
PPrreesseenntt DDaayy 
Till now we program like this 
(new Foo).doSomething1(); 
(new Foo).doSomething2(); 
(new Foo).doSomething3(); 
(new Foo).printOutput 
Synchronous ? 
Blocking ? 
Sequential ?
Reactive programming is completely different from the 
sequential work flow. They react when something is done 
Worker A 
Done and react 
Worker B 
Done and react 
Worker C 
Done and react 
Output 
Reactive programming
Reactive pprrooggrraammmmiinngg –– WWhhyy?? 
Fast, faster, fastest 
….. fastetstestestest ...
Reactive pprrooggrraammmmiinngg –– WWhhyy??
WWhhaatt iiss rreeaaccttiivvee??
What iiss rreeaaccttiivvee -- DDiisssseeccttiioonn
What iiss rreeaaccttiivvee -- DDiisssseeccttiioonn
What iiss rreeaaccttiivvee -- DDiisssseeccttiioonn
What iiss rreeaaccttiivvee -- DDiisssseeccttiioonn
What iiss rreeaaccttiivvee -- TTooggeetthheerr
Reactive – Parts – Event Driven 
Concurrent application uses shared memory to 
communicate. 
Thread A Thread B 
R/W R/W 
Shared memory 
Synchronization Non-determinism Hard to understand code
Reactive – Parts – Event Driven 
Event driven system avoid shared memory communication 
And uses message passing model throughout the 
Application 
Actor A Message Actor B 
Continued..
Reactive –– PPaarrttss –– EEvveenntt DDrriivveenn 
Sender Message Receiver 
Loosely coupled Easier to extend Evolve 
Receiver dormant till it receives message 
Large number of receivers share same hardware thread 
Low latency High throughput 
Lower operational costs
Reactive –– PPaarrttss –– EEvveenntt DDrriivveenn 
UI 
Web Layer 
Services 
Database 
Application must be 
reactive from top to 
bottom. 
None of the layers 
should stall.
Reactive –– PPaarrttss –– EEvveenntt DDrriivveenn 
We need to go in parallel. PERIOD!
Reactive –– PPaarrttss –– SSccaallaabbllee 
The word scalable is defined by Merriam- 
Webster as “capable of being easily expanded 
or upgraded on demand”
Reactive –– PPaarrttss –– SSccaallaabbllee 
Node 1 
Actor A 
Node 2 
Message Actor B 
Asynchronous message passing 
Location transparency 
The network is inherently unreliable
RReeaaccttiivvee –– PPaarrttss –– RReessiilliieenntt 
Isolate failures Prevent cascading failures
RReeaaccttiivvee –– PPaarrttss –– RReessiilliieenntt 
Node 1 
Actor A 
Node 2 
Message Actor B 
Loose coupling Isolated components 
Hierarchy of failure management
Reactive –– PPaarrttss –– RReessppoonnssiivvee 
Responsive is defined by Merriam- 
Webster as "quick to respond or 
react appropriately". 
Respond immediately 
Even in event of failure
Reactive –– PPaarrttss –– RReessppoonnssiivvee 
Response event is returned in O(1) or at least O(log n) 
time regardless of load 
How ? 
Batching when there is high traffic 
Queues bound with back pressure 
Failures handled with recovery and circuit breakers
RReeaaccttiivvee –– tthhee wwhhoollee
RReeaaccttiivvee –– HHooww??
RReeaaccttiivvee –– HHooww??
SSccaallaa BBaassiiccss
SSccaallaa BBaassiiccss
SSccaallaa BBaassiiccss 
1. Scala method definitions 
def fun(x: Int) = { 
result 
} 
2 . def fun = result 
3 .Scala variable definitions 
var x: Int = expression 
val x: String = expression 
1. Java method definitions 
Int fun(int x) { 
return result 
} 
2. (no parameterless methods) 
3. java variable definitions 
Int x = expression 
final String x = expression
Scala BBaassiiccss –– CCoonncciissee ccooddee
SSccaallaa BBaassiiccss 
pure object system 
operator overloading 
closures 
mixin composition with traits special 
treatment of interfaces 
existential types 
abstract 
pattern matching 
static members 
primitive types 
break and continue 
Interfaces 
Wildcards 
raw types 
enums
What is Akka 
Akka is the framework developed by Typesafe 
Right abstraction with actors for concurrent, fault-tolerant and 
scalable applications 
For Fault-Tolerance uses “Let It Crash” model 
Abstraction for transparent distribution of load 
We can Scale In and Scale Out
Problem Which Akka Solve 
Truly highly concurrent systems 
Truly scalable systems 
Self-healing, fault-tolerant systems
Akka 
Never think in terms of shared state, state visibility, threads, 
locks, concurrent collections, thread notification etc 
Low level concurrency becomes Simple Workflow - we only think 
in terms of message flows in system 
We get high CPU utilization, low latency, high throughput and 
scalability - for free as part of this model 
Proven and superior model for detecting and recovering from 
errors
Akka 
Main part of Akka 
Understanding the ACTOR model
Actor Model 
Messages are in mailbox but no thread is allocated
Actor Model 
Actor read the message , a thread is allocated to it 
And it apply behavior on it and state change
Actor Model 
After processing thread is deallocated
DDeeffiinnee aaccttoorr 
Define message on which actor will react. 
case object Hello 
Define actor 
Class HelloActor extends Actor 
{ 
def receive = { 
case Hello => println(“Hello”) 
case _=> println(“I did not understand”) 
} 
}
Tying it back – Event Driven 
Message Passing 
! ?
Tying it back – Event Driven 
Code
Tying it back – Resilience
RReessiilliieenntt 
Applications which are fault tolerant, detect failure and 
recover from failure are resilient. 
Such system are self healed and unstoppable
Actor System 
Guardian actor 
Actor A Actor B 
Actor C Actor D 
Actor System can be think of as home for actors
Actors form parent child relationship 
Guardian actor 
Actor A Actor B 
/A 
Actor C Actor D 
/A/C 
/B 
/B/D
OOnnee ffoorr oonnee SSttrraatteeggyy 
Guardian 
System actor 
X Y z
OOnnee ffoorr oonnee SSttrraatteeggyy 
Guardian 
System actor 
X Y z 
Only x will be restated
OOnnee ffoorr aallll SSttrraatteeggyy 
Guardian 
System actor 
X Y z
OOnnee ffoorr aallll SSttrraatteeggyy 
Guardian 
System actor 
X 
Y 
z 
All children will be restated
Tying it back – Resilience 
Code
Tying it back – Scalability
SSccaallaabbllee 
Applications which are able to scale up/down as the load 
Increases or decreases are scalable. 
Scaling is of two types. 
1. Scaling the application on all cpu's core is called scale 
up. 
2. Scaling the application to multiple machine is called 
scale out
AAkkkkaa SSccaallaabbiilliittyy CCooddee
RReessppoonnssiivvee 
Application which are able to respond even if failure occurs 
or load increases are responsive.
RReessppoonnssiivvee 
Bringing these three traits (event driven, resilient, and 
Scalable) together make to the applications responsive 
and all these four qualities together make the application 
reactive.
Reactive ppllaattffoorrmm –– ccoommppoonneennttss 
Play (Reactive we application framework built on top of 
akka) 
Akka Clustering 
Akka persistence 
Adaptive load balancing 
Software Transnational memory
TThhaannkkss

Reactive programming with scala and akka

  • 1.
    Reactive programming wwiitthhSSccaallaa aanndd AAkkkkaa Piyush Mishra Software Consultant Knoldus Software LLP
  • 2.
    AAggeennddaa ● Reactiveprogramming and why we need it. ● Brief introduction to Scala ● Brief introduction to Akka ● Reactive applications. ● Four principles of reactive applications.
  • 3.
    PPrreesseenntt DDaayy HHoowwddoo wwee wwrriittee aapppplliiccaattiioonnss ttooddaayy?? A Foo
  • 4.
    PPrreesseenntt DDaayy Tillnow we program like this (new Foo).doSomething1(); (new Foo).doSomething2(); (new Foo).doSomething3(); (new Foo).printOutput Synchronous ? Blocking ? Sequential ?
  • 5.
    Reactive programming iscompletely different from the sequential work flow. They react when something is done Worker A Done and react Worker B Done and react Worker C Done and react Output Reactive programming
  • 6.
    Reactive pprrooggrraammmmiinngg ––WWhhyy?? Fast, faster, fastest ….. fastetstestestest ...
  • 7.
  • 8.
  • 9.
    What iiss rreeaaccttiivvee-- DDiisssseeccttiioonn
  • 10.
    What iiss rreeaaccttiivvee-- DDiisssseeccttiioonn
  • 11.
    What iiss rreeaaccttiivvee-- DDiisssseeccttiioonn
  • 12.
    What iiss rreeaaccttiivvee-- DDiisssseeccttiioonn
  • 13.
    What iiss rreeaaccttiivvee-- TTooggeetthheerr
  • 14.
    Reactive – Parts– Event Driven Concurrent application uses shared memory to communicate. Thread A Thread B R/W R/W Shared memory Synchronization Non-determinism Hard to understand code
  • 15.
    Reactive – Parts– Event Driven Event driven system avoid shared memory communication And uses message passing model throughout the Application Actor A Message Actor B Continued..
  • 16.
    Reactive –– PPaarrttss–– EEvveenntt DDrriivveenn Sender Message Receiver Loosely coupled Easier to extend Evolve Receiver dormant till it receives message Large number of receivers share same hardware thread Low latency High throughput Lower operational costs
  • 17.
    Reactive –– PPaarrttss–– EEvveenntt DDrriivveenn UI Web Layer Services Database Application must be reactive from top to bottom. None of the layers should stall.
  • 18.
    Reactive –– PPaarrttss–– EEvveenntt DDrriivveenn We need to go in parallel. PERIOD!
  • 19.
    Reactive –– PPaarrttss–– SSccaallaabbllee The word scalable is defined by Merriam- Webster as “capable of being easily expanded or upgraded on demand”
  • 20.
    Reactive –– PPaarrttss–– SSccaallaabbllee Node 1 Actor A Node 2 Message Actor B Asynchronous message passing Location transparency The network is inherently unreliable
  • 21.
    RReeaaccttiivvee –– PPaarrttss–– RReessiilliieenntt Isolate failures Prevent cascading failures
  • 22.
    RReeaaccttiivvee –– PPaarrttss–– RReessiilliieenntt Node 1 Actor A Node 2 Message Actor B Loose coupling Isolated components Hierarchy of failure management
  • 23.
    Reactive –– PPaarrttss–– RReessppoonnssiivvee Responsive is defined by Merriam- Webster as "quick to respond or react appropriately". Respond immediately Even in event of failure
  • 24.
    Reactive –– PPaarrttss–– RReessppoonnssiivvee Response event is returned in O(1) or at least O(log n) time regardless of load How ? Batching when there is high traffic Queues bound with back pressure Failures handled with recovery and circuit breakers
  • 25.
  • 26.
  • 27.
  • 28.
  • 30.
  • 31.
    SSccaallaa BBaassiiccss 1.Scala method definitions def fun(x: Int) = { result } 2 . def fun = result 3 .Scala variable definitions var x: Int = expression val x: String = expression 1. Java method definitions Int fun(int x) { return result } 2. (no parameterless methods) 3. java variable definitions Int x = expression final String x = expression
  • 32.
    Scala BBaassiiccss ––CCoonncciissee ccooddee
  • 33.
    SSccaallaa BBaassiiccss pureobject system operator overloading closures mixin composition with traits special treatment of interfaces existential types abstract pattern matching static members primitive types break and continue Interfaces Wildcards raw types enums
  • 34.
    What is Akka Akka is the framework developed by Typesafe Right abstraction with actors for concurrent, fault-tolerant and scalable applications For Fault-Tolerance uses “Let It Crash” model Abstraction for transparent distribution of load We can Scale In and Scale Out
  • 35.
    Problem Which AkkaSolve Truly highly concurrent systems Truly scalable systems Self-healing, fault-tolerant systems
  • 36.
    Akka Never thinkin terms of shared state, state visibility, threads, locks, concurrent collections, thread notification etc Low level concurrency becomes Simple Workflow - we only think in terms of message flows in system We get high CPU utilization, low latency, high throughput and scalability - for free as part of this model Proven and superior model for detecting and recovering from errors
  • 37.
    Akka Main partof Akka Understanding the ACTOR model
  • 38.
    Actor Model Messagesare in mailbox but no thread is allocated
  • 39.
    Actor Model Actorread the message , a thread is allocated to it And it apply behavior on it and state change
  • 40.
    Actor Model Afterprocessing thread is deallocated
  • 41.
    DDeeffiinnee aaccttoorr Definemessage on which actor will react. case object Hello Define actor Class HelloActor extends Actor { def receive = { case Hello => println(“Hello”) case _=> println(“I did not understand”) } }
  • 42.
    Tying it back– Event Driven Message Passing ! ?
  • 43.
    Tying it back– Event Driven Code
  • 44.
    Tying it back– Resilience
  • 45.
    RReessiilliieenntt Applications whichare fault tolerant, detect failure and recover from failure are resilient. Such system are self healed and unstoppable
  • 46.
    Actor System Guardianactor Actor A Actor B Actor C Actor D Actor System can be think of as home for actors
  • 47.
    Actors form parentchild relationship Guardian actor Actor A Actor B /A Actor C Actor D /A/C /B /B/D
  • 48.
    OOnnee ffoorr oonneeSSttrraatteeggyy Guardian System actor X Y z
  • 49.
    OOnnee ffoorr oonneeSSttrraatteeggyy Guardian System actor X Y z Only x will be restated
  • 50.
    OOnnee ffoorr aallllSSttrraatteeggyy Guardian System actor X Y z
  • 51.
    OOnnee ffoorr aallllSSttrraatteeggyy Guardian System actor X Y z All children will be restated
  • 52.
    Tying it back– Resilience Code
  • 53.
    Tying it back– Scalability
  • 54.
    SSccaallaabbllee Applications whichare able to scale up/down as the load Increases or decreases are scalable. Scaling is of two types. 1. Scaling the application on all cpu's core is called scale up. 2. Scaling the application to multiple machine is called scale out
  • 55.
  • 56.
    RReessppoonnssiivvee Application whichare able to respond even if failure occurs or load increases are responsive.
  • 57.
    RReessppoonnssiivvee Bringing thesethree traits (event driven, resilient, and Scalable) together make to the applications responsive and all these four qualities together make the application reactive.
  • 58.
    Reactive ppllaattffoorrmm ––ccoommppoonneennttss Play (Reactive we application framework built on top of akka) Akka Clustering Akka persistence Adaptive load balancing Software Transnational memory
  • 59.

Editor's Notes