SlideShare a Scribd company logo
1 of 45
Download to read offline
DESIGNING REACTIVE SYSTEMS
WITH AKKA
Thomas Lockney • @tlockney
http://thomas.lockney.net
OSCON 2015 • Portland, Oregon
WHO AM I?
Engineering at Nike+/CDT
Past experience at Cisco, Janrain, Simple, IBM/
Tivoli, etc.
Founder of PNWScala, PDXScala, & various
events/groups
OUR MODERN WORLD
Distributed
Mobile
Unreliable, frequent disconnects
“The Cloud”
OUR MODERN WORLD
OUR MODERN WORLD
OUR MODERN WORLD
REACTIVE SOFTWARE
Message-driven
Resilient
Elastic
Responsive
APPLYING OLD PATTERNS
TO
NEW CHALLENGES
INTRODUCING AKKA
Scalable concurrency toolkit
Actor model implementation (more on this shortly)
Fault tolerance
Distributed by default
THE ACTOR MODEL
Message-passing
Encapsulation of state and behavior
Mutable state is protected
Sequential internally, asynchronous between actors
A SIMPLE EXAMPLE
case class Greeting(name: String)


class GreetingActor extends Actor with ActorLogging {

def receive = {

case Greeting(name) ⇒

log.info("Hello, {}", name)

}

}



val system = ActorSystem("MyExampleSystem")

val greeter = system.actorOf(Props[GreetingActor],

name = "greeter")

greeter ! Greeting("Thomas")
case class


class
log.info(
}
}



val
val
name =
greeter
case class Greeting(name: String)


A SIMPLE EXAMPLE
def receive = {

case Greeting(name) ⇒

log.info("Hello, {}", name)

}

val system = ActorSystem("MyExampleSystem")

val greeter = system.actorOf(Props[GreetingActor],

name = "greeter")
greeter ! Greeting("Thomas")
protocol
behavior
actor system
actor creation
sending a message
case class


class
log.info(
}
}



val
val
name =
greeter
case class Greeting(name: String)


class GreetingActor extends Actor with ActorLogging {

def receive = {

case Greeting(name) ⇒

log.info("Hello, {}", name)

}

}



val system = ActorSystem("MyExampleSystem")

val greeter = system.actorOf(Props[GreetingActor],

name = "greeter")

greeter ! Greeting("Thomas")
WHAT AKKA PROVIDES
Actor hierarchy with supervision
Flexible message routing
Configurable scheduling via dispatchers
THE HIERARCHY
THE HIERARCHY
Guardian
THE HIERARCHY
Guardian
User
hierarchy
THE HIERARCHY
Guardian
User
hierarchy
User
actors
SUPERVISION
SUPERVISION
/a/c/f fails
SUPERVISION
supervisor
restarts
child
SUPERVISION
SUPERVISOR EXAMPLE
case object DieNow

case class DomainException(failureMsg: String)
extends Exception(failureMsg)



class Child extends Actor {

import context.dispatcher


scheduler.scheduleOnce(Random.nextInt(5000).milliseconds,
self, DieNow)



def receive = {

case DieNow ⇒ throw new DomainException("R.I.P.")

}

}
SUPERVISOR EXAMPLE
class TopLevel extends Actor {

def receive = Actor.emptyBehavior



override def preStart() = {
context.actorOf(Props[Child])
}



override def supervisorStrategy =

OneForOneStrategy(maxNrOfRetries = 2,
withinTimeRange = 10.seconds) {

case DomainException(msg) ⇒ Restart

}

}
DESIGNING WITH AKKA
Protocols
Workflow
Failure Handling
–Merriam-Webster
“a system of rules that explain the correct conduct and
procedures to be followed in formal situations.”
Protocol
PROTOCOLS
Define the interaction between actors
Specify clear boundaries of responsibility
Encode external representation of state at a point in time
Demarcate success and failure clearly
AVERY SIMPLE PROTOCOL
trait Response

case class Success(res: Array[Byte]) extends Response

case class Failure(err: String) extends Response
WORKFLOW
Work-pulling
Pipelines
WORK-PULLING
Enables a simple form of back-
pressure
Decouples failure handling from the
workflow
WORK-PULLING
EXAMPLE
case object RequestWork

case class Work(id: String, data: String)
class Worker(queue: ActorRef) extends Actor with ActorLogging {



override def preStart = {

queue ! RequestWork

}



def receive = {

case w:Work ⇒

log.info(w.data.toUpperCase)

queue ! RequestWork

}



}
EXAMPLE
class Queuer extends Actor {


val queuedWork = Queue.empty[Work]

val requestors = Queue.empty[ActorRef]

def receive = {

case RequestWork if (queuedWork.nonEmpty) ⇒

sender ! queuedWork.dequeue

case RequestWork ⇒

requestors.enqueue(sender)

case w:Work if (requestors.nonEmpty) ⇒

requestors.dequeue ! w

case w:Work ⇒

queuedWork.enqueue(w)

}


}
PIPELINES
Commonly found in data processing applications
Distinct phases of data responsibility
May span one or more systems
Often have radically varying scaling needs
PIPELINES
Each phase has its own failure domain
Back-pressure is managed through the chain
The simplified view
PIPELINES
Each phase has a failure domain
Back-pressure is still managed through the
chain
The more typical view
PIPELINES
Often just an extension of more basic patterns
Work-pulling is a common component
Keep an eye on Akka Streams!
FAULT-TOLERANCE
Circuit-breakers
Isolate key systems
Fault isolation and failure-domains
Restrict the size of the impact
CIRCUIT-BREAKER
Isolate important components
Avoid cascading failures
CIRCUIT-BREAKER
class FailingActor extends Actor {

import context.dispatcher

system.scheduler.scheduleOnce(Random.nextInt(5000).milliseconds, self, Die)

def receive = {

case Die ⇒

throw new Exception("I've fallen and I can't get up!")

case s: String ⇒ sender ! s.toUpperCase

}

}
CIRCUIT-BREAKER
val breaker =

new CircuitBreaker(context.system.scheduler,

maxFailures = 5,

callTimeout = 10.seconds,

resetTimeout = 1.minute)



def receive = {

case Tick ⇒

val in = Random.alphanumeric.take(10).mkString

breaker.withCircuitBreaker(toUpper ? in) pipeTo self

case s:String ⇒

log.info("Received: {}", s)

}
FAILURE-DOMAINS AND FAULT ISOLATION
FAILURE-DOMAINS AND FAULT ISOLATION
These are failure-domains
FAILURE-DOMAINS AND FAULT ISOLATION
FAILURE-DOMAINS AND FAULT ISOLATION
These don’t have to care
THANKYOU! QUESTIONS?

More Related Content

Viewers also liked

Введение в Akka
Введение в AkkaВведение в Akka
Введение в Akka
Zheka Kozlov
 
Язык программирования Scala / Владимир Успенский (TCS Bank)
Язык программирования Scala / Владимир Успенский (TCS Bank)Язык программирования Scala / Владимир Успенский (TCS Bank)
Язык программирования Scala / Владимир Успенский (TCS Bank)
Ontico
 

Viewers also liked (20)

Play Template Engine Based On Scala
Play Template Engine Based On ScalaPlay Template Engine Based On Scala
Play Template Engine Based On Scala
 
HTML5 with Play Scala, CoffeeScript and Jade - Devoxx 2011
HTML5 with Play Scala, CoffeeScript and Jade - Devoxx 2011HTML5 with Play Scala, CoffeeScript and Jade - Devoxx 2011
HTML5 with Play Scala, CoffeeScript and Jade - Devoxx 2011
 
Your First Scala Web Application using Play 2.1
Your First Scala Web Application using Play 2.1Your First Scala Web Application using Play 2.1
Your First Scala Web Application using Play 2.1
 
Play framework And Google Cloud Platform GCP.
Play framework And Google Cloud Platform GCP.Play framework And Google Cloud Platform GCP.
Play framework And Google Cloud Platform GCP.
 
Voxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVM
Voxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVMVoxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVM
Voxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVM
 
Введение в Akka
Введение в AkkaВведение в Akka
Введение в Akka
 
Reactive Web-Applications @ LambdaDays
Reactive Web-Applications @ LambdaDaysReactive Web-Applications @ LambdaDays
Reactive Web-Applications @ LambdaDays
 
Akka-http
Akka-httpAkka-http
Akka-http
 
Lagom in Practice
Lagom in PracticeLagom in Practice
Lagom in Practice
 
Dependency injection in scala
Dependency injection in scalaDependency injection in scala
Dependency injection in scala
 
Akka http 2
Akka http 2Akka http 2
Akka http 2
 
Язык программирования Scala / Владимир Успенский (TCS Bank)
Язык программирования Scala / Владимир Успенский (TCS Bank)Язык программирования Scala / Владимир Успенский (TCS Bank)
Язык программирования Scala / Владимир Успенский (TCS Bank)
 
Composable and streamable Play apps
Composable and streamable Play appsComposable and streamable Play apps
Composable and streamable Play apps
 
Akka http
Akka httpAkka http
Akka http
 
Practical Akka HTTP - introduction
Practical Akka HTTP - introductionPractical Akka HTTP - introduction
Practical Akka HTTP - introduction
 
Securing Microservices using Play and Akka HTTP
Securing Microservices using Play and Akka HTTPSecuring Microservices using Play and Akka HTTP
Securing Microservices using Play and Akka HTTP
 
Akka Streams and HTTP
Akka Streams and HTTPAkka Streams and HTTP
Akka Streams and HTTP
 
Building scalable rest service using Akka HTTP
Building scalable rest service using Akka HTTPBuilding scalable rest service using Akka HTTP
Building scalable rest service using Akka HTTP
 
Akka HTTP
Akka HTTPAkka HTTP
Akka HTTP
 
Building a Reactive RESTful API with Akka Http & Slick
Building a Reactive RESTful API with Akka Http & SlickBuilding a Reactive RESTful API with Akka Http & Slick
Building a Reactive RESTful API with Akka Http & Slick
 

Similar to Designing Reactive Systems with Akka

Concurrency on the JVM
Concurrency on the JVMConcurrency on the JVM
Concurrency on the JVM
Vaclav Pech
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
Akka london scala_user_group
Akka london scala_user_groupAkka london scala_user_group
Akka london scala_user_group
Skills Matter
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
Joe Zulli
 
Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scala
parag978978
 

Similar to Designing Reactive Systems with Akka (20)

Concurrency on the JVM
Concurrency on the JVMConcurrency on the JVM
Concurrency on the JVM
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
Message-based communication patterns in distributed Akka applications
Message-based communication patterns in distributed Akka applicationsMessage-based communication patterns in distributed Akka applications
Message-based communication patterns in distributed Akka applications
 
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
 
Akka london scala_user_group
Akka london scala_user_groupAkka london scala_user_group
Akka london scala_user_group
 
Scala - just good for Java shops?
Scala - just good for Java shops?Scala - just good for Java shops?
Scala - just good for Java shops?
 
Activator and Reactive at Play NYC meetup
Activator and Reactive at Play NYC meetupActivator and Reactive at Play NYC meetup
Activator and Reactive at Play NYC meetup
 
Asynchronous Orchestration DSL on squbs
Asynchronous Orchestration DSL on squbsAsynchronous Orchestration DSL on squbs
Asynchronous Orchestration DSL on squbs
 
The Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect SystemThe Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect System
 
A Survey of Concurrency Constructs
A Survey of Concurrency ConstructsA Survey of Concurrency Constructs
A Survey of Concurrency Constructs
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scalaz 8 vs Akka Actors
Scalaz 8 vs Akka ActorsScalaz 8 vs Akka Actors
Scalaz 8 vs Akka Actors
 
Event Sourcing - what could go wrong - Devoxx BE
Event Sourcing - what could go wrong - Devoxx BEEvent Sourcing - what could go wrong - Devoxx BE
Event Sourcing - what could go wrong - Devoxx BE
 
JavaScript Editions ES7, ES8 and ES9 vs V8
JavaScript Editions ES7, ES8 and ES9 vs V8JavaScript Editions ES7, ES8 and ES9 vs V8
JavaScript Editions ES7, ES8 and ES9 vs V8
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scala
 

Recently uploaded

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Recently uploaded (20)

SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 

Designing Reactive Systems with Akka