Cluster-singletons – what and what for?

447 0

A cluster-singleton is a service that is deployed across multiple cluster nodes, which is never active in more than one node concurrently. There are multiple copies of the same service, all of them deployed into a cluster, each copy deployed on its own node, but only one of those copies is effectively active at any point in time.

Cluster-singletons are not a java thing. They are a software thing (says a Java Guy).

But there is still a very important question to be answered here: why would you cluster a service if you are not taking advantage of the multiple nodes? Typically you would want to cluster a service so that the cluster nodes could collaborate in handling a workload. Isn’t that right? It is right, but there is a catch. Clusters are not only about scalability. They are not only meant to allow you to handle a continuously growing workload. Clusters are also for High-availability. The reason why you would deploy a singleton service into a cluster is for you to be sure that the service will always be running. Because when the node that is currently with the service active goes down, another node will be elected to activate the service. This is why cluster-singleton services are also called HA-singleton services.

One thing is to be taken seriously: Cluster-singletons are expected to be rare. It’s okay if you have been developing applications for years and never came across one. It probably made sense to not have one or you weren’t that serious about High-availability. But you don’t want to develop a cluster-singleton REST API. Why would you do that? Typically you would want your API to scale-out one day and it’s fine if you don’t expect it to scale, but why give yourself the trouble of making it a cluster-singleton? See, cluster-singletons are meant for background tasks. There is also a very small set of use cases that are not necessarily background-tasks. But remember, it’s a very small set. I could give you the example of a cluster-singleton service that generates auto-increment IDs for new database records. It makes total sense for this service to be a singleton: to guarantee that IDs won’t be duplicated or re-used.

When you build a cluster-singleton service, there is a very key aspect you need to take into account, mostly when you cluster-singleton service is a background tasks: embedded fault-tolerance. I don’t mean circuit breakers, bulk-head and all that microservices mambodjambo, no. I mean: recoverability. Make sure that on startup, the service checks for any work that might have been interrupted, so that it can complete that work before it runs the normal procedure. This is critical, because you never know when will the active node go down.

The second key aspect to take into account when building cluster-singleton services is something I named the singleton-responsibility principle:

“A singleton-service should only perform tasks that would introduce inconsistency when performed concurrently”. Mário Júnior

You have to agree that this is even more important than the recoverability, which i considered to be be most key aspect. This is the actual key aspect.

Picture a scenario in which we are building a cluster-singleton service to send birthday felicitation emails to customers at midnight. The details of the customers to be notified are all in a database. We are making it singleton because we don’t want the customers to be getting duplicates. If we have two or more instances of this service, all polling the database concurrently, they might pick up the same record at some point in time and then notify the same customer. That’s why we decided to make it singleton. To avoid duplications. I want us to quickly explore two possible designs for this cluster-singleton service:

  1. Simple design – the cluster singleton service does everything, from the email message composition to the email message dispatching.

Cluster-singletons - what and what for?

This design works just fine, but the problem with this approach is that we are adding unnecessary complexity to the singleton-service. Wait! what? Complexity? Why? Because now the singleton-service has to care about connectivity with the email server and this comes with all sorts of problems, like for example: having to retry when the email fails to be delivered to the SMTP server. Worst, it limits the amount of felicitation emails that can be sent concurrently. This is why the cluster-singleton service must only do the job that is required to be singleton. The overall solution design itself is simple, but the cluster-singleton is internally complex.

  1. Apparently complex design – the cluster-singleton service only composes the messages.

Cluster-singletons - what and what for?

The overall solution design here is much more complex than the first, but the singleton-service internal design was simplified. It composes an email message, pushes it into a message queue and assumes that another system will dispatch those messages asynchronously. The message dispatcher cluster is then responsible for actually dispatching those email messages and now we can even scale the message dispatcher as we wish. Note that it is assumed that the message-queue will ensure that the same message is not delivered to multiple dispatcher nodes concurrently, avoiding concurrent dispatching, ensuring that the dispatcher nodes are all busy, dispatching different messages. The singleton-service will now take less time to complete its execution, since it’s doing less things. It’s all about the singleton-responsibility principle.

The inconsistency doesn’t come from sending multiple emails concurrently. We don’t have to make the sending part become singleton. The inconsistency comes from the racing condition introduced when having multiple instances polling the database. The only thing our singleton service has to do is to eliminate that racing condition, which leads us to an interpretation of the singleton-responsibility principle:

“Cluster-singletons are meant to avoid racing conditions and provide predictability with a consistent course of actions or events”. Mário Júnior

This is pretty much all I had for you regarding cluster-singletons. There were probably tons of spelling errors, but I hope you got the message I was trying to convey. I will soon publish another article about cluster-singletons, but focusing on Java-EE.

It’s always a pleasure. Happy new year.

(Visited 5 times, 1 visits today)

Mário Júnior

Mário Francisco Júnior is the Head Of Software Development at Vodacom Mozambique.