Akka Cluster in use (Part 2): Forming a Cluster

Reading Time: 3 minutes

Hello friends, in our last post on Akka Cluster, we understood the purpose of an Akka Cluster. Now, next step is to understand: How to Form an Akka Cluster?

But before we start forming an Akka Cluster, let us understand that: How Actors can communicate with each other over a cluster of machines (JVMs)? Now we know that each Actor in Akka has an Address. That Address can be local or remote, i..e., the address can be of local JVM or remote JVM. The actors in the local JVM can be communicated with the local addresses. Whereas to communicate with remote actors, remote addresses have to be used. Now let us see: How an Actor Address looks like?

In the above image we can see that an address is composed of 4 parts – ActorSystem name, Host Name or Address, Port, and the ActorPath.

Now we know how actors can communicate with each other over a cluster of JVMs. So, we can begin forming a cluster.

Step 1: Configure Remote Addresses

To configure remote addresses, a remote transport protocol should be enabled in the ActorSystem. There are 3 protocols that are supported by Akka Cluster:

  • aeron-udp: A high throughput and low latency protocol.
  • tcp: Good throughput and latency protocol, but less than aeron-udp.
  • tls-tcp: An encrypted protocol which encrypts the data being transferred across the cluster.

Following is a sample remote address configuration for creating an Akka Cluster on our local machine:

akka {
  remote {
    artery {
      transport = tcp
      canonical {
        hostname = "127.0.0.1"
        port = 2551
      }
    }
  }
}

Step 2: Naming ActorSystem

Since Akka Cluster leverages the remote capabilities of Akka, by using the remote addresses of the actors. It becomes necessary that the ActorSystem’s name should be same on all JVMs. Otherwise, Akka Cluster won’t be able to link all the ActorSystems, running on different JVMs, into a single cluster of ActorSystems.

Step 3: Enabling Akka Cluster

Next step in order to setup Akka Cluster is to enable the Akka Cluster. This can be done by adding the following configuration to our application:

akka {
  actor {
    provider = "cluster"
  }
}

This configuration will allow the Actor System to manage actors on the other JVMs too.

Step 4: Joining the Cluster

How to Join the cluster? Now when the cluster is enabled and configured, a JVM needs to join the cluster in order to form one. For that Akka Cluster requires an initial set of JVMs which can be contacted by a new JVM to join the cluster. They are known as Seed Nodes. Although seed nodes are not special nodes, they are just like other nodes of the cluster. Even once a cluster is fully formed, seed nodes are no longer required.

For configuring the seed nodes, we need to provide a set of addresses of the nodes along with the ports:

akka {
  cluster {
    seed-nodes = [
      "akka://Loyalty@127.0.0.1:2551",
      "akka://Loyalty@127.0.0.1:2552"
    ]
  }
}

However, there are other techniques too via which seed nodes can be assigned dynamically, like Akka Cluster Bootstrap. But we will not be covering that in the blog post.

Step 5: Forming the Cluster

The last step of forming an Akka Cluster is to start the seed nodes and start forming a cluster. For that we need to start the seed node in the list provided in the configuration. Since, the first seed node cannot contact or communicate with any other node, it will form a cluster of one node. Rest of the nodes will join the cluster formed by the first seed node and form a multi-node cluster. However, once the cluster is fully formed, the first need is no longer required. But as a Good Practice, at least one seed node should always be running so that new node(s) can be added to the cluster, if required.

So, now we know, how to form an Akka Cluster. In out future posts we will get to know, how to create an Akka Cluster on our local machine and how to manage it. So stay tuned 🙂

Refrences

Written by 

Himanshu Gupta is a software architect having more than 9 years of experience. He is always keen to learn new technologies. He not only likes programming languages but Data Analytics too. He has sound knowledge of "Machine Learning" and "Pattern Recognition". He believes that best result comes when everyone works as a team. He likes listening to Coding ,music, watch movies, and read science fiction books in his free time.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading