Akka Primer
Akka is a toolkit for building resilient, concurrent, and distributed systems on the JVM. This primer walks through the actor model, message passing, and supervision strategies so you can wire Meridian agents into an Akka cluster without surprises.
1. The Actor Model
Actors are lightweight isolated units of computation that communicate exclusively via asynchronous messages. Each actor owns its mutable state, processes one message at a time, and never shares memory directly with another actor. This eliminates entire categories of concurrency bugs by construction.
2. Defining a Behavior
Akka Typed asks you to describe an actor as a Behavior. The example below shows a simple ping-pong responder that replies with a counter on every message received.
object Pinger {
sealed trait Cmd
case class Ping(replyTo: ActorRef[Pong]) extends Cmd
case class Pong(count: Int)
def apply(n: Int = 0): Behavior[Cmd] =
Behaviors.receive { (ctx, msg) =>
msg match {
case Ping(reply) =>
reply ! Pong(n + 1)
apply(n + 1)
}
}
}3. Supervision and Failure
Let it crash. Akka actors are supervised by their parent, and the parent decides whether to restart, resume, or stop a failing child. Combine this with persistence and clustering and you get systems that self-heal under load instead of collapsing into deadlock.