educative.io

Static Instances holding state

What do you mean by Static Instances in a Class? I cannot figure it out. You mean objects holding states? or you mean sessions? would you provide an example or dig a little deep into this? thanks.

1 Like

@Javier

In object-oriented programming, the instance variables hold object state in them. Static variables moreover hold state that spans across multiple objects. They generally hold state per classloader. Now, if the instance running that classloader goes down all the data is lost. Whatever data static variables hold it’s not application-wide.

For this reason, distributed memory like Redis, Memcache etc. is used to maintain a consistent state application-wide. When writing applications for distributed systems it’s a good practice to avoid using static instances in the class.

These resources will give you further insights into this:

Functional Programming Pdf

Why functional programming is advantageous for distributed systems - Quora

1 Like

This also confused me. I feel like state is what you mean rather than static? All state is lost if a server goes down, not just static variables. (Presuming we’re talking about the Scalability chapter)

I was about to ask the same exact question. In general, I would expect something like avoiding stateful software components in distributed systems to avoid state inconsistency. Static variables and properties might not be a bottleneck here all the time since they do not hold a global state of the application. I might expect you to say something like “avoiding using a decentralized session management since they can screw up scalability” as a user might authenticate in one instance of the app and continue on another instance due to the load balancing techniques so they might be vaulnerable to un-authinticated behaviours.

@Mohammed_Abdoh static variables don’t hold the global state of the app still they can cause inconsistency in the system. Also, you are right, decentralized session management provides a more high-level view of an inconsistent system, at the component level. In the lesson, I specifically brought up static variables.