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

Scale up and out with Akka.NET

Scale up and out with Akka.NET

Roger Johansson

June 09, 2016
Tweet

More Decks by Roger Johansson

Other Decks in Programming

Transcript

  1. Scale up and out with Akka.NET Roger Johansson akka.net Akka.NET

    Founder Twitter: @rogeralsing Github: rogeralsing Mail: [email protected] Github.com/rogeralsing/presentations
  2. Actor Model Three axioms: Send – an actor can send

    messages to other actors Create – an actor can create new actors Become – an actor can decide how to handle it’s next message ”An island of consistency in a sea of concurrency” ”Shared nothing”, ”Black box” ”Location transparent”, ”Distributable by design”
  3. OOP Actor Model Behavior Stateful Synchronous method calls Behavior Stateful

    Asynchronous message passing OOP vs. Actor Model Why Actors?? The synchronous model has worked nicely for 60 years!?
  4. Moore’s Law We can no longer build faster processors Instead,

    we stack them next to eachother and call them ”cores” 200 300 400 500 1000 1800 2530 3200 3600 2200 2930 3000 3200 3330 3330 3150 3200 3150 3150 3150 1 1 1 1 1 1 1 1 1 2 2 4 4 8 8 16 16 32 32 64 0 10 20 30 40 50 60 70 0 500 1000 1500 2000 2500 3000 3500 4000 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 MHZ and Cores per year Mhz Cores
  5. CPU

  6. Classic .NET technologies Elasticity: Scale out: WCF Web API MSMQ

    Scale up: Parallel Linq TPL – async await Threads OMG-WTF-Difficult
  7. Scale up and out are essentially the same That is;

    we want to execute code ”somewhere”, on a core, on a machine, in a cluster. Why should we have to resort to different technologies to accomplish the same thing?
  8. Scale up and out are essentially the same That is;

    we want to execute code ”somewhere”, on a core, on a machine, in a cluster. Why should we have to resort to different technologies to accomplish the same thing?
  9. Event-driven thread ActorRef ActorRef Actor State Supervision Children Mailbox Behavior

    Transport Location Transparency, Distributed by default Shared Nothing, Black box An island of consistency in a sea of concurrency
  10. Thread Pool Multiplex Scheduling Actor1 Actor1 Actor2 Actor2 Actor3 Actor4

    Actor4 Actor1 Cheaper than threads, no context switching Actor3 Actor4 Actor2 Actor1 Actor3 Actor2 Time
  11. Actor Model Products basered on the actor model: Facebook WhatsApp

    (Erlang) RabbitMQ (Erlang) CouchDB (Erlang) LinkedIn.com (JVM Akka) Walmart.com (JVM Akka) Azure Service Fabric alternative backend (Akka.NET)
  12. A router delegates messages to other ”routee” actors Group routers

    – delegate messages to your existing actors Pool routers – delegate messages to a dedicated pool of actors Routers
  13. BroadcastRouter 1 2 1 2 1 2 1 2 Router

    Routee2 Routee3 Routee1 Notifies all ”routees”
  14. RoundRobinRouter 1 2 1 2 3 3 4 4 Router

    Routee1 Routee2 Routee3 Scale up! .. Or down!
  15. RoundRobinRouter Router Routee1 Routee2 Routee3 Routee1 Routee2 Routee3 Routee1 Routee2

    Routee3 Scale up! Scale out! Remote1 Remote2 Remote3 Router Router Router
  16. ConsistentHashRouter M Y M Y X A X A Router

    Routee1 Routee2 Routee3 Affinity between hash index and routee
  17. ConsistentHashRouter Id: 123 Create … Id: 123 Stop … Id:456

    Create … Id: 789 Payment … Routee1 Routee2 Routee3 Prevents race conditions Router Id: 789 Payment … Id: 123 Stop … Id:456 Create … Id: 123 Create …
  18. Routee1 Routee2 Routee3 ? ? ? ? Sender ! Similar

    to broadcast, forwards the first reply to the original sender ScatterGatherFirstCompletedRouter Router
  19. Supervision Every actor is being supervised by another actor that

    dictates how failures should be handled A supervisor can decide to: • Force a restart • Terminate • Ignore and resume • Escalate up to it’s own supervisor
  20. /user /system / Error Kernel User actor Hierarchy System actor

    Hierarchy ”System Guardian” ”Guardian” ”Root Guardian”
  21. /c2 /b2 /b1 /a1 /a2 /b3 /c3 /c4 /c5 /user

    /system / Error Kernel /c1 OneForOne supervisor AllForOne supervisor /b1 /b3
  22. Become Become or hotswap means that an actor can decide

    how it should process the next incoming message. In Akka.NET this is handled by the .Become and .Unbecome methods. Very useful for creating state machines
  23. Private void Alive(){ …//other code if (this.hitpoints <= 0) this.Become(Dead);

    … } private void Dead(){ Receive<Resurrect>(_ => { this.hitpoints = this.maxHitpoints; this.Become(Alive); }); }
  24. Other features • Persistence • Cluster • Cluster Sharding •

    Dependency Injection • Finite State Machines • F# API • Reactive Streams
  25. Before Akka.NET 200-300 simulated Excel workbooks. Aprox. 7000 Excel celles.

    Excel compatible formulas. Single user per worksheet.
  26. With Akka.NET 800 000 Excel cells. Multiuser, multiple users editing

    worksheets – google docs style. Two devs rewrote the entire system in two weeks.
  27. Before Akka.NET Legacy system, written around 2000. One customer per

    server, SPOF. Huge amount of synchronization/locking code.
  28. With Akka.NET 85% smaller codebase, code focuses on business logic,

    Akka.NET handles infrastructure. High Availability - Failover with Akka.Cluster.
  29. FAQ Akka/NET vs. Project Orleans http://bit.ly/25KAuzC Akka/NET vs. Message Queues,

    e.g. NServiceBus http://stackoverflow.com/a/13018198 OT, but how did you get the strange arrows and symbols in the code? https://github.com/tonsky/FiraCode
  30. Scale up and out with Akka.NET Roger Johansson akka.net Akka.NET

    Co-Founder Twitter: @rogeralsing Github: rogeralsing Mail: [email protected] Github.com/rogeralsing/presentations