Options for implementing Singleton in Clustered environment

Singleton is a very popular java design pattern (read more) that restricts the Instantiation of a class to one object. Singleton works well in a non-clustered environment where you have only single JVM running but in a multiple JVM environment, where each of them will have their own copy of the singleton object can lead to multiple issues because the access to the resource needs to be restricted and synchronized.

To achieve the clustering of Singleton objects across JVM’s, one can use various techniques available like (JMS, DB, Custom API, 3rd party tools etc), but each of them have some impact on the core business logic and specially if the requirement for clustered environment is decided later in the project then making changes to business logic will be huge and critical.

Not to worry - Even if the requirement to have singleton across JVM’s is realized in later phases of the project, then tools like Terracotta or Oracle Coherence are good options to consider. These tools work on the concept of providing an in memory replication of objects across JVM's in effect providing you singleton view of the objects. One can also make use of any of the cluster-aware cache provider’s like Swarm Cache or JBoss Tree Cache.

Also, there is product called JGroups – allows developers to create reliable messaging (one-to-one or one-to-many) applications where reliability is a deployment issue, and does not have to be implemented by the application developer

Now a days Application server’s also provide some level of custom API’s to circumvent this problem.
JBoss has HASingleton Service which is meant to solve this problem. Click here to know more.

Web logic has the concept of Singleton Service – where only instance runs within the cluster and all clients will look up to the same instance.

WebSphere supports the concept of singleton across cluster in the WebSphere XD version of the application server as the partition facility – ObjectGrid or You could use the DistributedMap that is built into WAS.

No comments:

Post a Comment