Mind is Software

Ying’s thoughts about software and business

Akka

This is a note for learning Akka based on the book Akka in Action and the official document.

What is Akka

Akka is an open source toolkit that provides the actor programming model, runtime, and required supporting tools for building concurrent and distributed applications.

Akka is centered on actors that are a lot like message queues without the configuration and message broker installation overhead. They are micro-size message queues that you can create thousands or millions of them. They receive immutable messages and act on each message to execute some tasks and, unlike message queue, can also send messages to other actors. Actor works asynchronously and can be local or remote.

Akka features

  • Use in-memory state, changes are sent as messages to a log. Only reread log if the applicatoin restarts.
  • Push events to interested parties.
  • Use asynchornous message sending/receiving.
  • Manage loosely-coupled crashed components.

Actors store state in memory and log changes to an event source. It recovers from events, partitions cross servers, sends and receives messages. Messages are ordered, one at a time. A supervisor watches actors to handle errors and crashes.

An actor only has four asynchronous operations.

  • Send: sending immutable untyped message in a fire and forget style. The order of sent messages is kept between a sending actor and its receiving actor.
  • Create: an actor can create other actors.
  • Become: actors implement state machines to manage state.
  • Supervise: an actor supervises that actors that it creates.

Actors are decoupled on three axes: space/location, time, and interface. An actor doesn’t care the location of another actor. It has no expectation when its work will be done. An actor has no defined interface and nothing is shared between actors.

When an application starts, Akka creates an ActorSystem that creates a top-level actor, usually one, that is a Supervisor actor to monitor everything. The ActorSystem provides remoting and event sorucing for actors. More capabilities are provided as Akka extensions.

An actor has an ActorRef and ActorPath. Messages are sent to the actor’s ActorRef and will be first stored in a mailbox in the order they arrive. Akka uses dispatchers to push the messages to actors’ mailbox and invoke actors at some point later. A dispatcher can use different threading models. Actor are lighter than threads: 2.7 million actors can fit in 1GB of memory while only 4096 threads fit for 1GB memory.

Simple, Akka provides:

  • event-driven, multi-threaded behavior without the use of low-level concurrency constructs
  • transparent remote communication between systems and their components
  • high-available and scalable architecture
  • lightweight
  • strong isolation through messaging

Quickstart

When defining Actor and messages, the best practices are:

  • define message with good names and rich semantic and domain specific meaning.
  • message should be immutable.
  • define messages as static classes in its actor.
  • use a static props method describing hwo to construct the actor.

In Akka, you use a factory to create an actor and return an ActorRef that point to the newly-created actor instance. This level of indirection brings location transparency and flexibility because the actor can be re-created on deman.

The actorOf() method of the akka.actor.ActorSystem factory takes two parameters: a configuration Props object and a name.

The Reason of Being

Akka is created by the following observations:

  • Real concurrency should delegate tasks to threads without blocking and locks.
  • The task delegation requires a new programming model that includes explict error handling and recovering.
  • No need to care about the low-level executing mechanism.

Instead of calling, Akka uses message passing. Actors have states and receive messages and act on them one by one independently. Actors are supervised to handle failures.

Akka has the following parts:

  • Actor library: the core Akka library is akka-actor. Actor encapsulates its state and execution.
  • Remoting: it enables actores living on different computers and makes message passing location-transparent.
  • Cluster: it manages a set of related systems and provides more functions thant remoting.
  • Cluster sharding: it helps to distribute a set of actors among cluster members.
  • Cluster singleton: a single entity shared by other members of the cluster. It can migrate to anthoer member when the host computer fails.
  • Cluster publish-subscribe: it allows message multicasting (broadcasting).
  • Persistence: it enables actors to save and restore state to/from a storage.
  • Streams: a graph of actors process a stream of sequential events and coordinate resources.
  • HTTP: a separate module to build and consume HTTP service.

Supervision

There is a supervision hierarchy. There are four options for an actor to respond to a failure of its subordinates: resume, restart, stop permanetnly, and escalate the failure.

Each supervisor is configured with a function translating failures into one of the four choices. Supervision-related paretn-child communcation happens in a special system channel and the order of messages is not deterministic.

A system has at least three actors, i.e., the root guardian /, not a real actor but a bubble walker. and its two children: the /user guardian and the /system guardian.

In addition to supervision, an actor can monitor any other actor. The death of an actor results in a Terminated message sent to the monitoring actor.

Actors

There are many types of actor references:

  • purely local actor: no support for networking.
  • local actor: it has protocol and remote addressing information.
  • local with route: send message to its children.
  • remote actor: in a different machine/process.
  • special-purpose actors
    • Promise actor
    • Dead letter actor
    • Empty local actor
  • internal actors
    • root actor
    • first logging actor

An actor has a logical path and a physical path.

Actors are created by ActorSystem.actorOf() method or ActorContext.actorOf() method. Use ActorSystem.actorSelection() or ActorContext.actorSelection to find an actor reference.


Share