In today’s world, systems can be deployed over hundreds or thousands of servers. Each of these servers serve multiple calls and is expected to have millisecond response times. The framework Akka.NET is one of the frameworks that helps create applications and systems meeting today’s expectations. It makes it easier to build concurrent, distributed, robust and resilient applications.
In this article, to get a clear picture of the framework and how it works, we will study the models AKKA.NET is based on to better understand, in a second part, how the framework is structured.
I. The cor concepts of Akka.Net
Akka.Net is a set of open-source libraries or designing reactive and Actor Model-based systems.
1. What is an Actor Model?
The Actor model is a conceptual model to deal with concurrent computation. It defines some general rules for how the system’s components should behave and interact with each other. It was described in « A universal modular ACTOR formalism for artificial intelligence » by Carl Hewitt, Peter Bishop and Richard Steiger (1973). But relatively recently, it has been implemented: initial work on Akka dates from 2009 and first version of akka.net was released in 2015.
This computer science concept uses « actors » as the fundamental agents of computing. Actors are stateful entities communicating with each other by explicit message passing. They have these main characteristics:
- Communication with asynchronous messaging
- Management of their own state
- It can also create other actors
2. What is a reactive system?
Today’s specifications of systems define reactive systems. Those latter are defined in a document, first released in 2013, The Reactive Manifesto. It defines the core principles of reactive programming. There are 4 core principles:
- Responsiveness (rapid and consistent response times)
- Resilience (system responsive in front of any failure via replication, containment, isolation, and delegation)
- Elasticity (reacting to changes and staying responsive under any workload)
- Message driven system (establishing a boundary between components by relying on asynchronous message passing)
For more info about The Reactive Manifesto: https://www.reactivemanifesto.org/
II. Akka.Net’s skeleton
Akka.Net adheres to the principles set forth in The Reactive Manifesto and is Actor model based.
With the framework, we are working with high level of abstractions (actors, messages, etc…) and higher level of concurrency (no manual management of thread nor manual locking of resources). Working with an asynchronous message passing offers simple concurrency since actors are not blocked waiting for responses from other actors. Akka.Net provides a simple remote deployment model where configuration can specify where actors are to be created: it implements the concept of location transparency (c.f. A set of actors : an actor system).
Akka.Net is highly performant: it can handle around 50 million messages per second and one gigabyte of heap memory can store around 2.5 million actor instances (Actor Model benchmarks : https://github.com/Blind-Striker/actor-model-benchmarks)
Some advanced features Akka.NET offers are load balancing, routing requests to multiple child instances, the possibility to create self-healing systems (through the hierarchies of supervisors).
1. What is an actor in Akka.Net?
In Akkar.NET, every actor is an Actor Model’s actor. The framework takes encapsulation to its extreme. In fact, each actor encapsulates not only its states but also its behavior, i.e. the execution of a small well-defined task implemented in its core. The execution of actors’ code is identical whether they are local or distributed.
Every actor has:
- An incoming message mailbox: messages received are queued for processing
- A behavior: the actor performs a defined action according to the type of message received
- A state: the current state of the actor
- A set of children actors
- A possible supervisory strategy: handling faults in children
An actor processes one message at a time. The message is received, stored in the mailbox and processed when the actor is released or wakes up. The actor will continue processing till the mailbox is empty then it sleeps again.
Basically, in Akka.NET, messages are simple POCO (Plain Old CLR Object) classes: they don’t derive from any special base class. They should be immutable.
2. A set of actors: an actor system
An actor system is a set of actors. The actors within the same actor system communicate via local messages; however, actors in different actor systems communicate via remote messages. The actor systems could run on the same machine or on a distributed system. Akka.Net offers a simple communication system: making actors from different actor systems communicate doesn’t need a specific configuration. The framework implements the concept of location transparency; it is the ability to send a message to an actor without needing to know where it is, whether it is in the same actor system instance or in another one.
3. Self-healing systems: Akka.Net’s actor supervision hierarchies
An actor may have children. If it does, it is responsible for supervising its children. Creating children, it creates a certain hierarchy. Parent actors supervise child actors which can be themselves parent: a tree of actors is created. This hierarchy allows system to be fault tolerant and to be able to self-heal. Self-healing is a feature that tells that a problem has occurred and automatically find a solution without any external intervention. When an error occurs in a child, the parent is notified, and it is the one responsible to fix the problem via several strategies such as restarting, stopping or resuming the actor. When the parent actor doesn’t know what to do with its child in error it should notify its parent: it is the scalation of error strategy. Supervision hierarchy allows the system to not only deal with faults but become self-healing.
4. Actor references: how to create a simple communication system
In Akka.Net, when we are working with actors, we are not working with a direct reference of actors to make them communicate. Akka.Net provides what we call an “actor reference” (ActorRef). This latter is a layer of indirection that helps pass around the actors’ references throughout the actor system. Each actor instance has several special references: reference to itself, reference to its parent and references to all the children it created. These references can be obtained when creating an actor or by looking up existing actors.
In this article we got a brief overview on the core concept of Akka and on details of how it is structed and how it works. With this framework, building a reactive distributed fault tolerant and self-healing system becomes easy.
One of the biggest projects concurrent to Akka.Net, is the project Microsoft Orleans. The main differences between the two frameworks are the level of abstraction around the Actor Model and the fact that Orleans is designed for use in the cloud and Akka.Net is designed for use within application and for the cloud. Project Orleans raised the abstraction of the Actor Model so as to simplify it; however, with this level of abstraction developers wonder whether it is a real implementation of the Actor Model.
Get started with Akka.Net link: https://github.com/petabridge/akka-bootcamp