An introduction to Akka Clustering

Reading Time: 3 minutes

Akka cluster provides a fault tolerant decentralized peer to peer cluster membership service with no single point of failure or single point of bottleneck. It does this using gossip protocol and an automatic failure detector.

Node is a logical member of a cluster there could be multiple nodes in a single machine. It is defined by a hostname:port:uid. UID is not user-visible most of the time it used by the Akka internally to identify whether it is the same system or system on the same node and port.

Cluster is a set of node drawing together through the cluster membership protocol. Cluster assigned a special leadership role to a node in the cluster. It will manage cluster convergence such as down and up event. Akka cluster is not a SPOF (single point of failure) and it is fine if a node fails, another node will take over immediately.

The is also a seed node as you can see in the above image. The seed nodes are configured contact points for new nodes joining the cluster. When a new node starts, it sends a message to all seed nodes and then sends join command to the seed node which answers first.

In Akka cluster, there is a membership lifecycle for every node. The node begins with the joining state. Once all the nodes have seen that a new node is joining through a gossip protocol, the leader will set a node in “up“. If the node leaving the cluster is safe, it switches to the leaving state. Once the leader sees the node in the leaving state, the leader will set a node in the existing state and once all other nodes see the existing state, the leader will remove the node from the cluster and mark it as removed. If something abnormal happens on the node, it is set unreachable until the leader decides to mark it “down“. This could happen only when auto-downing is enabled or Akka split brain is used or an operator on node invokes the down command explicitly.

So let’s start with the basic configuration.

The following configuration enables the Cluster extension to be used. It joins the cluster and an actor subscribes to cluster membership events and logs them

Joining to Seed Nodes

Now Let’s create simple cluster listener

The actor registers itself as a subscriber of certain cluster events. It receives events corresponding to the current state of the cluster when the subscription starts and then it receives events for changes that happen in the cluster.

That’s all for this blog



Written by 

I am from India and developing Scala Microservices :)