4

I am learning Spring, and I know that bean will be by default singleton in one application context.But what if we deploy the applications in distributed system? What will be the practical way to achieve singleton since every time a request comes in, it may be routed to a different machines with a different application context?

I have read Singleton in Cluster environment but the answer is about cache.

I guess we can do something like putting the singleton into a central place(like memcached) , and every time we need this bean and serialize and deserialize it from IO, Does this work? But, in my opinion, this will be cost a lot since some object is very "expensive" to transfer.

Thank you!

Community
  • 1
  • 1
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149

3 Answers3

3
  • Either your singleton is stateless: then you just re-create the same thing in each node, with no communication between nodes needed;

  • or your singleton is stateful: then you need to distribute its state. This is where memcached or any other of a slew of available distributed caches must be applied. You will still be re-creating the singleton itself in each node independently, but you'll make its internal state reside in the distributed cache.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • So, no "Singlton" at global level, but only "singleton-state" in central, am I right? – JaskeyLam Nov 10 '14 at 11:59
  • 1
    You must understand that the concept of a "singleton object" has quite a different meaning when applied to distributed systems. It is no longer an object in the Java sense. Rather, it is a complete mechanism which manages to present a *programming model* inside each node such that an object appears to be a cluster-wide singleton. The internal mechanism is basically the way I explain in the answer. – Marko Topolnik Nov 10 '14 at 12:29
0

You can set up your web/app server to make sessions "sticky": once a request is routed to a particular server, all requests in that session go to the same server.

The larger question is: why are you designing and implementing a distributed system this way? A singleton for all can't scale. There's no sense in clustering anything if you insist on this path.

A better solution would be stateless, immutable, functional. Create stateless REST services that model your system.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Hmm...I just think, since the singleton makes sense in single-node application, and when we cluster it, this singleton remains singleton should still make sense, doesn't it(please correct me if i am wrong)? So, the question comes to how to remain the object singleton in distributed level without sticky session. Thank you! – JaskeyLam Nov 10 '14 at 11:58
  • What makes you think singletons make sense in single node applications? Google has gone to great lengths to eliminate them from their code: https://code.google.com/p/google-singleton-detector/ – duffymo Nov 10 '14 at 14:00
  • well, then why singleton patterns is a common patterns and spring bean by default is singleton? There must be some reason that singleton makes sense, doesn't it? – JaskeyLam Nov 10 '14 at 14:05
  • Those reasons have diminished since the GoF book was published in 1995. It'd be voted off the island today.\ – duffymo Nov 10 '14 at 15:46
0

It depends, if you are going to use the Singleton instance just like a service and you won't store any global variable in it, you won't need to make it distributed.

In some cases that you require the distribution and, therefore, use a cache solution; you may try to optimize your implementation to store minimum data to make it distibuted less costly

Alper
  • 571
  • 5
  • 15