Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static variable scope in Servie Fabric Application

I am running my Service Fabric Stateful service in local with 3 partitions and 3 Replica.

The application has a public static variable and the value is initialized in the 'startup' class.

I was assuming that the scope of the static variable was per replica. But It seems the static variable scope is shared between all instance in a node.

ie. I am accessing the static variable from Partition 1 Primary Replica but its giving value from Partition 3 Secondary Replica where both replica reside in same node. The value of static variable seems to be overwritten in some order inside the same node.

What is the scope of Static variable inside Stateful service fabric application?

like image 563
Binu Vijayan Avatar asked Aug 22 '16 11:08

Binu Vijayan


1 Answers

Each replica of a stateful service or instance of a stateless service of the same service type is an instantiation of your StatefulService- or StatelessService-derived service class (the one with RunAsync) in the same process and in the same AppDomain. In other words, all the replicas of a service that are placed on the same node are just a bunch of .NET objects in the same process. So yes, a static variable will be seen across them. Static variables are not recommended. If you need "just one" of something, use a singleton pattern and scope singleton instances to service replicas/instances, which you'll have to do yourself with a look-up table.

like image 184
Vaclav Turecek Avatar answered Oct 23 '22 16:10

Vaclav Turecek