0

We have an ASP.NET application with Microsoft Azure Service Fabric Micro services. There is an event table in SQL Server database which stores events and the corresponding times when they are to be executed.

We have another Microsoft Azure Reliable Actor Service that calls an API in one of the Micro Services periodically every 10 minutes to read the events that are scheduled for the next 10 minutes.

So, in the first call, it will read the events up to a specific time. In the next call, we need to read from where the last call ended reading. That is, we need to preserve the end times of each read for the subsequent reads.

In order to do this, we used a static variable in the API to store the end time in each read. In the next read, we retrieve the value from this static variable and use it as our start time for the read. That is on each read, this variable will have the end time of the last read.

This worked perfectly when we run it locally. But after the deployment of the micro service, the variable started returning incorrect values. It will return the correct value most of the times, but sometimes (like 1 in 10 reads), it will return the value stored from say 5 or 6 calls back.

I hear this could be due to the fact that IIS Express has multiple worker processes and each process may have different copies of the static variables. If this is the case, how can I avoid this and achieve the expected behavior? If not, what could be the reason and the solution for it?

Please note that this variable is for the whole application and not user session specific. I want the variable value to be preserved as long as the micro service is running.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nimish David Mathew
  • 2,958
  • 6
  • 29
  • 45
  • Did you declare the static variable to be [`volatile`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/volatile)? See [When should the volatile keyword be used in C#?](https://stackoverflow.com/q/72275/3744182). But see also [c# - Volatile keyword usage vs lock](https://stackoverflow.com/q/19382705). – dbc Dec 31 '17 at 07:44
  • This api with the static variable, on how many nodes is it running? Is it a stateless service? Why did you use a static variable as opposed to store it using a statefull service? – Peter Bons Dec 31 '17 at 12:30
  • @dbc Tried volatile. But it doesn't seem to work. – Nimish David Mathew Jan 04 '18 at 09:18
  • @PeterBons It is running on 3 nodes. It is stateless. "Why did you use a static variable as opposed to store it using a statefull service?" - Can you elaborate on this? – Nimish David Mathew Jan 04 '18 at 09:21

0 Answers0