Use Cases of Akka Cluster

Use Cases of Akka Cluster :
  1. Microservices:

    • Used to coordinate and communicate between distributed microservices.
  2. Real-time Data Processing:

    • Handles large-scale data pipelines in distributed environments.
  3. Scalable Systems:

    • For systems that need to scale horizontally by adding new nodes dynamically.
  4. Resilient Actor Systems:

    • Provides redundancy and fault tolerance in actor-based applications.
How It Works :
  1. Cluster Nodes:

    • Each instance of an Akka application participating in the cluster is called a node. These nodes communicate via the Akka remoting mechanism.
  2. Gossip Protocol:

    • Nodes exchange state information using the gossip protocol to maintain cluster state consistency.
  3. Membership State:

    • Nodes can have different statuses like Joining, Up, Leaving, or Down, depending on their role in the cluster.
  4. Message Delivery:

    • Messages are routed automatically to the appropriate actors, regardless of their physical location.
Code Example : Here’s a basic setup of an Akka Cluster node:
import akka.actor.ActorSystem
import akka.cluster.Cluster
import com.typesafe.config.ConfigFactory

object ClusterNode extends App {
  // Load cluster configuration
  val config = ConfigFactory.load("application.conf")

  // Create an ActorSystem
  val system = ActorSystem("ClusterSystem", config)

  // Join the cluster
  val cluster = Cluster(system)

  println(s"Node [${cluster.selfAddress}] started and joined the cluster.")
}?

application.conf Configuration Example :
akka {
  actor.provider = cluster
  remote.artery.canonical.hostname = "127.0.0.1"
  remote.artery.canonical.port = 2551
  cluster.seed-nodes = [
    "akka://ClusterSystem@127.0.0.1:2551",
    "akka://ClusterSystem@127.0.0.1:2552"
  ]
}?