Scaling software with akka

  • Published on
    06-May-2015

  • View
    2.470

  • Download
    1

Embed Size (px)

DESCRIPTION

Scala Conference in Japan 2013

Transcript

  • 1.Scaling software withJonas BonrCTO Typesafe Twitter: @jbonerAkka:

2. Scaling Scaling software withsoftware with 3. ScalingScalingCopyright Ingeborg van Leeuwensoftware with software with 4. Selection of Akka Production Users 5. Manage System Overload 6. Automatic Replication & Distribution for Fault-tolerance & Scalability 7. Program at a Higher Level 8. Program at a Higher Level 9. Program at a Higher Level Never think in terms of shared state, statevisibility, threads, locks, concurrent collections,thread notications etc. 10. Program at a Higher Level Never think in terms of shared state, statevisibility, threads, locks, concurrent collections,thread notications etc. Low level concurrency plumbing BECOMESSIMPLE WORKFLOW - you only think about howmessages ow in the system 11. Program at a Higher Level Never think in terms of shared state, statevisibility, threads, locks, concurrent collections,thread notications etc. Low level concurrency plumbing BECOMESSIMPLE WORKFLOW - you only think about howmessages ow in the system You get high CPU utilization, low latency, highthroughput and scalability - FOR FREE as part ofthe model 12. Program at a Higher Level Never think in terms of shared state, statevisibility, threads, locks, concurrent collections,thread notications etc. Low level concurrency plumbing BECOMESSIMPLE WORKFLOW - you only think about howmessages ow in the system You get high CPU utilization, low latency, highthroughput and scalability - FOR FREE as part ofthe model Proven and superior model for detecting andrecovering from errors 13. Distributable by Design 14. Distributable by Design 15. Distributable by Design Actors are location transparent & distributable by design 16. Distributable by Design Actors are location transparent & distributable by design Scale UP and OUT for free as part of the model 17. Distributable by Design Actors are location transparent & distributable by design Scale UP and OUT for free as part of the model You get the PERFECT FABRIC for the CLOUD 18. Distributable by Design Actors are location transparent & distributable by design Scale UP and OUT for free as part of the model You get the PERFECT FABRIC for the CLOUD - elastic & dynamic 19. Distributable by Design Actors are location transparent & distributable by design Scale UP and OUT for free as part of the model You get the PERFECT FABRIC for the CLOUD - elastic & dynamic - fault-tolerant & self-healing 20. Distributable by Design Actors are location transparent & distributable by design Scale UP and OUT for free as part of the model You get the PERFECT FABRIC for the CLOUD - elastic & dynamic - fault-tolerant & self-healing - adaptive load-balancing, cluster rebalancing & actor migration 21. Distributable by Design Actors are location transparent & distributable by design Scale UP and OUT for free as part of the model You get the PERFECT FABRIC for the CLOUD - elastic & dynamic - fault-tolerant & self-healing - adaptive load-balancing, cluster rebalancing & actor migration - build extremely loosely coupled and dynamic systems that can change and adapt at runtime 22. How can we achieve this?? 23. How can we achieve this? 24. Lets use ActorsHow can we achieve this? 25. What is an Actor? 26. What is an Actor? Akkas unit of code organization is called an Actor 27. What is an Actor? Akkas unit of code organization is called an Actor Actors helps you create concurrent, scalable andfault-tolerant applications 28. What is an Actor? Akkas unit of code organization is called an Actor Actors helps you create concurrent, scalable andfault-tolerant applications Like Java EE servlets and session beans, Actors is amodel for organizing your code that keeps manypolicy decisions separate from the business logic 29. What is an Actor? Akkas unit of code organization is called an Actor Actors helps you create concurrent, scalable andfault-tolerant applications Like Java EE servlets and session beans, Actors is amodel for organizing your code that keeps manypolicy decisions separate from the business logic Actors may be new to many in the Java community,but they are a tried-and-true concept (Hewitt 1973)used for many years in telecom systems with 9 ninesuptime 30. What can I use Actors for? 31. What can I use Actors for?In different scenarios, an Actor may be analternative to: 32. What can I use Actors for?In different scenarios, an Actor may be analternative to: - a thread 33. What can I use Actors for?In different scenarios, an Actor may be analternative to: - a thread - an object instance or component 34. What can I use Actors for?In different scenarios, an Actor may be analternative to: - a thread - an object instance or component - a callback or listener 35. What can I use Actors for?In different scenarios, an Actor may be analternative to: - a thread - an object instance or component - a callback or listener - a singleton or service 36. What can I use Actors for?In different scenarios, an Actor may be analternative to: - a thread - an object instance or component - a callback or listener - a singleton or service - a router, load-balancer or pool 37. What can I use Actors for?In different scenarios, an Actor may be analternative to: - a thread - an object instance or component - a callback or listener - a singleton or service - a router, load-balancer or pool - a Java EE Session Bean or Message-Driven Bean 38. What can I use Actors for?In different scenarios, an Actor may be analternative to: - a thread - an object instance or component - a callback or listener - a singleton or service - a router, load-balancer or pool - a Java EE Session Bean or Message-Driven Bean - an out-of-process service 39. What can I use Actors for?In different scenarios, an Actor may be analternative to: - a thread - an object instance or component - a callback or listener - a singleton or service - a router, load-balancer or pool - a Java EE Session Bean or Message-Driven Bean - an out-of-process service - a Finite State Machine (FSM) 40. So, what is theActor Model?? 41. Carl Hewitts denition http://bit.ly/hewitt-on-actors 3: 42. Carl Hewitts denition- The fundamental unit of computation that embodies: http://bit.ly/hewitt-on-actors 3: 43. Carl Hewitts denition- The fundamental unit of computation that embodies:- Processinghttp://bit.ly/hewitt-on-actors 3: 44. Carl Hewitts denition- The fundamental unit of computation that embodies:- Processing- Storagehttp://bit.ly/hewitt-on-actors 3: 45. Carl Hewitts denition- The fundamental unit of computation that embodies:- Processing- Storage- Communicationhttp://bit.ly/hewitt-on-actors 3: 46. Carl Hewitts denition- The fundamental unit of computation that embodies:- Processing- Storage- Communication- 3 axioms - When an Actor receives a message it can:http://bit.ly/hewitt-on-actors 3: 47. Carl Hewitts denition- The fundamental unit of computation that embodies:- Processing- Storage- Communication- 3 axioms - When an Actor receives a message it can:- Create new Actorshttp://bit.ly/hewitt-on-actors 3: 48. Carl Hewitts denition- The fundamental unit of computation that embodies:- Processing- Storage- Communication- 3 axioms - When an Actor receives a message it can:- Create new Actors- Send messages to Actors it knowshttp://bit.ly/hewitt-on-actors 3: 49. Carl Hewitts denition- The fundamental unit of computation that embodies:- Processing- Storage- Communication- 3 axioms - When an Actor receives a message it can:- Create new Actors- Send messages to Actors it knows- Designate how it should handle the next message it receiveshttp://bit.ly/hewitt-on-actors 3: 50. 4 core Actor operations 0. DEFINE 1. CREATE 2. SEND 3. BECOME 4. SUPERVISE4 51. 0. DEFINE case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } 52. 0. DEFINE Dene the message(s) the Actorshould be able to respond to case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } 53. 0. DEFINE Dene the message(s) the Actorshould be able to respond to Dene the Actor class case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } 54. 0. DEFINE Dene the message(s) the Actorshould be able to respond toDene the Actor class case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } Dene the Actors behavior 55. 1. CREATE CREATE - creates a new instance of an Actor Extremely lightweight (2.7 Million per Gb RAM) Very strong encapsulation - encapsulates:- state- behavior- message queue State & behavior is indistinguishable from each other Only way to observe state is by sending an actor a message and see how it reacts 56. CREATE Actor case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter")ActorRef 57. CREATE Actor case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) Create an Actor system " + who) => log.info("Hello } } val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter")ActorRef 58. CREATE Actor case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) Create an Actor system " + who) => log.info("Hello } Actor conguration } val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter")ActorRef 59. CREATE Actor case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) Create an Actor system " + who) => log.info("Hello } Actor conguration } val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") Give it a nameActorRef 60. CREATE Actor case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) Create an Actor system " + who) => log.info("Hello } Actor conguration } val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") Create the ActorGive it a nameActorRef 61. CREATE Actor case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) Create an Actor system " + who) => log.info("Hello } Actor conguration } val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter")Create the ActorYou get an ActorRef back Give it a nameActorRef 62. Actors can form hierarchies Guardian System Actor 63. Actors can form hierarchies Guardian System Actor system.actorOf(Props[Foo], Foo) 64. Actors can form hierarchiesGuardian System ActorFoo system.actorOf(Props[Foo], Foo) 65. Actors can form hierarchiesGuardian System ActorFoocontext.actorOf(Props[A], A) 66. Actors can form hierarchiesGuardian System ActorFoo Acontext.actorOf(Props[A], A) 67. Actors can form hierarchiesGuardian System ActorFoo BarAA CE C BBD 68. Name resolution - like a le-system Guardian System Actor Foo Bar A A C E CB B D 69. Name resolution - like a le-system Guardian System Actor/Foo Foo Bar A A C E CB BD 70. Name resolution - like a le-system Guardian System Actor/Foo Foo Bar/Foo/A A A C E CB B D 71. Name resolution - like a le-system Guardian System Actor/Foo Foo Bar/Foo/A A A C /Foo/A/B E C B B D 72. Name resolution - like a le-system Guardian System Actor/Foo Foo Bar/Foo/A A AC /Foo/A/B E C B B D /Foo/A/D 73. 2. SEND 74. 2. SEND SEND - sends a message to an Actor 75. 2. SEND SEND - sends a message to an Actor Asynchronous and Non-blocking - Fire-forget 76. 2. SEND SEND - sends a message to an Actor Asynchronous and Non-blocking - Fire-forget EVERYTHING is asynchronous and lockless 77. 2. SEND SEND - sends a message to an Actor Asynchronous and Non-blocking - Fire-forget EVERYTHING is asynchronous and lockless Everything happens Reactively 78. 2. SEND SEND - sends a message to an Actor Asynchronous and Non-blocking - Fire-forget EVERYTHING is asynchronous and lockless Everything happens Reactively - An Actor is passive until a message is sent to it, which triggers something within the Actor 79. 2. SEND SEND - sends a message to an Actor Asynchronous and Non-blocking - Fire-forget EVERYTHING is asynchronous and lockless Everything happens Reactively - An Actor is passive until a message is sent to it, which triggers something within the Actor - Messages is the Kinetic Energy in an Actor system 80. 2. SEND SEND - sends a message to an Actor Asynchronous and Non-blocking - Fire-forget EVERYTHING is asynchronous and lockless Everything happens Reactively - An Actor is passive until a message is sent to it, which triggers something within the Actor - Messages is the Kinetic Energy in an Actor system - Actors can have lots of buffered Potential Energy but cant do anything with it until it is triggered by a message 81. SEND messagecase class Greeting(who: String)class GreetingActor extends Actor with ActorLogging {def receive = {case Greeting(who) => log.info("Hello " + who)}}val system = ActorSystem("MySystem")val greeter = system.actorOf(Props[GreetingActor], name = "greeter")greeter ! Greeting("Charlie Parker") 82. SEND messagecase class Greeting(who: String)class GreetingActor extends Actor with ActorLogging {def receive = {case Greeting(who) => log.info("Hello " + who)}}val system = ActorSystem("MySystem")val greeter = system.actorOf(Props[GreetingActor], name = "greeter")greeter ! Greeting("Charlie Parker")Send the message 83. Full examplecase class Greeting(who: String)class GreetingActor extends Actor with ActorLogging {def receive = {case Greeting(who) => log.info("Hello " + who)}}val system = ActorSystem("MySystem")val greeter = system.actorOf(Props[GreetingActor], name = "greeter")greeter ! Greeting("Charlie Parker") 84. Load Balancing 85. Routers 86. Routers val router =system.actorOf(Props[SomeActor].withRouter( RoundRobinRouter(nrOfInstances = 5))) 87. Router + Resizerval resizer =DefaultResizer(lowerBound = 2, upperBound = 15)val router =system.actorOf(Props[ExampleActor1].withRouter(RoundRobinRouter(resizer = Some(resizer)))) 88. or from congakka.actor.deployment {/path/to/actor {router = round-robinnr-of-instances = 5}}cong 89. or from cong akka.actor.deployment { /path/to/actor { router = round-robin resizer { lower-bound = 12 upper-bound = 15 } } } 90. 3. BECOME push/pop 91. 3. BECOME BECOME - dynamically redenes Actors behavior push/pop 92. 3. BECOME BECOME - dynamically redenes Actors behavior Triggered reactively by receive of m...