Spring Singleton in a Clustered Environment

As discussed in this article, it is not practical to use singleton in a clustered environment (due to multiple singleton objects in different JVMs), this should be true for single elements created by the Spring framework.

If this is correct, then we must be very careful about using the Spring framework to use singleton classes. Can you tell if this understanding is correct?

+4
source share
1 answer

This is not necessarily the case.

The problem is to use solitary singles in separate JVMs if they share a significant state. For example, a singleton that has stored and issued incremental identifiers would be very dangerous if two separate instances existed on two separate application servers that were part of the same application.

There is nothing in Spring that makes this more or less complicated. Your Spring beans (probably services) should aim to maintain as small a state as practical, as a good practice. If they need to split the state, then you have to solve this problem as you would with any other common state.

Many people use Spring in clustered environments and do not encounter any problems related to the above. I am one of them!

+6
source

All Articles