A Tour of (Advanced)
Akka Features in 60
Minutes [CON1706]
Johan Janssen, Info Support
@johanjanssen42
Content
▪ Why Akka?
▪ Local actor
▪ Remote actor
▪ Scheduling
▪ Cluster
▪ Routing
▪ Cluster singleton
▪ Sharding
▪ Persistence
▪ Akka HTTP
▪ Finite State Machines
▪ Conclusion
▪ Questions
Why Akka?
Why Akka?
▪ Concurrent
▪ Scalable
▪ Fault tolerant
▪ More natural programming experience when connecting to other
systems
▪ Easy to use?
Local actor
Local actor
Actor on
JVM 1
Local actor
Coordinator Actor
Hello
conference
Local actor
Coordinator Actor
println("Hello conference")
class Worker extends Actor {
def receive = {
case x =>
println(x)
}
}
val system = ActorSystem("ExampleActorSystem")
val workerActorRef = system.actorOf(Props[Worker])
workerActorRef ! "Hello conference"
Scala
Remote actor
Remote actor
Actor on
JVM 1
Actor on
JVM 2
val workerActorRef = system.actorOf(Props[Worker])
workerActorRef ! "Hello conference"
val workerActorRef =
context.actorSelection("akka.tcp://
ExampleActorSystem@127.0.0.1:9005
/user/workerActor")
workerActorRef ! "Hello conference"
akka {
actor {
provider =
"akka.remote.RemoteActorRefProvider"
}
remote {
enabled-transports =
["akka.remote.netty.tcp"]
netty.tcp {
hostname = "127.0.0.1"
port = 9002
Remote actor
Coordinator
actor
Worker
actor
StartMessage
Hello
conference
Remote actor
Coordinator
actor
Worker
actor
WorkerMessage
Greetings from the
coordinator:
Hello Conference
Remote actor
Coordinator
actor
Worker
actor
WorkerResponse
Message
Item processed
successfully
Scheduling
Scheduling
Actor
Scheduled once after 1 second
Tick
Scheduled every 5 seconds
Tock
system.scheduler.scheduleOnce(1 seconds, scheduleReceiveActor, Tick)
system.scheduler.schedule(0 seconds, 5 seconds, scheduleReceiveActor, Tock)
Scheduling
▪ Does not work for fixed point in time like 17:00
– Use Quartz
Cluster
Cluster
ActorSystem
on JVM 1
ActorSystem
on JVM 3
ActorSystem
on JVM 2
ActorSystem
on JVM 4
Seed nodes
▪ Contact points for automatically joining a cluster
akka {
cluster {
seed-nodes = [
"akka.tcp://ClusterNode@127.0.0.1:2551",
"akka.tcp://ClusterNode@127.0.0.1:2552"
]
}
}
Cluster Worker
Node
Port 2551
Association failed with [akka.tcp://ClusterSystem@127.0.0.1:2552
Cluster Worker
Node
Port 2551
Worker
Node
Port 2552
Member Up with IP: 127.0.0.1 and port: 2551
Member Up with IP: 127.0.0.1 and port: 2552
Member Up with IP: 127.0.0.1 and port: 2551
Member Up with IP: 127.0.0.1 and port: 2552
Cluster Worker
Node
Port 2551
Worker
Node
Port 2552
Member Up with IP: 127.0.0.1 and port: 2550
Member Up with IP: 127.0.0.1 and port: 2550
Coordinator
Node
Port 2550
Cluster Worker
Node
Port 2551
Worker
Node
Port 2552
Coordinator
Node
Port 2550
RegisterWorker
RegisterWorker
Cluster Worker
Node
Port 2551
Worker
Node
Port 2552
Coordinator
Node
Port 2550
Worker registered with IP: 127.0.0.1 and port: 2551
Worker registered with IP: 127.0.0.1 and port: 2552
Routing
Routing
Actor on
JVM 1
Actor on
JVM 3
Actor on
JVM 4
Actor on
JVM 2
Loadbalancer
akka {
actor {
provider = "akka.cluster.ClusterActorRefProvider"
deployment {
/coordinator/router {
router = round-robin-pool
nr-of-instances = 10
routees.paths = ["/user/emptystringactor"]
cluster {
enabled = on
allow-local-routees = off
}
}
}
}
}
Routing
Coordinator
Actor on
JVM 1
EmptyString
Actor on
JVM 3
EmptyString
Actor on
JVM 4
EmptyString
Actor on
JVM 2""
Routing
Coordinator
Actor on
JVM 1
EmptyString
Actor on
JVM 3
EmptyString
Actor on
JVM 4
EmptyString
Actor on
JVM 2""
HashMap[hostname, counter]
Routing
Coordinator
Actor on
JVM 1
EmptyString
Actor on
JVM 3
EmptyString
Actor on
JVM 4
EmptyString
Actor on
JVM 2
""
Routing
Coordinator
Actor on
JVM 1
EmptyString
Actor on
JVM 3
EmptyString
Actor on
JVM 4
EmptyString
Actor on
JVM 2
""
Cluster singleton
Cluster singleton
▪ Only one instance of the actor in the cluster
▪ (Re)created on the oldest node
▪ Can be used for instance for scheduling/caching
Cluster singleton
Actor on
JVM 1
Other
actors on
JVM 3
Other
actors on
JVM 4
Singleton
actor and
other actors
on JVM 2
Cluster singleton
Coordinator
EmptyString
Actor on
JVM 3
EmptyString
Actor on
JVM 4
EmptyString
Actor on
JVM 2""
Cluster singleton
Coordinator
EmptyString
Actor on
JVM 3
EmptyString
Actor on
JVM 4
EmptyString
Actor on
JVM 2""
HashMap[hostname, counter]
Cluster singleton
Coordinator
EmptyString
Actor on
JVM 3
EmptyString
Actor on
JVM 4
EmptyString
Actor on
JVM 2""
Cluster singleton
Coordinator
EmptyString
Actor on
JVM 3
EmptyString
Actor on
JVM 4
EmptyString
Actor on
JVM 2""
HashMap[hostname, counter]
Cluster singleton
Coordinator
EmptyString
Actor on
JVM 3
EmptyString
Actor on
JVM 4
EmptyString
Actor on
JVM 2
Crash
Cluster singleton
Coordinator
EmptyString
Actor on
JVM 3
EmptyString
Actor on
JVM 4
""
Cluster singleton
Coordinator
EmptyString
Actor on
JVM 3
EmptyString
Actor on
JVM 4
""
HashMap[hostname, counter]
Cluster singleton
Coordinator
EmptyString
Actor on
JVM 3
EmptyString
Actor on
JVM 4
""
Cluster singleton
Coordinator
EmptyString
Actor on
JVM 3
EmptyString
Actor on
JVM 4
""
HashMap[hostname, counter]
Sharding
Sharding
▪ Dividing a set of actors over a cluster
▪ Actors will be divided into groups called shards
▪ It will divide based on a logical identifier
Sharding
Actor on
JVM 1
odd
Shard on
JVM 3
even
Shard on
JVM 2
0, 2, 4, 6, 8
1, 3, 5, 7, 9
Persistence
Persistence
▪ Store actor information
▪ Recover after crash
▪ Possibility to take snapshots
Example without persistence
Actor
Cobol
Example without persistence
Actor
Cobol
Example without persistence
Cobol
Actor
Example without persistence
Actor
Java Cobol
Example without persistence
Cobol
Java
Actor
Crash
Restart
Example without persistence
Actor
Example without persistence
Actor
Scala
Example without persistence
Scala
Actor
Persistence
PersistentActor
Cobol
Command
Journal
Persistence
Journal
Cobol
Event
PersistentActor
Persistence
Journal
ACK
PersistentActor
Cobol
Event
Persistence
Cobol
Journal
Cobol
Event
PersistentActor
Persistence
Cobol
Journal
Cobol
Event
PersistentActor
Java
Event
Persistence
Cobol
Java
Journal
Cobol
Event
PersistentActor
Crash
Restart Java
Event
Persistence
Journal
Cobol
Event
PersistentActor
Java
Event
Persistence
Cobol
Java
Journal
Cobol
Event
PersistentActor
Java
Event
Scala
Persistence
Cobol
Java
Scala
Journal
Cobol
Event
PersistentActor
Java
Event
Snapshots
Journal
PersistentActor
Snapshot store
State until
Event C++
C++
Cobol
Snapshots
Journal
PersistentActor
Snapshot store
State until
Event C++
C++
Cobol
Snapshots
Journal
PersistentActor
Snapshot store
State until
Event C++ C++
Cobol
Snapshots
Journal
PersistentActor
Snapshot store
State until
Event C++
Java
C++
Cobol
Snapshots
Journal
PersistentActor
Snapshot store
State until
Event C++
Crash
Restart
C++
Cobol
Java
Snapshots
Journal
PersistentActor
Snapshot store
State until
Event C++ C++
Cobol
Java
Snapshots
Cobol
C++
Journal
PersistentActor
Snapshot store
State until
Event C++ C++
Cobol
Java
Snapshots
Cobol
C++
Java
Journal
PersistentActor
Snapshot store
State until
Event C++ C++
Cobol
Java
Snapshots
Cobol
C++
Java
Scala
Journal
PersistentActor
Snapshot store
State until
Event C++ C++
Cobol
Java
Akka HTTP
Akka HTTP
Actor on
JVM 1
Actor on
JVM 2
Finite State Machine
Finite State Machine
▪ State
▪ Event
Finite State Machines (FSM)
Inprogress
Project
Crappy
Project
New
Project
Progress, iteration
Progress, iteration
Progress, iteration
NoProgress
Progress, 2
Finite State Machines (FSM)
Inprogress
Project
Crappy
Project
New
Project
Work harder
Iteration: 0
Finite State Machines (FSM)
Inprogress
Project
Crappy
Project
New
Project
Good job!
Iteration: 0
Finite State Machines (FSM)
Inprogress
Project
Crappy
Project
New
Project
Wrong direction
Iteration: 0
Finite State Machines (FSM)
Inprogress
Project
Crappy
Project
New
Project
Use Akka
Iteration: 0
Finite State Machines (FSM)
Inprogress
Project
Crappy
Project
New
Project
Good job!
Iteration: 1
Finite State Machines (FSM)
Inprogress
Project
Crappy
Project
New
Project
Wrong direction
Iteration: 1
Finite State Machines (FSM)
Inprogress
Project
Crappy
Project
New
Project
Use Akka
Iteration: 1
Finite State Machines (FSM)
Inprogress
Project
Crappy
Project
New
Project Get another job!
Iteration: 2
Conclusion
Conclusion
▪ Akka can be used with Scala or Java
▪ There is even a .NET version of Akka
▪ Akka is really powerful
▪ Akka is quite easy to use
▪ Some features are still experimental
Questions
Johan Janssen @johanjanssen42
GitHub: https://github.com/johanjanssen/Akka-examples

JavaOne: A tour of (advanced) akka features in 60 minutes [con1706]