Actor's in Akka Framework




As in Java, we can compare Actor as a thread. But in comparison to the threads, Actors are much lighter. Its a much higher level of abstraction than the threads. The implementation is also much simpler than as compared to the threads. Once we grab the main idea of this model it let us focus on our requirements more than managing the low-level problems like a lock and shared data as in threads.

Some of the main features of Akka actor library are

  • It is fault tolerance which means it can be used in creating the "self-healing systems".
  • As per the documentation, there can be 2.7 million actors per GB of RAM.
  • It provides us location transparency. It can span multiple JVM's and servers it is designed to work as distributed servers.
Alvin Alexander in his book called Scala Cookbook states that 

"I like to think of an actor as being like a web service on someone else's servers that I cant control. I can send messages to that web service to ask if to do something, or I can query it for information, but I cant reach into the web service to directly modify its state or access its resources, I can only work through its API, which is just like sending immutable messages, In one way, this is a little limiting but in terms of sagely writing parallel algorithms it is very beneficial."

I think this statement is enough to understand the actor's main concept.

The Actor Model
Here are some points about the actors.
  1. Like an object in the actor-based system, an actor is the smallest unit.
  2. Like objects, actors will also have state and behavior. 
  3. We cannot do anything inside the actor itself to get its state. The only thing we can do is sending actor a message to request its state information but we cannot call and execute its internal methods. 
  4. Every actor will have its own mailbox which the main purpose is to process the message in the mailbox.
  5. We can only communicate with the actor by sending them the immutable messages which go into its mailbox.
  6. After receiving the messages Actor takes it out from the mailbox do some task for which it is built for and once completion takes other messages from the mailbox. And if there is no message then it will wait until it receives one.

Typesafe suggest us to think the actor's as a person, such as a person in the business organization. An actor can have only one parent which is also known as the supervisor who created it. But an actor can have many children. Similarly, an actor can have may siblings. Each actor is delegate to their own work. 

Failure handling
If something goes wrong or if an exception occurs when the actor suspends itself and all of his children and sends a message to his parent(supervisor) about the failure and its occurrence. 
based on the nature of work and failure the supervisor actor has the four choices which are:
  1. Resume the subordinates, keeping its internal state. 
  2. Restart the subordinates, giving it a clean state. 
  3. Terminate the subordinates. 
  4. Escalate the failure.

Comments

Popular posts from this blog

Functional Programming in Java - Part 1

Akka Actors in Java - Part 1

sealed keyword in Scala