SlideShare a Scribd company logo
Actor-based concurrency 
and Akka 
Fundamentals 
Bon Festival 2014 
Ngoc Dao
Benefit of actor in one sentence 
C vs Java: 
You can use memory without having to free 
memory. 
Thread vs actor: 
You can use concurrency without having to 
create threads. 
Don't communicate by sharing memory; 
share memory by communicating.
Actor model 
● Actor = 
states + 
mailbox + 
behaviors (msg handlers) 
http://www.cs.tsukuba.ac.jp/~yas/c 
s/csys-2013/2013-12-03/ 
● From outside, can’t manipulate actors directly. 
● To interact with an actor, must send msgs to it. 
● Each actor has a mailbox, msgs are put to mailbox, and 
processed one by one. ← An actor is like a single 
threaded process; it doesn’t do more than one thing at a 
time.
Concurrency: Actor vs Thread 
Thread: 
● Heavy weight: Can only create not too many threads; 
usually: 2000~5000 
● Shared state ← Source of bugs 
● Passive: Have to call object.method() to make the 
object alive. 
Actor: 
● Light weight: Can create millions of actors; 
usually: ~2.5 million actors/GB 
● Shared nothing 
● Active: Actors are alive by themselves. ← Easy to 
model programs that have millions of on-going things 
(very high level of concurrency), like MMOG games.
Concurrency: Actor vs Thread 
● Thread: n dimensions, hard to reason about. 
● Actor: 1D, one thing at a time. 
var1 
var2
Concurrency: Actor vs Thread 
● Actor is a high level logical way to think, to 
model programs. 
● At lower level, actors run above a thread 
pool.
Akka http://akka.io/ 
(an implementation of actor model)
Akka: Demo 
● Live coding demo
Demo: Counter 
// First, define messages 
object Counter { 
case class Inc(amount: Int) 
case class Dec(amount: Int) 
}
import akka.actor.Actor 
// Define actor state and msg handler 
class Counter(name: String) extends Actor { 
// Actor state 
private var value = 0 
override def preStart() { 
println(s"I'm alive: $name") 
} 
override def postStop() { 
println(s"I'm dead: $name") 
} 
...
... 
import Counter._ 
// Msg handler 
def receive = { 
case Inc(amount) => 
value += amount 
println(s"Value: $value") 
case Dec(amount) => 
value -= amount 
println(s"Value: $value") 
} 
}
import akka.actor.{ActorSystem, Props} 
// Think of this as a thread pool 
val system = ActorSystem("mysystem") 
// Create actor reference (instance) 
val c1 = system.actorOf( 
Props(new Counter("mylock")) 
) 
// Send messages to the actor 
c1 ! Counter.Inc(1) 
c1 ! Counter.Dec(2)
Demo: Behavior transition 
CodeLock: 
https://github.com/ngocdaothanh/code-lock-fsm-akka
Some actor antipatterns 
Send mutable msgs between actors. 
← May lead to bug, if actor A sends msg M to actor B, state 
of B incorporates M, then M is later changed by A. 
Fix: Use immutable msgs.
Some actor antipatterns 
From inside actor:outsideObject.foo(new 
Callback { 
def onCallback() { 
directlyModifyActorState() 
} 
}) ← May lead to bug, because the actor’s thread and the 
callback’s thread may be 2 different threads. Remember: 
An actor is like a single threaded process, can’t do more 
than one thing at a time. 
Fix: self ! msgFromCallback
References 
● Concurrent Programming for Scalable Web 
Architectureshttp://berb.github.io/diploma-thesis/ 
index.html 
● Functions + Messages + Concurrency = 
Erlanghttp://www.infoq.com/presentations/joe-armstrong- 
erlang-qcon08 
● Akka dochttp://doc.akka.io/docs/akka/2.3.5/scala.html

More Related Content

PDF
Model with actors and implement with Akka
ODP
How to start using Scala
KEY
Introduction to Actor Model and Akka
KEY
Actors and Threads
PDF
Actor model : A Different Concurrency Approach
PDF
A gentle introduction into AKKA and the actor model
PDF
Introduction to Actor Model and Akka
PPTX
I18nize Scala programs à la gettext
Model with actors and implement with Akka
How to start using Scala
Introduction to Actor Model and Akka
Actors and Threads
Actor model : A Different Concurrency Approach
A gentle introduction into AKKA and the actor model
Introduction to Actor Model and Akka
I18nize Scala programs à la gettext

What's hot (19)

PDF
Oslo.versioned objects - Deep Dive
PPT
Web development basics (Part-5)
PPT
Web development basics (Part-6)
PDF
Akka - A Brief Intro
PDF
Advanced Reflection in Pharo
PPTX
Akka framework
PPTX
JavaScript Basics
PDF
Actor Patterns and NATS - Boulder Meetup
PDF
Intro to javascript (4 week)
PDF
Reflection in Pharo: Beyond Smalltak
PPTX
Akka - young fighter course
PPT
Javascript Workshop
PDF
Dynamically Composing Collection Operations through Collection Promises
PPTX
jQuery (intermediate)
PPTX
JS - Basics
PPT
Web development basics (Part-4)
PPTX
Declarative JavaScript concepts and implemetation
PDF
Reflection in Pharo: Beyond Smalltak
PDF
Back to the future: Isomorphic javascript applications
Oslo.versioned objects - Deep Dive
Web development basics (Part-5)
Web development basics (Part-6)
Akka - A Brief Intro
Advanced Reflection in Pharo
Akka framework
JavaScript Basics
Actor Patterns and NATS - Boulder Meetup
Intro to javascript (4 week)
Reflection in Pharo: Beyond Smalltak
Akka - young fighter course
Javascript Workshop
Dynamically Composing Collection Operations through Collection Promises
jQuery (intermediate)
JS - Basics
Web development basics (Part-4)
Declarative JavaScript concepts and implemetation
Reflection in Pharo: Beyond Smalltak
Back to the future: Isomorphic javascript applications
Ad

Viewers also liked (10)

PPTX
Go lang - What is that thing?
PDF
Go Lang Tutorial
PPTX
Mongo db - How we use Go and MongoDB by Sam Helman
PDF
H20: A platform for big math
PPTX
Recommendation at Netflix Scale
PDF
Akka in 100 slides or less
PDF
Hadoop Operations Powered By ... Hadoop (Hadoop Summit 2014 Amsterdam)
PDF
Introducing Akka
PPTX
Go Programming Language (Golang)
PDF
Apache Hadoop Java API
Go lang - What is that thing?
Go Lang Tutorial
Mongo db - How we use Go and MongoDB by Sam Helman
H20: A platform for big math
Recommendation at Netflix Scale
Akka in 100 slides or less
Hadoop Operations Powered By ... Hadoop (Hadoop Summit 2014 Amsterdam)
Introducing Akka
Go Programming Language (Golang)
Apache Hadoop Java API
Ad

Similar to Actor-based concurrency and Akka Fundamentals (20)

PDF
Introducing Akka
PDF
Actor Model Akka Framework
PDF
Introduction to concurrent programming with akka actors
PDF
Introduction to concurrent programming with Akka actors
PPTX
Oop2011 actor presentation_stal
PPTX
Reactive Programming using Actor Model in Akka
PDF
Akka introtalk HyScala DEC 2016
PDF
Akka-intro-training-public.pdf
PDF
Building Massively Scalable application with Akka 2.0
PDF
Akka and futures
PDF
Akka lsug skills matter
PDF
Scaling Web Apps with Akka
PDF
Akka Actors: an Introduction
PPTX
Akka Actors
PDF
Introduction to Akka
PDF
Build Cloud Applications with Akka and Heroku
PDF
Akka in Action: Heiko Seeburger
PDF
Akka 2.0 Reloaded
PDF
Akka knolx
PPTX
Introduction to Akka - Atlanta Java Users Group
Introducing Akka
Actor Model Akka Framework
Introduction to concurrent programming with akka actors
Introduction to concurrent programming with Akka actors
Oop2011 actor presentation_stal
Reactive Programming using Actor Model in Akka
Akka introtalk HyScala DEC 2016
Akka-intro-training-public.pdf
Building Massively Scalable application with Akka 2.0
Akka and futures
Akka lsug skills matter
Scaling Web Apps with Akka
Akka Actors: an Introduction
Akka Actors
Introduction to Akka
Build Cloud Applications with Akka and Heroku
Akka in Action: Heiko Seeburger
Akka 2.0 Reloaded
Akka knolx
Introduction to Akka - Atlanta Java Users Group

More from Ngoc Dao (20)

PDF
Develop realtime web with Scala and Xitrum
PDF
BIG DATA サービス と ツール
PDF
How to write a web framework
PDF
Xitrum @ Scala Matsuri Tokyo 2014
PDF
Xitrum HOWTOs
ODP
Xitrum @ Scala Conference in Japan 2013
ODP
SockJS Intro
ODP
Easy distributed load test with Tsung
PPTX
Cloud Erlang
PPTX
Xitrum internals
PPT
Những lỗi bảo mật web thường gặp ở phần application
PPT
Erlang Web
PPT
Nitrogen Web Framework
PDF
スポイトができるまで
PDF
Camellia General
PPT
Nhập môn BDD
PPT
何でRuby
PDF
Sinh hoat CLB tin hoc Komaba lan 2 - Phat bieu cua Ngoc
PDF
Sinh hoat CLB tin hoc Komaba lan 1 - Phat bieu cua G
PDF
Sinh hoat CLB tin hoc Komaba lan 1 - Phat bieu cua Ngoc
Develop realtime web with Scala and Xitrum
BIG DATA サービス と ツール
How to write a web framework
Xitrum @ Scala Matsuri Tokyo 2014
Xitrum HOWTOs
Xitrum @ Scala Conference in Japan 2013
SockJS Intro
Easy distributed load test with Tsung
Cloud Erlang
Xitrum internals
Những lỗi bảo mật web thường gặp ở phần application
Erlang Web
Nitrogen Web Framework
スポイトができるまで
Camellia General
Nhập môn BDD
何でRuby
Sinh hoat CLB tin hoc Komaba lan 2 - Phat bieu cua Ngoc
Sinh hoat CLB tin hoc Komaba lan 1 - Phat bieu cua G
Sinh hoat CLB tin hoc Komaba lan 1 - Phat bieu cua Ngoc

Recently uploaded (20)

PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPT
Introduction Database Management System for Course Database
PDF
Digital Strategies for Manufacturing Companies
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Nekopoi APK 2025 free lastest update
PDF
medical staffing services at VALiNTRY
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Transform Your Business with a Software ERP System
PPTX
history of c programming in notes for students .pptx
Design an Analysis of Algorithms II-SECS-1021-03
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PTS Company Brochure 2025 (1).pdf.......
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Softaken Excel to vCard Converter Software.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Design an Analysis of Algorithms I-SECS-1021-03
Introduction Database Management System for Course Database
Digital Strategies for Manufacturing Companies
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
ISO 45001 Occupational Health and Safety Management System
Nekopoi APK 2025 free lastest update
medical staffing services at VALiNTRY
2025 Textile ERP Trends: SAP, Odoo & Oracle
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Which alternative to Crystal Reports is best for small or large businesses.pdf
Transform Your Business with a Software ERP System
history of c programming in notes for students .pptx

Actor-based concurrency and Akka Fundamentals

  • 1. Actor-based concurrency and Akka Fundamentals Bon Festival 2014 Ngoc Dao
  • 2. Benefit of actor in one sentence C vs Java: You can use memory without having to free memory. Thread vs actor: You can use concurrency without having to create threads. Don't communicate by sharing memory; share memory by communicating.
  • 3. Actor model ● Actor = states + mailbox + behaviors (msg handlers) http://www.cs.tsukuba.ac.jp/~yas/c s/csys-2013/2013-12-03/ ● From outside, can’t manipulate actors directly. ● To interact with an actor, must send msgs to it. ● Each actor has a mailbox, msgs are put to mailbox, and processed one by one. ← An actor is like a single threaded process; it doesn’t do more than one thing at a time.
  • 4. Concurrency: Actor vs Thread Thread: ● Heavy weight: Can only create not too many threads; usually: 2000~5000 ● Shared state ← Source of bugs ● Passive: Have to call object.method() to make the object alive. Actor: ● Light weight: Can create millions of actors; usually: ~2.5 million actors/GB ● Shared nothing ● Active: Actors are alive by themselves. ← Easy to model programs that have millions of on-going things (very high level of concurrency), like MMOG games.
  • 5. Concurrency: Actor vs Thread ● Thread: n dimensions, hard to reason about. ● Actor: 1D, one thing at a time. var1 var2
  • 6. Concurrency: Actor vs Thread ● Actor is a high level logical way to think, to model programs. ● At lower level, actors run above a thread pool.
  • 7. Akka http://akka.io/ (an implementation of actor model)
  • 8. Akka: Demo ● Live coding demo
  • 9. Demo: Counter // First, define messages object Counter { case class Inc(amount: Int) case class Dec(amount: Int) }
  • 10. import akka.actor.Actor // Define actor state and msg handler class Counter(name: String) extends Actor { // Actor state private var value = 0 override def preStart() { println(s"I'm alive: $name") } override def postStop() { println(s"I'm dead: $name") } ...
  • 11. ... import Counter._ // Msg handler def receive = { case Inc(amount) => value += amount println(s"Value: $value") case Dec(amount) => value -= amount println(s"Value: $value") } }
  • 12. import akka.actor.{ActorSystem, Props} // Think of this as a thread pool val system = ActorSystem("mysystem") // Create actor reference (instance) val c1 = system.actorOf( Props(new Counter("mylock")) ) // Send messages to the actor c1 ! Counter.Inc(1) c1 ! Counter.Dec(2)
  • 13. Demo: Behavior transition CodeLock: https://github.com/ngocdaothanh/code-lock-fsm-akka
  • 14. Some actor antipatterns Send mutable msgs between actors. ← May lead to bug, if actor A sends msg M to actor B, state of B incorporates M, then M is later changed by A. Fix: Use immutable msgs.
  • 15. Some actor antipatterns From inside actor:outsideObject.foo(new Callback { def onCallback() { directlyModifyActorState() } }) ← May lead to bug, because the actor’s thread and the callback’s thread may be 2 different threads. Remember: An actor is like a single threaded process, can’t do more than one thing at a time. Fix: self ! msgFromCallback
  • 16. References ● Concurrent Programming for Scalable Web Architectureshttp://berb.github.io/diploma-thesis/ index.html ● Functions + Messages + Concurrency = Erlanghttp://www.infoq.com/presentations/joe-armstrong- erlang-qcon08 ● Akka dochttp://doc.akka.io/docs/akka/2.3.5/scala.html