Singleton in a Cluster Environment

What is the best strategy for reorganizing a Singleton object into a cluster environment?

We use Singleton to cache some user information from the database. It is mainly read-only, but updated when a specific event occurs.

Now our application needs to be deployed in a clustered environment. By definition, each JVM will have its own instance of Singleton. Thus, the cache may not be synchronized between the JVM when the update event occurs on one node and its cache is updated.

What's the best way to sync a cache?

Thank.

Editing. The cache is mainly used to provide an autocomplete list (performance considerations) for the user interface, and we use Websphere. Therefore, any advice related to Websphere is welcome.

+42
java singleton cluster-computing websphere
Jul 28 '09 at 13:31
source share
9 answers

The simplest approaches:

  • Add an expiration timer to cache-cache-cache-cache, so that everyone clears the cache so often and saturated calls retrieve updated data from a source (for example, a database)

  • Implement a notification mechanism for the cache using something like a JMS / tibRV theme. Receive each cache instance to subscribe and respond to any change messages posted on this topic.

+9
Jul 28 '09 at 13:54
source share

Replace cache-cache-cache with distributed cache.

One such cache might be JBoss Infinispan , but I'm sure there are other common caching and mesh technologies, including commercial ones, which are probably more mature at this point.

For singleton objects in general, I'm not sure. I think that I will try not to have singles in the first place.

+16
Jul 28 '09 at 13:35
source share

You can use the DistributedMap built into WAS.

-Rick

+8
Aug 04 '09 at 21:57
source share

Or something like memcached

http://www.danga.com/memcached/

What is memcached? memcached is a high-performance distributed memory object caching system, common but intended for use in accelerating dynamic web applications by facilitating database loading.

Danga Interactive developed memcached to increase the speed of LiveJournal.com, a site that was already making 20 million + dynamic page views per day for 1 million users with a bunch of web servers and a bunch of database servers. memcached dropped database loading for almost nothing, providing faster page load times for users, more efficient use of resources, and faster access to databases on memcache slip.

+4
Jul 28 '09 at 13:55
source share

If possible, use the support of your application server for this, if possible (some of them are, and some are not). For example, we use JBoss support for "HA Singleton", which is a service that runs only on the main cluster node. This is not ideal (you have to deal with the case when his brain farts from time to time), but he is good enough.

Otherwise, you can create something using JGroups, which provides automatic discovery and coordination of the cluster node, but this is not trivial.

As a last resort, you can use database locks to manage single-user clusters, but this is seriously fragile. Not recommended.

As an alternative to a singleton cluster, you can use a distributed cache. I recommend JBossCache (no JBoss application server is required to run) or EhCache (which now provides a distribution mechanism). You will have to reorganize your cache to work in a distributed way (it wonโ€™t work magically), but it will probably be a better solution than a cluster singleton.

+1
Jul 28 '09 at 13:38
source share

I'm with Mr. West Hansen on this, moving as far from the singleton as possible. After I was surpassed by a nightmare that is SAAJ and JAXP, and with compatible versions working on JBoss, I ended up singles and factories. A factory is not required to create a SOAP message.

Ok, angry with memcache or something like that? What connection do you need for your cache? Is it bad if it is TOTAL outdated or there is some flexibility in how obsolete data can get?

+1
Jul 28 '09 at 13:41
source share

There are several ways to deal with this, depending on 1) how the data data is data, and 2) every time that all instances must have the same values โ€‹โ€‹all the time.

If you only need data that is reasonably suitable for the data, but it is not necessary for each JVM to have the corresponding data, you can simply update all jvm data in the same schedule (for example, every 30 seconds).

If the update should happen at about the same time, you can send one jvm a message to everyone else, saying "time to update it"

If the same information is always required for each jvm, you need to synchronize when the wizard says โ€œupdate nowโ€, all caches block any new requests, update and tell the wizard that they are done. When the master receives a response from each member of the cluster, it sends another message that says to continue.

+1
Jul 28 '09 at 13:59
source share

I faced a similar situation, but I use Oracle WebLogic and Coherence.

I am working on a web application that uses a hash map with cached data read from a database (text to display on web form labels). For this, the developers used a singleton instance, where they stored all this information. This worked well in a single server environment, but now we want to switch to a cluster solution, and I ran into this problem with this singleton instance.

From what I have read so far, this is the best solution to accomplish what I want . I hope this helps you in your problem too.

+1
Feb 25 '10 at 11:16
source share

There are products for distribution in the memory cache (for example, memcache) that can help in this situation.

The best solution, if possible, could be to prevent singletones from being single, but the application would allow separate instances (let's say everyone recognizes when they need to be updated), but not that they should be synchronized via the JVM, which can turn your cache into a bottleneck.

0
Jul 28 '09 at 13:59
source share



All Articles