“Using actors is like renting a car — we can rent one quickly and easily if we need one; If the car breaks down, you don’t need to fix it yourself. You can just call the rental company and replace it with another car.”

Excerpt from: [U.S.] Paul Butcher. “The Seven-Week seven-Concurrency Model.”

What is the actor

Actors are similar to objects in object-oriented (OO) programming — they encapsulate state and communicate with other actors via messages. In object oriented, we use method calls to pass information, while in actor, we use sending messages to pass information.

In the actor model, the actor is the smallest unit of computation.

Although mutable state is retained in the actor, it is not shared with other actors, and private variables in the actor are not directly changed by other actors.

  • All calculations are performed in actors
  • Actors can only communicate with each other through messages
  • In response to the message, the actor can do the following
    • Change the state or behavior
    • Send messages to other actors
    • Creates a limited number of child actors

Benefits of the Actor model

  • Event-driven model-Actor communicates in the form of asynchronous messages that do not block other work while waiting for a reply.
  • Strong isolation – Unlike traditional Java objects, actors do not provide public API methods for external invocation. Instead, it only provides services externally in the form of messages. This avoids any sharing of state between actors; The only way to get the state of an actor is to send a message asking for it.
  • Location transparency – The system uses the factory to build an actor instance and returns a reference to it. Since location does not matter, Actor instances can be started, stopped, moved, and restarted to expand/shrink, or to recover from unexpected failures.
  • Lightweight – Using only a few hundred bytes per actor instance, it is easy to create millions of concurrent actors in a single application.

Let it crash

In the traditional model, we tend to consider all kinds of cases as much as possible, and do all kinds of checks on the format and type of the input parameters, so as to tolerate faults. This mixes in a lot of business-irrelevant code, making the code flow more complex without obvious errors

The actor model follows the “let it crash” philosophy for exceptions. Instead of doing defensive logic, let the actor manager deal with these issues.

  • Because actors are independent of each other, a crash of one actor is unlikely to affect other actors.
  • The absence of defensive code makes the logic cleaner and easier to understand.
  • The actor manager can choose the crash handling solution or not handle it and just record the cause of the crash.

Actor managers have four different options for dealing with failures:

  • Resume the subactor, keeping its existing internal state but ignoring the message that caused the failure;
  • Restart the subactor, which cleans up its existing state by starting a new instance;
  • Stop the child Actor permanently and send all future messages to the Dead-letter Office.
  • The fault is transmitted to a higher level, if the regulator itself fails.

The message

Actors communicate by sending messages to each other.

The messages received by the actor are temporarily stored in one of its internal queues, often called a mailbox, and the actor then processes the messages in a serial manner.

Although actors can run at the same time, within a single actor, messages are processed sequentially in the order of mailBox, and the next message will be processed only after the current message is processed, so we do not use the problem of relational concurrency when processing messages.

A reply message

In the Actor model, the ability to reply messages is not provided directly.

Therefore, we typically include a reference to the actor of the sender in the message, so that if the receiver needs to reply to the message, it can reply to the message directly through the reference in the message.

The resources

Seven-week seven-week Concurrency Model

Doc. Akka. IO/docs/akka/c…

zhuanlan.zhihu.com/p/82037463

www.infoq.cn/article/Bui…