The Top 7 Mistakes Newbies Make with Akka.NET
15 minutes to readThe actor model is a radically new concept for the majority of Akka.NET users, and therein lies some challenges. In this post we’re going to outline some of the common mistakes we see from beginners all the time when we take Akka.NET questions in Gitter chat, StackOverflow, and in our emails.
7. Making message classes mutable
One of the fundamental principles of designing actor-based systems is to make 100% of all message classes immutable, meaning that once you allocate an instance of that object its state can never be modified again.
The reason why immutable messages are crucial is because you can send the same message to 1000 actors concurrently and if each one of those actors makes a modification to the state of the message, all of those state changes are local to each actor.
There are no side effects to other actors when one actor modifies a message, which is why you should never need to use a lock
inside an actor!
For example, this is an immutable message class:
public class Foo{ public Foo(string name, ReadOnlyList<int> points){ Name = name; Points = points; } public string Name {get; private set;} public ReadOnlyList<int> Points {get; private set;} }
This class is immutable because:
string
is an immutable class - any modifications to it produce an entirely new string. The original is never modified, so to all of the other actors processing thisRead more