akka

Akka

Akka typed actor system patterns for building reactive, distributed, and concurrent Scala applications

Details

Language / Topic
scalaScala
Category
framework
Compatible Frameworks
akka

Rules

balanced
- Define actor behavior with `Behaviors.setup { ctx => Behaviors.receiveMessage { case Cmd(...) => ... } }` — use typed `ActorRef[Protocol]`, never `ActorRef[Any]`.
- Model protocols as sealed trait hierarchies: `sealed trait Command; case class DoWork(replyTo: ActorRef[Result]) extends Command`.
- Spawn child actors with `ctx.spawn(behavior, "descriptive-name")` — meaningful names are essential for supervision tree debugging.
- Use `ctx.ask[Cmd, Reply](ref, replyTo => Cmd(data, replyTo), timeout, mapResult)` for request-reply — never use raw `?` with typed actors.
- Supervise with `Behaviors.supervise(behavior).onFailure[DBException](SupervisorStrategy.restart.withLimit(3, 1.second))`.
- Use Akka Streams `Source`, `Flow`, `Sink` for backpressure-aware pipelines — prefer over manual queue management in actors.
- Persist state with `EventSourcedBehavior[Command, Event, State]` — implement `commandHandler` and `eventHandler` as pure functions.
- Register services in the Receptionist: `ctx.system.receptionist ! Receptionist.Register(ServiceKey[Protocol]("svc"), ctx.self)` for cluster-wide discovery.