Singleton Design Pattern in a clustered Environment !!!

When to Use:

We can use Singleton pattern while creating objects of thread pools, caches etc to avoid wasting resources. If you want to store global information like item price of products etc. It’s now a anti-pattern and you should avoid it by different substitutes.

Important Note: It has now become Anti-design pattern and we should avaoid by using follwoing techniques:

1. Dependency Injection

2. Using Factory design Pattern

3. Using Enum class etc. (Introduced in Java 1.5)

// Enum singleton - the preferred approach 

public enum MySingleton{
 INSTANCE Consolas, Monaco, monospace; 

Singleton Design Pattern in a clustered Environment [Source:

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 multiple 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.

  • Application server’s also provide some level of custom API’s to circumvent this problem.
  •  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 Swarm Cache or JBoss TreeCache should work as cache entries are singletons and clustering is built in.
  • Also, there is product called JGroups – which uses mulch-cast communication (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.
  • JBoss has has Singleton Service ( based on MBeans) which is meant to solve this problem. Check here and here
  • Weblogic 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


Java Architect

2 thoughts on “Singleton Design Pattern in a clustered Environment !!!

  1. I am looking to achieve same for .NET on using Elastic Load Balancing on Amazon. Please suggest any tools/framework to keep certain logic Global.

    Is it good practice to keep a separate server for shared resources? How do we handle fail over of those resources?

    1. I have not worked on .net. I think you can always use C# to implement similar logic. I am not sure which kind of logic you are trying to save? on .Net?

      Most of people use to keep shared resources on a separate server, it depends which kind of data you want to store. If it’s static like images, videos, static html, you can store on Web server. In normal practice, we can use at least instances of App server and web server to handle failover. You can persist session info in your database and share same session from other backup app server if one server got failed. For shared content we should also use at least two web server and sync for any update.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: