Last week, I spoke at the Trivadis Late Lounge in Glattbrugg about Akka.NET.
18.02.2016 Trivadis Late Lounge – Skalierbar, parallel, verteilt und fehlertolerant? Gibt’s nicht! – Doch, gibt’s, mit Akka.NET
Akka.NET is a framework that implements the Actor Model for the .NET CLR. The Actor Model is a concept that can be applied to software. It originated in the seventies at the MIT and has influenced a lot of programming languages since then. The first implementations of the actor model can be found in the Erlang language that was used to power telecommunication backbones with massive throughput.
Even today, there are a lot of companies that apply the actor model in their products using Erlang, Akka or other implementations:
Akka.NET is a port from Akka, which is an Actor Model implementation for the JVM. The core features of Akka.NET can be found in the slide below:
The main benefit of using Akka ist that the complexity related to threading and thread synchronization, which is inherently required for scalable and concurrent applications, is built into the product on a conceptual level.
In classic applications, different junks of code are executed on the same thread. You could say that the thread “travels” through the code:
In the Actor Model, on the other hand, the chunks of code are encapsulated inside actors. The actors communicate only via asynchronous messages and do not share any state. Every actor “has its own thread”, so to speak. This prevents a massive load of problems that show up as soon as different threads try to use the same resources.
A single actor consists of two message queues, one for system messages and one for messages from other actors. It has a dispatcher that dispatches the messages from the queue to the code that processes them. An actor by definition only processes one message at a time. Scaling actors can be achieved by replacing a single actor with an entire actor pool.
I tried to summarize the many principles related to Akka in 4 points:
Everything is an Actor / Shared Nothing / Lightweight Actors
Actors are relatively easy to write and instantiate and they are very lightweight. According to the framework guys, you can host up to 3-4 million actors in one GB of system memory.
DIstributed by default / Divide and Conquer
The idea behind the actor model is to take a large amount of work and divide into little parts that can be distributed between a large pool of actors. This leads to a runtime architecture that can be distributed between threads, processes or even machines.
Actors depend on each other in a hierarchical fashion with parent-child relationships:
Fault Tolerance / Supervision / Error-Kernel Pattern
The hierarchical runtime structure allows a developer to move critical code to the outer leaves of the application. This minimizes impact on the entire application when problems arise. This concept is often referred to as the Error-Kernel Pattern.
Akka supports resiliency out-of-the box. Every parent is responsible for its children. If the child has a problem, the parent gets notified and must issue a directive to the child on how it should continue. Possibilities are: Restart, Ignore, Stop, Escalate.
Loose Coupling, Location Transparency, Dynamics
Actors can be referenced via an Interface of type IActorReference that is stored in a variable or via the Actor path:
In addition, all the configuration can be moved out of the code into a configuration file using HOCON (Human-Optimized Config Object Notation):
Summary
Akka.NET is an enormously powerful framework. It supports the most challenging concepts in todays software out of the box: Concurrency, Scale, Resiliency.
Programming Akka has a learning curve. However, once you understand the concepts, implementing actors is regular C# stuff and much easier than dealing with classic multithreading using System.Threads!
With a couple of lines of code, I managed to remarkably speed up my demo data processing application without even importing the System.Thread namespace.
Resources:
You can get Akka.NET at:
For learning, I recommend the Petabridge Akka.NET Bootcamp:
https://petabridge.com/bootcamp/
or, of course, Pluralsight:
https://www.pluralsight.com/search?q=akka.net&categories=course
Another interesting post for real-life projects is “6 months of akka.NET” by Stephen Kennedy:
http://www.devenable.com/six-months-of-akka-net/
Try it out today!
PS: I like to thank Christoph Pletz for reusing parts of his slides.
Slides: