Introduction to Actor Model [Akka in a Nutshell #1]

According to Akka documentation,

” An actor is a container for State, Behavior, a Mailbox, Child Actors and a Supervisor Strategy. “

Let us begin by understanding the need of an Actor Model,

A lot of things have changed in the present scenario. The processes are getting more coarse, This means in order to execute our programs faster we need to use multiple cores which in turn would use multiple threads.

One of the model that helps us to do so is the Actor model.

” The Actor Model provides a higher level of abstraction for writing concurrent and distributed systems. It alleviates the developer from having to deal with explicit locking and thread management, making it easier to write correct concurrent and parallel systems. ”

A very useful approach to achieve concurrency today would be by having Shared Mutable State.

But the problem with this approach can be that, state of a large number of stateful objects can be changed by multiple parts of your application each running in its own thread.

We need objects that can handle non blocking operations and can save the internal state from other operations.

These objects are nothing else but Actors.

An Actor is a computation entity that in response to a message,

  • can send a finite number of message to other actors.
  • create a finite number of new actors.
  • designate their behaviors to be used for the next message it receives.

It gives you:

  • Simple and high-level abstractions for distribution, concurrency and parallelism.
  • Asynchronous, non-blocking and highly performant message-driven programming model.
  • Very lightweight event-driven processes (several million actors per GB of heap memory).

I hope you guys now can have a clear picture about Actors. So let’s take a step forward and discuss some basic terminologies related to Akka.

  • Concurrency Vs Parallelism


  • Asynchronous Vs Synchronous


  • Blocking Vs Non Blocking


I think we are ready to do the mandatory Hello World program to get our hands on Akka. 😛

Hello, Akka !!

Add dependency for Akka in your build.sbt, here we will be using this one:

libraryDependencies += "com.typesafe.akka" % "akka-actor_2.11" % "2.4.14"

Let us now define a simple greet message,

case class Greet(name: String)

And a greeter Actor for our greet message,

class Greeter extends Actor {
  def receive = {
    case Greet(name) => println(s"Hello $name")

The complete code snippet would be :

import{Actor, ActorSystem, Props}

//greet message
case class Greet(name: String)

//greeter Actor
class Greeter extends Actor {
 def receive = {
 case Greet(name) => println(s"Hello $name")

object HelloAkka extends App {

 val system=ActorSystem("Intro-Akka")

 val greeter=system.actorOf(Props[Greeter],"greeter")

 greeter ! Greet("Akka")

Guys,this is it for now. I hope this blog helped in providing a better knowledge about Actors. In our further blog series, we’ll cover the following topics :



About Himani Arora

Software consultant at Knoldus Software LLP.
This entry was posted in Scala and tagged , , , , , , . Bookmark the permalink.

4 Responses to Introduction to Actor Model [Akka in a Nutshell #1]

  1. Prabhat Kashyap says:

    Reblogged this on Prabhat Kashyap – Scala-Trek.

  2. Pingback: Getting Started with Actors [Akka in a Nutshell #2] | Knoldus

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s