Top Akka Interview Interview Questions (2021) | CodeUsingJava








Most frequently asked Akka Interview Questions



  1. What is Akka framework?
  2. What is an Actor in Akka?
  3. What are the Components of an Actor?
  4. What is an ActorSystem in Akka?
  5. Define lifecycle for Akka actor?
  6. What is the difference between Akka Kill vs Stop vs Poison Pill?
  7. Does Akka support clustering and remoting (scaling up and out)?
  8. How does Akka manage failures?
  9. How does akka compare to Erlang?
  10. What's the difference of the Akka's Actor with Scala's Actor model?


What is Akka framework?

  • Akka is a free and open-source toolkit and runtime simplifying the construction of concurrent and distributed applications on the JVM. Akka supports multiple programming models for concurrency, but it emphasizes actor-based concurrency, with inspiration drawn from Erlang.
    One can integrate this library into any JVM(Java Virtual Machine) support language. It implements Actor Based Model. The Actor Model provides a higher level of abstraction for writing concurrent and distributed applications.


What is an Actor in Akka?

An actor is an entity which communicates to other actor by message passing. Actor has it's own state and behavior. As in object-oriented programming everything is an object same like everything is an actor in actor-based system. In other words, we can say that an actor is an object that encapsulates state and behavior.

What is an ActorSystem in Akka?

The ActorSystem is a root actor in actors structure. An ActorSystem is a hierarchical group of actors which share common configuration, e.g. dispatchers, deployments, remote capabilities and addresses. It is also the entry point for creating or looking up actors. It is an abstract class which extends to ActorRefFactory trait. ActorSystem provides an actorOf() method which is used to create actor instance.

What are the Components of an Actor?


Actor


Define lifecycle for Akka actor?

The lifecycle of Akka actor is as follows-
  • preStart()
  • postStop()
  • preRestart(reason: Throwable, message: Option[Any])
  • postRestart(reason: Throwable)

What is the difference between Akka Kill vs Stop vs Poison Pill?

Both stop and PoisonPill will terminate the actor and stop the message queue. They will cause the actor to cease processing messages, send a stop call to all its children, wait for them to terminate, then call its postStop hook. All further messages are sent to the dead letters mailbox. By contrast, the Kill message causes the actor to throw an ActorKilledException which gets handled using the normal supervisor mechanism.


Does Akka support clustering and remoting (scaling up and out)?

Akka's clustering tech is increasingly rich, with tools such as:
  • Akka Cluster Sharding - contains built-in mechanisms to deal with spreading heterogeneous Actors around the cluster, and letting clients communicate with them without needing to know where they are located.
  • Akka Persistence - a robust persistence model for Actors, so that the cluster can be rebalanced easily and automatically and Actors can be passivated safely.
  • Multi-Node Testing - so that you can build a single unit test for Actor Systems clustered across multiple JVMs or multiple hard nodes.

How does Akka manage failures?

A validation error means that the data of a command sent to an actor is not valid, this should rather be modelled as a part of the actor protocol than make the actor throw exceptions.A failure is instead something unexpected or outside the control of the actor itself, for example a database connection that broke. Opposite to validation errors, it is seldom useful to model failures as part of the protocol as a sending actor can very seldomly do anything useful about it.
For failures it is useful to apply the "let it crash" philosophy: instead of mixing fine grained recovery and correction of internal state that may have become partially invalid because of the failure with the business logic we move that responsibility somewhere else. For many cases the resolution can then be to "crash" the actor, and start a new one, with a fresh state that we know is valid.


How does akka compare to Erlang?

Erlang does copy-on-send - Akka uses shared memory (immutable objects) for in-VM sends
Erlang does per-process GC - Akka uses JVM GCs
Erlang has OTP - Akka integrates with the entire Java ecosystem (Apache Camel, JAX-RS, etc etc)
Erlang does the process scheduling for you - Akka allows you to use many different Dispatchers with endless configuration opportunities
Erlang does hot code reload - Akka can support it, but it's less flexible because of JVM classloading


What's the difference of the Akka's Actor with Scala's Actor model?

For programming model, there is a big difference between the scala standard library and Akka. In the scala standard library, you basically implement the "run" method - and if you want to wait for an incoming message, you get yourself into a waiting state (by calling "receive" or "react"). So, the programming model follow the "thread-metaphor". However, in Akka, the programming metaphor is that you implement a few life-cycle methods - but the "run"-method is written inside the framework. It actually turns out that this programming model works a lot better with the event-based execution model as well.