![Page 1: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/1.jpg)
Introduction to Akka
Mirko Adari
![Page 2: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/2.jpg)
• Akka is a toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven applications on the JVM.
![Page 3: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/3.jpg)
Why Akka?
• Complexity
– Concurrency
– Scalability
– Fault-tolerance
![Page 4: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/4.jpg)
CONCURRENCY
![Page 5: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/5.jpg)
Status quo
• Threads
• Shared state
• State visibility
• Locks
• Concurrent collections
• Thread notifications
• …
![Page 6: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/6.jpg)
What is an actor?
• By Carl Hewitt:
– Unit of computation
• Processing
• Storage
• Communication
![Page 7: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/7.jpg)
What can actors be used for?
• a thread
• an object instance or component
• a callback or listener
• a singleton or service
• a router load-balancer or pool
• a Finite State Machine
![Page 8: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/8.jpg)
Core operations
• Define
• Create
• Send
• Become
![Page 9: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/9.jpg)
Define
class HelloActor extends Actor {
val log = Logging(context.system, this)
def receive = {
case “hello” = log.info(“received hello”)
case _ = log.info(“received unknown message”)
}
}
![Page 10: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/10.jpg)
Create
class HelloActor extends Actor {
val log = Logging(context.system, this)
def receive = {
val byeActor = context.actorOf(Props[ByeActor],
name = “byeActor”)
}
}
![Page 11: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/11.jpg)
Send
class HelloActor extends Actor {
val log = Logging(context.system, this)
def receive = {
case “hello” = sender ! “hello back”
case _ = log.info(“received unknown message”)
}
}
![Page 12: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/12.jpg)
Become
class HelloActor extends Actor { val log = Logging(context.system, this) def happy: Receive = { case “hello” = sender ! “I feel great.” case “bye” = become(sad) } def sad: Receive = { case “hello” = become(happy) case “sad” = sender ! “I’m thinking of suicide.” } def receive = { case “hello” = become(happy) case “bye” = become(sad) } }
![Page 13: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/13.jpg)
Become
• Transform into a pool or a router
• Finite State Machines
• Adaptive worker processes
![Page 14: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/14.jpg)
Key concepts
• Parallel
• Asynchronous
• Non-blocking
• Immutable
• Mailboxes
![Page 15: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/15.jpg)
• What happens if actor sends a message to itself?
![Page 16: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/16.jpg)
Futures
class HelloActor extends Actor {
def receive = {
val future = timeActor ? “time”
future onSuccess {
case time = sender ! time
}
}
}
![Page 17: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/17.jpg)
SCALABILITY
![Page 18: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/18.jpg)
Practices
• Stateless
• Datastore
• Replication
• SOA
![Page 19: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/19.jpg)
Small footprint
• 2.7M actors per GB memory
![Page 20: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/20.jpg)
Routers
akka.actor.deployment {
/hello-pool {
router = round-robin
nr-of-instances = 5
}
}
![Page 21: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/21.jpg)
Remoting
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
transport = "akka.remote.netty.NettyRemoteTransport"
netty {
hostname = "127.0.0.1"
port = 2552
}
}
}
![Page 22: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/22.jpg)
FAULT-TOLERANCE
![Page 23: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/23.jpg)
Inspiration
• Erlang
– Designed by Ericsson in 1986
– Telecommunications
– Modern users
• Amazon
• Yahoo
• T-Mobile
![Page 24: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/24.jpg)
Supervision
• Every actor has a supervisor
![Page 25: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/25.jpg)
• Who supervises system?
![Page 26: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/26.jpg)
Supervision
• Every actor has a supervision strategy
– Escalate
– Stop
– Restart
– Resume
![Page 27: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/27.jpg)
Default strategy
• ActorInitializationException => Stop
• ActorKilledException => Stop
• Exception => Restart
• Otherwise => Escalate
![Page 28: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/28.jpg)
Custom strategy
class Supervisor extends Actor {
override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10 withinTimeRange = 1 minute) {
case _: ArithmeticException ⇒ Resume
case _: NullPointerException ⇒ Restart
case _: IllegalArgumentException ⇒ Stop
case _: Exception ⇒ Escalate
}
![Page 29: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/29.jpg)
Example
![Page 30: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/30.jpg)
Example
![Page 31: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and](https://reader036.vdocuments.mx/reader036/viewer/2022081421/5ec9240240d5f7781a79024a/html5/thumbnails/31.jpg)
Q&A