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?
Disclai
mer
Local actor
Local actor
Actor on JVM 1
Local actor
Coordinator Actor
Hello conferenc
e
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://
[email protected]: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 conferenc
e
Remote actor
Coordinator actor
Worker actor
WorkerMessage
Greetings from the coordinator:
Hello Conference
Remote actor
Coordinator actor
Worker actor
WorkerResponseMessage
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://[email protected]:2551", "akka.tcp://[email protected]:2552" ] }}
Cluster Worker Node
Port 2551
Worker Node
Port 2552
Member Up with IP: 127.0.0.1 and port: 2551Member Up with IP: 127.0.0.1 and port: 2552
Member Up with IP: 127.0.0.1 and port: 2551Member 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
CoordinatorNode
Port 2550
Cluster Worker Node
Port 2551
Worker Node
Port 2552
CoordinatorNode
Port 2550
RegisterWorker
RegisterWorker
Cluster Worker Node
Port 2551
Worker Node
Port 2552
CoordinatorNode
Port 2550
Worker registered with IP: 127.0.0.1 and port: 2551Worker 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
CoordinatorActor
on JVM 1
EmptyString Actor on JVM 3
EmptyString Actor on JVM 4
EmptyString Actor on JVM 2""
Routing
CoordinatorActor
on JVM 1
EmptyString Actor on JVM 3
EmptyString Actor on JVM 4
EmptyString Actor on JVM 2""
HashMap[hostname, counter]
Routing
CoordinatorActor
on JVM 1
EmptyString Actor on JVM 3
EmptyString Actor on JVM 4
EmptyString Actor on JVM 2
""
Routing
CoordinatorActor
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 2Crash
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
oddShard on
JVM 3
evenShard on
JVM 20, 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
CobolJava
ActorCrash
Restart
Example without persistence
Actor
Example without persistence
Actor
Scala
Example without persistence
Scala
Actor
Persistence
PersistentActor
Cobol
Command
Journal
PersistenceJournal
CobolEvent
PersistentActor
PersistenceJournal
ACK
PersistentActor CobolEvent
Persistence
Cobol
Journal
CobolEvent
PersistentActor
Persistence
Cobol
Journal
CobolEvent
PersistentActor
JavaEven
t
Persistence
CobolJava
Journal
CobolEvent
PersistentActorCrash
Restart JavaEven
t
PersistenceJournal
CobolEvent
PersistentActor
JavaEven
t
Persistence
CobolJava
Journal
CobolEvent
PersistentActor
JavaEven
t
Scala
Persistence
CobolJavaScala
Journal
CobolEvent
PersistentActor
JavaEven
t
Snapshots
JournalPersistentActorSnapshot
store
State until
Event C++
C++Cobol
Snapshots
JournalPersistentActorSnapshot
store State until
Event C++ C++
Cobol
Snapshots
JournalPersistentActorSnapshot
storeState until
Event C++ C++
Cobol
Snapshots
JournalPersistentActorSnapshot
storeState until
Event C++ Java
C++Cobol
Snapshots
JournalPersistentActorSnapshot
storeState until
Event C++
Crash
Restart
C++Cobol
Java
Snapshots
JournalPersistentActorSnapshot
storeState until
Event C++ C++
Cobol
Java
Snapshots
CobolC++
JournalPersistentActorSnapshot
storeState until
Event C++ C++
Cobol
Java
Snapshots
CobolC++Java
JournalPersistentActorSnapshot
storeState until
Event C++ C++
Cobol
Java
Snapshots
CobolC++JavaScala
JournalPersistentActorSnapshot
storeState 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)
InprogressProject
CrappyProject
NewProject
Progress, iterationProgress, iteration
Progress, iteration
NoProgressProgress, 2
Finite State Machines (FSM)
InprogressProject
CrappyProject
NewProject
Work harderIteration: 0
Finite State Machines (FSM)
InprogressProject
CrappyProject
NewProject
Good job!
Iteration: 0
Finite State Machines (FSM)
InprogressProject
CrappyProject
NewProject
Wrong direction
Iteration: 0
Finite State Machines (FSM)
InprogressProject
CrappyProject
NewProject
Use Akka
Iteration: 0
Finite State Machines (FSM)
InprogressProject
CrappyProject
NewProject
Good job!
Iteration: 1
Finite State Machines (FSM)
InprogressProject
CrappyProject
NewProject
Wrong direction
Iteration: 1
Finite State Machines (FSM)
InprogressProject
CrappyProject
NewProject
Use Akka
Iteration: 1
Finite State Machines (FSM)
InprogressProject
CrappyProject
NewProject 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 @johanjanssen42GitHub: https://github.com/johanjanssen/Akka-examples