What is Akka | Akka Actors | Parallelism & Multithreading

Reading Time: 2 minutes
Akka Logo

What is Akka?

Akka is a platform for designing scalable, resilient systems that span processor cores and networks. It allows you to focus on meeting business needs instead of writing low-level code to provide reliable behavior, fault tolerance, and high performance.

Features provided by Akka

  • Multi-threaded behavior – without the use of low-level concurrency constructs like atomics or locks. Thus, relieving you from even thinking about memory visibility issues.
  • Transparent remote communication between systems and their components – Thus relieving you from writing and maintaining difficult networking code.
  • Clustered, high-availability, elastic architecture, that scales on-demand – Thus enabling you to deliver a truly reactive system.

What are Akka Actors?

Firstly, Akka’s actor model adds a layer of abstraction that makes it easier to write accurate concurrent, parallel, and distributed systems. Secondly, The actor model applies to the entire set of its libraries. Thus, allowing you to understand and use them consistently.

Features of Akka actors :

  • An Actor provides a higher level of abstraction for writing concurrent and distributed system
  • An actor can easily write asynchronous code without the need for lock and synchronization.
  • It helps deal with explicit locking and thread management.
  • Hence making it easier to write correct concurrent and parallel systems.
  • Actors are objects and you can’t access them directly rather, but only send messages.
  • An Actor represents an independent computational unit. Therefore it encapsulates its state and part of the application logic.
  • Actors are isolated from each other, So they can only communicate through message
  • In addition, Actors do not share memory.
  • Multiple messages are processed in FIFO order / Only one message is processed at a time.

Let’s see an Example

1. Adding the Dependency

libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.6.18"

2. Creating an Actor System

val actorSystem = ActorSystem("firstActorSystem")
println("Actor System name: " + actorSystem.name)

3. Creation of Actor

class WordCounter extends Actor {
    override def receive: Receive = {
      case message: String => println(s"[WordCount] Message received: ${message}")
      case _ => println(s"[WordCount] I didn't understand")

4. Instantiate an Actor

val wordCounter = actorSystem.actorOf(Props[WordCounter], "wordCounter")

5. Communicate with an Actor

wordCounter ! "I am learning akka and scala because its amazing and fast"
wordCounter ! 15

Output :

Actor System name: firstActorSystem
[WordCount] Message received: I am learning akka and scala because its amazing and fast
[WordCount] I didn't understand

Code Representation

Actor System Code


Link: https://doc.akka.io/docs/akka/current/typed/guide/introduction.html

Written by 

Pallav is working as Software Consultant at Knoldus Inc. He is a technology enthusiast and a keen learner with more than 2 years of experience. He works as a Quality Catalyst in the organization. He has a good understanding of programming languages like Java and Scala. He likes to listen to songs and playing table tennis.