Scala Akka Basics Cheat Sheet
Introduces Akka actors, actor systems and messaging with tell and ask, supervision, and the modern typed actor Behaviors API.
2 PagesAdvancedApr 8, 2026
Defining an Actor
A classic Akka actor with a message-handling receive block.
scala
import akka.actor.{Actor, ActorSystem, Props}class GreeterActor extends Actor { def receive: Receive = { case name: String => println(s"Hello, $name!") case _ => println("Unknown message") }}
Actor System & Messaging
Creating actors and sending messages with tell and ask.
scala
val system = ActorSystem("greeter-system")val greeter = system.actorOf(Props[GreeterActor](), "greeter")greeter ! "World" // fire-and-forget (tell)// Ask pattern for a response (returns a Future)import akka.pattern.askimport akka.util.Timeoutimport scala.concurrent.duration._implicit val timeout: Timeout = Timeout(3.seconds)val future = greeter ? "World"system.terminate()
Core Akka Concepts
Key ideas behind the actor model in Akka.
- ActorRef- A lightweight, location-transparent handle used to send messages to an actor; never reference the actor instance directly
- Mailbox- Each actor has a queue of incoming messages processed one at a time, avoiding shared-state race conditions
- Supervision- Parent actors supervise children and decide how to handle failures: Restart, Resume, Stop, or Escalate
- tell (!) vs ask (?)- tell is fire-and-forget with no reply; ask returns a Future representing an eventual reply
- Props- Immutable configuration object describing how to create an actor instance, passed to actorOf
- context.become- Lets an actor swap its message-handling behavior at runtime for stateful protocols
Akka Typed Behaviors
The modern, type-safe actor API.
scala
import akka.actor.typed.{ActorSystem, Behavior}import akka.actor.typed.scaladsl.Behaviorsobject Greeter { sealed trait Command final case class Greet(name: String) extends Command def apply(): Behavior[Command] = Behaviors.receiveMessage { case Greet(name) => println(s"Hello, $name!") Behaviors.same }}val system: ActorSystem[Greeter.Command] = ActorSystem(Greeter(), "greeter-system")system ! Greeter.Greet("World")
Pro Tip
Never call methods directly on an actor instance or share its mutable state outside the actor — always communicate through its ActorRef with messages, or you lose Akka's single-threaded-per-actor safety guarantee.
Was this cheat sheet helpful?
Explore Topics
#ScalaAkkaBasics#ScalaAkkaBasicsCheatSheet#Programming#Advanced#DefiningAnActor#ActorSystemMessaging#CoreAkkaConcepts#AkkaTypedBehaviors#APIs#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance