Connecting...

W1siziisimnvbxbpbgvkx3rozw1lx2fzc2v0cy9zawduawz5lxrly2hub2xvz3kvanbnl2jhbm5lci1kzwzhdwx0lmpwzyjdxq

An introduction to Akka Clustering by Prabhat Kashyap

W1siziisijiwmtkvmdyvmdqvmtuvntuvmzivote0l3blegvscy1wag90by01ndy4mtkuanblzyjdlfsiccisinrodw1iiiwiotawedkwmfx1mdazzsjdxq

Looking to learn more about Akka Clustering?

Knoldus Software Consultant, Prabhat Kashyap gives us a great introduction, showing us the basic configuration and more!

 

'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 this 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 is started it sends a message to all seed nodes and then sends join command to the seed node that answers first.

In Akka cluster, there is a membership lifecycle for every node. The node begins with the joining state. Once all node has seen that the new node is joining through a gossip protocol, the 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. Once all node sees 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

1  class SimpleClusterListener extends Actor with ActorLogging {
2
3  val cluster = Cluster(context.system)
4
5  // subscribe to cluster changes, re-subscribe when restart
6  override def preStart(): Unit = {
7  cluster.subscribe(self, initialStateMode = InitialStateAsEvents, classOf[MemberEvent], classOf[UnreachableMember])
8  }
9  override def postStop(): Unit = cluster.unsubscribe(self)
10
11 def receive = {
12 case MemberUp(member) =>
13 log.info("Member is Up: {}", member.address)
14 case UnreachableMember(member) =>
15 log.info("Member detected as unreachable: {}", member)
16 case MemberRemoved(member, previousStatus) =>
17 log.info("Member is Removed: {} after {}", member.address, previousStatus)
18 case _: MemberEvent => // ignore
19 }
20 }

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'

 

This article was written by Prabhat Kashyap and posted originally on Knoldus Blog.