Singleton in a cluster
Singleton is a design pattern means allowing only one instance of the class. (Check out more on singleton’s here) Singleton works well till the point you have single JVM.

In a multiple JVM’s environment, each of them will have their own copy of the singleton object which can lead to multiple issues specially in a clustered environment where the access to the resource needs to be restricted and synchronized.

To achieve clustering across JVM’s, one can use multiple techniques (JMS, DB, Custom API, 3rd party tools), but each of them have an impact on the business logic. If the requirement for JVM clustering is realized later in the game, then change to business logic will be huge.
  • In case, the requirement to have singleton across JVM’s is realized later, then tools like Terracotta, Oracle Coherence are good options. These work on the concept of providing an in memory replication of objects across JVMs in effect providing you singleton view  or making use of any of the cluster-aware cache provider’s like SwarmCache or JBoss TreeCache should work as cache entries are singletons and clustering is built in.
  • Also, there is product called JGroups – which uses multi-cast comm. (TCP/UDP).  It allows to form a group and Applications (JVM’s) can participate and JGroups will send messages to everyone in the group so that they can be in sync. 
  • Application server’s also provide some level of custom API’s to circumvent this problem. 
  1. JBoss has HASingleton Service ( based on MBeans) which is meant to solve this problem. Check here and here
  2. Weblogic has the concept of Singleton Service – where only instance runs within the cluster and all clients will look up to the same instance. 
  3. WebSphere supports the concept of singleton across cluster in the WebSphere XD version of the application server as the partition facility – ObjectGrid 
    5 Comments To ' Singleton in a cluster '

    Anonymous said...

    Thanks a lot a very good articel

    Anand said...

    Thanks. This one is too good. Consolidated view of all the approaches.

    Sudhir Katroliya said...

    It is good articel

    Alen J said...

    Great article singleton is best choice for unique classes, thanks for showing up class services.

    DTDC Tracking

    gaurav said...

    up vote
    0
    down vote
    favorite
    I have configured a Akka scheduler in my play application. It works fine but now the problem is I have two instances of same application running in a cluster. So scheduler is also running twice. I want it to run only once for the overall application. Is there some provision in akka to achieve this. Also java related help will be appreciable.

    Post a Comment