Akka Tutorial

Rating: 5
Views: 2977
by Saikumar
Last modified: April 8th 2021

In this Akka tutorial, you will learn why Akka is widely used in developing the applications. How Akka is used to embed the features like multithreading, concurrency into an application.How an actor is created in the actor model. The life cycle of an actor helps us to know how the actor is stopped and restarted. Concepts like creating actor, configuring actor enables you to get basic programming knowledge of the Akka framework. You can learn about the methods used for sending and receiving messages among actors. You will acquire knowledge on implementing multi-threading concept through message passing mechanism. Strategies implemented by the Akka to make application Fault-tolerant and Location Transparent.

If you want to enrich your career and become a professional in Akka, then visit Mindmajix - a global online training platform: "Akka Online Training". This course will help you to achieve excellence in this domain.

Akka Tutorial for Beginners

Following topics will be covered in this Akka Tutorial:

What is Akka?

Akka is a framework or a tool-kit used for building applications on JVM(Java Virtual Machine). The applications built by Akka framework are concurrent, fault-tolerant and distributed. Akka adopts actor model to handle the concurrency. In the actor model, everything is actor likewise in object-oriented design everything is an object. In the Scala actor model, actors share information and tasks by using message passing Mechanism.

Need of Akka

Akka framework is widely used to build applications because the applications built by Akka implement concurrency and multi-threading. Applications which are deployed in real-world should contain features like concurrency or multi-threading because they will serve multiple clients at a time. Akka creates a layer between the actor and baseline system so that the actor’s only job is to process the messages. All the difficulty of generating threads, scheduling threads, Managing race conditions and synchronization, sending and receiving messages, is assigned to the framework to manage lucidly.

Introduction to Actor Model

In 1973 Carl Eddie Hewitt implemented the Actor Model as a conceptual model to manage concurrent computation while software sector is looking for an alternative to implement the features like concurrency and multi-threading in an application, considering as the best option.

In the actor model, the actor is represented as an individual entity. Some characteristics of the actor are as follows:

  • Actor encloses the state and application logic
  • Actor interacts only using messages.
  • Every actor has an exclusive address and mailbox through which he can receive messages from others.
  • Mailbox messages are processed by the actor in consecutive order.
  • Tree hierarchy is used to represent the actor system.

Creating Actor

Actors in Akka are represented using the hierarchy system. They share common configuration and ActorSystem defines it. We can define a sample ActorSystem by calling the Create() method:

ActorSystem system= ActorSystem.create(“test1-system”);

For handling the messages received from other actor extends an abstract class called “AbstractActor” and implements CreateReceive() method.

The actor we created should be included in Actor System.

ActorRef readingActorRef

  = system.actorOf(Props.create(MyActor1.class), "my-actor1");

Myactor1 is the actor we created.

Actor Configuration

Props class is used for Actor Configuration. Props are immutable class, so it is shared during the creation of new actors. Actor Configuration can be explained more by defining an actor named Actor1, and it will process some text.

public class MyActor1 extends AbstractActor {

    public Receive createReceive() {

        return receiveBuilder1().build();



Akka Actor Life-Cycle

Akka actor has life cycle methods. Following are the life cycle methods of Akka actor.

1) preStart()

To provide a particular implementation for an actor, prestart() method is called immediately after creating the actor. This method is overridable. Syntax for prestart() method is as follows:

def prestart() {

println(“prestart method is defined”);


2) postStop()

This method is used to release the resources of an actor after it is stopped. Messages sent to a stopped actor are forwarded to dead letters of the Actor system. This method is asynchronous and overridable.


def postStop(){

println(“postStop method is defined”);


3) preRestart()

When an exception is thrown, the actor may be restarted. After the actor is restarted, preRestart() method is called with the exception, which made the actor restart.


 def preRestart(reason:Throwable,){     

 println(“prerestart method is invoked”);


4) postRestart()

This method is invoked immediately after restarting the newly created actor. It is used to reinitialize the actor which is crashed due to an exception.

def postRestart(){

println(“postRestart method is invoked”);


Akka Actor Sending Messages

Sending Messages using the tell() method

For sending messages, Akka actor does not wait or block a thread.It sends messages asynchronously. Messages can be sent by using (!) exclamation mark. Using tell() method we can send messages as follows:

actor.tell(“ !hai”,null);

In the above statement “hai” is the message.

Message sending using ask method

Ask method is used to send messages and returns future which indicates a reply to the message. After a specific time-out period, the future is completed and TimeOut() Exception is thrown. Always use tell() method for performance and ask() method for reply. Messages can be sent using ask() method as follows:

Val future = actor("Hello");

Akka Actor Reply Messages

Actor can give reply to a message by using sender() method. This method provides you an instance called ActorRef and we can store it. If we want to give reply to a message. You can do it by sender() ! ReplyMsg. Akka actor can reply to messages as follows:

class ActorChildReplyExample extends Actor{  

def receive ={  

 case message:String => println("Message received from "+sender.path.name+" message: "+message);  

println("Replying to "+sender().path.name);  

sender()! "I got your message";  



Akka Actor Forward Messages

Messages are forwarded from one actor to another. Although the message is forwarded through the third party, we can maintain the address and reference of an actor.  

Fault Tolerance in Akka

Supervisory strategies of Akka are the fundamental and direct strategies to define the fault tolerance of your system. The message indicating a failure reaches the supervisor, the following actions are taken:

1. Resuming children and keeping its internal state:

This strategy is applied when the error does not damage the state of the child, and it can continue working correctly.

2. Restarting the child and clearing its internal state

This strategy is applied when the case is just opposite to the above one i.e. when the error damages the state of the child it is restarted.

3. Terminating the child permanently.

This strategy can be used in the scenarios when the error condition is not fixable, and the rest of the operations are performed in the absence of this child.

4. Stop itself and accelerate the error.

This strategy is applicable when the supervisor doesn’t know how to handle the fault, and it accelerates it to own supervisor.

Akka Features: Location Transparency

Another feature that is supported by Akka is Location transparency. This feature enables the actors to know the origin of the messages they receive. The sender of the message may exist in the same JVM or another JVM. Akka architecture facilitates to manage all these scenarios individually in such a way that it is fully transparent to the actor,

Actor systems are made to run in a distributed environment without needing any dedicated code. The configuration file is sufficient for Akka to tell for which node the messages should be sent.

Akka Streams

Akka Stream API is built on the Akka framework. Akka stream API enables us to form data transition flow from stand-alone steps. Essential concepts of Akka Stream are as follows:

  • Source: The starting point to the processing in the Akka- stream library. An object can be created for this class from numerous classes.
  • Flow: Core element used for Main Processing. Every object of flow will have one input value and one output value.
  • Materializer: To add some adverse effects like logging to flow, we will use this.
  • Sink: while building flow, it is not implemented until we do sink function on it. It is the final function to invoke all computations in the whole flow.


Akka builds the applications on JVM(Java Virtual Machine) which satisfies the requirements like concurrency and working on a distributed environment. It is written in Scala and conceals the complexity from the developer. This Akka tutorial is more than sufficient to gain basic knowledge of the Akka framework.