Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Scalable Applications with the Actor ...

Building Scalable Applications with the Actor Model

Greg Shackles

October 13, 2017
Tweet

More Decks by Greg Shackles

Other Decks in Technology

Transcript

  1. Amdahl’s law The theoretical speedup is always limited by the

    part of the task that cannot benefit from the improvement.
  2. What can an actor do? ! Send messages ! Create

    other actors ! Change behavior
  3. Messages ! Invoke actor behavior by sending it a message

    ! Messages are ◦ Immutable ◦ Processed one at a time, in order
  4. Behavior ! Switch how an actor will handle its next

    message ◦ Referred to as becoming something ! User actor in a chat system can become: ◦ Authenticating ◦ Authenticated ◦ Unauthenticated ◦ ...and so on
  5. ! Actors are only referenced by address ◦ Think: phone

    numbers, email addresses ! Code doesn’t know or care where the actor is ! Scaling from one node to thousands becomes configuration Location Transparency
  6. C#: Create a message public class Greet
 {
 public string

    Who { get; } public Greet(string who)
 {
 Who = who;
 }
 }
  7. C#: Create an actor public class GreetingActor : ReceiveActor
 {


    public GreetingActor()
 {
 Receive<Greet>(greet =>
 Console.WriteLine($"Hello {greet.Who}");
 }
 }
  8. F#: Create an actor let handleMessage (mailbox: Actor<'a>) msg =

    match msg with | Greet who -> printf "Hello %s" who | _ -> ()
  9. let system = ActorSystem.Create("my-system") let greeter = spawn system "greeter"

    (actorOf2 handleMessage) greeter <! Greet "World" F#: Send a message
  10. Supervision ! An actor can respond to subordinate actor failures

    with a directive to: ◦ Resume the subordinate ◦ Restart the subordinate ◦ Stop the subordinate ◦ Escalate to its own supervisor ! Actors are cheap, let them crash
  11. Example Supervision Strategy protected override SupervisorStrategy SupervisorStrategy() => new OneForOneStrategy(

    maxNrOfRetries: 10, withinTimeRange: TimeSpan.FromSeconds(30), decider: Decider.From(ex => { if (ex is ArithmeticException) return Directive.Resume; else if (ex is NotSupportedException) return Directive.Stop; return Directive.Restart; }));
  12. ! Categories of messaging patterns: ◦ At most once ◦

    At least once ◦ Exactly once ! Default is at-most-once delivery ◦ i.e. no guaranteed delivery Message Delivery Reliability
  13. ! Special type of actor that routes messages to its

    routees ! Built-in routing strategies ◦ Round robin ◦ Broadcast ◦ Random ◦ Consistent hashing ◦ Tail chopping ◦ Scatter-gather first-completed ◦ Smallest mailbox Routers
  14. Configuration
 
 Remoting actor { provider = "Akka.Remote.RemoteActorRefProvider, Akka.Remote" deployment

    { /remoteecho { remote = "akka.tcp://DeployTarget@localhost:8090" } } } remote { helios.tcp { port = 0 hostname = localhost } }
  15. Configuration
 
 Clustering akka { actor.provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster" remote

    { helios.tcp { port = 8081 hostname = localhost } } cluster { seed-nodes = ["akka.tcp://ClusterSystem@localhost:8081"] roles = ["crawlerV1", "crawlerV2"] role.["crawlerV1"].min-nr-of-members = 3 } }