I have a static class with a static dictionary to keep track of some stats. Is this approach viable in a single and multi server environment?
4 Answers
Not in a multi-server environment. Unless the dictionary isn't the .Net dictionary, but one that works against a database or some other outside storage. Also in a single server environment you should remember the Dictionary will be emptied with every IIS refresh.

- 67,283
- 14
- 105
- 142
-
Is there any way to keep a static variable in-sync across a multi-server environment? Would a singleton object work? – chobo Jan 21 '12 at 00:53
-
3@chobo you need to find a common place to keep the data. The data you're holding in static Dictionaries is just in the memory of the server it's on. Which design pattern you use doesn't matter. – Yuriy Faktorovich Jan 21 '12 at 00:54
-
1It's multiple separate computers. No, you won't have the same variable across all of them. `Session` "variables" would work, though. – John Saunders Jan 21 '12 at 00:55
-
@JohnSaunders assuming the data is at the session level, and the Session isn't in-memory or he's using sticky sessions. – Yuriy Faktorovich Jan 21 '12 at 00:58
-
I think based on the answers it's unanimous that I would need some sort of data persistence (database) to keep the data in sync.o. – chobo Jan 21 '12 at 06:14
No, a static variable in memory in one server won't be visible or synchronized with other servers.
Since you mention "keeping track of stats", what many multi-instance server farms end up doing is post-processing locally gathered data into an aggregate view of activity across all the servers. Each server accumulates stats in memory for a short while (a few minutes), then writes the data from memory to a log file on the local file system. Every few hours or daily a batch process copies the log files from each server in the farm to a central location and merges all the log records together to form one collection of data. One useful byproduct of this might be consolidating user activity across multiple servers if a user's web requests were served by different servers in the same farm (load balancing).
Depending on the volume of data, this batch processing of log data may itself require quite a bit of computational power, with multiple machines crunching different subsets of the server logs, and then those intermediates getting reduced again.
Google Sawzall is an example of a very large scale distributed data processing system. I believe Sawzall is (or was) used at Google to process server access logs to detect fraudulent ad click patterns.

- 35,318
- 5
- 75
- 119
No, it's not viable in a web farm multi-server environment.
The State Mode options are:
InProc mode, which stores session state in memory on the Web server. This is the default.
StateServer mode, which stores session state in a separate process called the ASP.NET state service. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
SQLServer mode stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
Custom mode, which enables you to specify a custom storage provider.
Off (disables session state)

- 1
- 1

- 295,962
- 43
- 465
- 541
-
The static variable will also store data from non-authenticated users, so I can't use a Session for that. – chobo Jan 21 '12 at 06:12
Normally, this is what you would use Application state for.

- 71,308
- 16
- 93
- 135
-
Ya, just read up on it and it seems to be exactly the same thing as I was doing. I wasn't aware that they had a class for that. – chobo Jan 21 '12 at 18:56