Akka Building Distributed Systems for Concurrent, Fault-tolerant and Scalable Java Applications 1 Thursday, November 21, 13 1
Problems with Multithreaded Programming Shared Mutable Objects Thread 2 Thread 1 Solution: Synchronized Access Shared Objects (guarded by locks) 2 Thursday, November 21, 13 2
Problems with Multithreaded Programming Shared Mutable Objects Thread 2 Thread 1 Solution: Synchronized Access Shared Objects (guarded by locks) Change in one part of the system may break it in another part. 2 Thursday, November 21, 13 2
Problems with Multithreaded Programming Shared Mutable Objects Thread 2 Thread 1 Solution: Synchronized Access Shared Objects (guarded by locks) Change in one part of the system may break it in another part. 1. Operations may occur serially -> poor performance 2. Locks -- do not compose -> hard to design applications -- do not scale well -> block the execution -- hard to get in right order and error recovery is complicated 2 Thursday, November 21, 13 2
Problems with Multithreaded Programming Deadlocks Thread 1 Thread 2 Synchronized Shared block 1 Shared block 2 Thread 1 has obtained the lock to synchronized block 1 and Thread 2 has obtained the lock to synchronized block 2 3 Thursday, November 21, 13 3
Problems with Multithreaded Programming Scalability Managing multiple threads in a single JVM Challenging Scaling the application across multiple JVMs? 4 Thursday, November 21, 13 4
Problems with Multithreaded Programming Scalability Managing multiple threads in a single JVM Challenging Scaling the application across multiple JVMs? Shared state stored in database and Relying on database to manage the concurrent access 4 Thursday, November 21, 13 4
Akka: A Solution for Concurrency, Fault-tolerance and Scalability Open source toolkit and runtime that runs on JVM Written in Scala May use Scala or Java to call libraries and features High level abstraction for concurrency: Actors combined with software transaction memory (SMT) to implement atomic message-passing 5 Thursday, November 21, 13 5
Actors in Akka Actors (lightweight processes) Provide Support Asynchronous, non- Simple and high-level blocking, and high- abstraction for concurrency performance event-driven and parallelism programming Implement share nothing architecture and encapsulate state and behavior state change is local 6 Thursday, November 21, 13 6
“Let it Crash” Model for Fault-tolerance standard java application critically important state is guarded by try/catch blocks try { } catch (ExceptionType name) { } catch (ExceptionType name) { } 7 Thursday, November 21, 13 7
“Let it Crash” Model for Fault-tolerance standard java application Akka supervisor of actor C critically important state is guarded by try/catch blocks actor P actor C try { } catch (ExceptionType name) { } catch (ExceptionType name) { } supervisor hierarchy 7 Thursday, November 21, 13 7
“Let it Crash” Model for Fault-tolerance standard java application Akka supervisor of actor C critically important state is guarded by try/catch blocks actor P Notify actor C try { Crash } catch (ExceptionType name) { } catch (ExceptionType name) { } supervisor hierarchy 7 Thursday, November 21, 13 7
The Actor Model 8 Thursday, November 21, 13 8
The Actor Model Creates ActorSystem ActorRef Dispatch Publishes Message MessageDispatcher Runs Message Queue Mailbox Actor Retrieves Invokes Message 8 Thursday, November 21, 13 8
Some More Advantages -- can maintain a thread pool having MessageDispatcher limited number of threads. -- can be configured with one-to-one Load balancing mapping of threads to actors. 9 Thursday, November 21, 13 9
Some More Advantages -- can maintain a thread pool having MessageDispatcher limited number of threads. -- can be configured with one-to-one Load balancing mapping of threads to actors. Local ActorRef ActorRef Remote ActorRef Horizontal scalability 9 Thursday, November 21, 13 9
Actor Hierarchies ActorSystem.Actors system hierarchy user hierarchy root actor: Guardian top-level actors child actors Actor hierarchy 10 Thursday, November 21, 13 10
Example: Computing Prime Numbers Create a master actor Create a round-robin router to distribute work across multiple worker actors Communicate between worker actors and the master actor Communicate between the master actor and a listner 11 Thursday, November 21, 13 11
12 Thursday, November 21, 13 12
13 Thursday, November 21, 13 13
14 Thursday, November 21, 13 14
15 Thursday, November 21, 13 15
15 Thursday, November 21, 13 15
16 Thursday, November 21, 13 16
17 Thursday, November 21, 13 17
Exercise Find GCD for a given set of numbers by creating 2 worker_actors and dividing the load among them equally. Report the time for computation taken by each worker_actor. Use Akka release 2.2.1 18 Thursday, November 21, 13 18
19 Thursday, November 21, 13 19
Recommend
More recommend