The runtime

MLambda.Actors

The actor model, native: actors addressed by route, so the same code runs in one process, a cluster or a node that is both.

In production under every MLambda service

MLambda Actors

An actor model written in Aleph and compiled to native code. Actors are addressed by route rather than by location, so the same actor runs unchanged in one process, inside a cluster, or on a node that is both at once. Moving from a laptop to a four-node cluster changes configuration, not code.

# aleph.toml
[dependencies]
mlambda-actor = { path = "../MLambda.Actors/MLambda.Actor" }

An actor

An actor is a class: a data type holds its state, and its instance of the Actor trait holds its methods. The attributes above the instance are its settings.

data Counter    = Counter { count :: Int }
data CounterMsg = Increment | Current (Reply Int)

@route("counter")
instance Actor Counter where
  type Message Counter = CounterMsg
  receive ctx c msg = case msg of
    Increment -> continue c { count = c.count + 1 }
    Current r -> respond r c.count >> continue c

instance Initial Counter where
  initial params = Counter 0

Talking to it

counterOf :: ActorSystem -> Address Counter
counterOf system = actorOf system (parameters [])

-- tell: no answer; ask: the answer, or the failure of the actor asked
tell counter Increment
ask counter (\r -> Current r)          -- Right 1

An address is typed by its class, so the compiler rejects a message the actor does not accept. An answer is a value, Either Fault r: a refusal, a failure or a timeout comes back as Left, never as an exception.

That code does not change when the counter moves to another machine. A host decides whether a route is answered here or through the cluster. See Topology & Hosting.

What you get

   
Classes Actors as a trait and its instances, settings as attributes (@route, @mailbox, @priority, @supervision, @passivateAfter, @ephemeral)
Guardian hierarchy A root, /user, /system and /temp, with parent-child supervision
Supervision oneForOne and allForOne, deciding Restart, Resume, Stop, Escalate or Propagate by the failure’s kind
Behaviour switching become and unbecome at run time, with message stashing
Death watch Watch an actor and be told when it stops
Lifecycle hooks onPreStart, onPostStop, onPreRestart, onPostRestart
Timers and timeouts scheduleOnce, scheduleRepeat, askWithin, a circuit breaker
Clustering Gossip membership, phi accrual failure detection, a CRDT route table
Fortress Every frame between nodes encrypted and authenticated with the cluster’s secret
Observability Prometheus metrics, Zipkin spans for Jaeger, per-request diagnostics
Sagas Steps with compensations, retries, breakers and parallel stages

Three ways to run the same actor

Topology What it is Use it for
Standalone One process, no network Tests, prototypes, single-process apps
Cluster A routing mesh with no actors of its own The backbone that other nodes join
Hybrid A node that both joins the mesh and hosts actors Most real deployments

Read Topology & Hosting for how a host is configured, and Deployment for the Docker images and compose files.

Where to go next

Project

The source is the MLambda.Actors repository, available through early access. How to build it and its images is on the Packages & Releases page.

Every page