Chapter 5. Actors

Among the many programming models known today in computer science, the actor model is a prominent one. As you’ll learn in this chapter, Dapr provides an efficient, lightweight, and flexible implementation of this programming model that supports any modern programming language.

The Actor Pattern

In computer science, the actor model is described as a mathematical model for concurrent computations. An actor can be thought of as a primitive for these computations: it receives a message and can then make local decisions based on that. An actor encapsulates its own state and achieves scalability by being partitioned using unique IDs. Once it has received a message, an actor can respond to the message, connect to an external system, or create more actors of different types. An actor may be stateful or stateless: when stateful, it modifies its own internal state. This leads to an object-oriented programming experience, where the state of an object is encapsulated within it, modified by strongly typed methods. Actors are always invoked in a turn-based concurrency model and allow for a single-threaded access to any given instance of an actor.

For example, Figure 5-1 illustrates that two concurrent calls to the same actor instance of type Cat, invoking the Eat method, will execute in a serial manner.

As you can see, each time a request comes in to an actor, a lock is acquired; only then can the method be executed and the actor’s state changed. Each actor instance keeps ...

Get Learning Dapr now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.