1

My ASP.NET MVC 4 project is using EF5 code-first, and some of the domain objects contain non- persisted counter properties which are updated according to incoming requests. These requests come very frequently and s scenario in which multiple request sessions are modifying these counters is quite probable.

My question is, is there a best practice, not necessarily related to ASP.NET or to EF, to handle this scenario? I think (but I'm not sure) that for the sake of this discussion, we can treat the domain objects as simple POCOs (which they are).

EDIT: As requested, following is the actual scenario:

The system is a subscriber and content management system. Peer servers are issuing requests which my system either authorizes or denies. Authorized requests result in opening sessions in peer servers. When a session is closed in the peer server, it issues a request notifying that the session has been closed.

My system needs to provide statistics - for example, the number of currently open sessions for each content item (one of the domain entities) - and provide real-time figures as well as per-minute, hourly, daily, weekly etc. figures.

These figures can't be extracted by means of querying the database due to performance issues, so I've decided to implement the basic counters in-memory, persist them every minute to the database and take the hourly, daily etc. figures from there.

The issue above results from the fact that each peer server request updates these "counters".

I hope it's clearer now.

3
  • 1
    People will be able to help you better if you ask about what you're trying to achieve. Shared counters in EF entities may not be the best approach. If you need this to scale you'll probably have to shard the counters anyway. Commented Dec 10, 2012 at 21:44
  • 2
    You say the counters are not persisted, but you allude to them being maintained across multiple incoming requests. Can you define what you mean by incoming request? Persistence has many forms, and the answer to your question requires more details about the problem. Commented Dec 10, 2012 at 21:52
  • 1
    Might be worth investigating the ConcurrentDictionary Commented Dec 10, 2012 at 22:14

2 Answers 2

1

Sounds like your scenario still requires a solid persistence strategy.

Your counter objects can be persisted to the HttpRuntime.Cache. Dan Watson has an exceptional writeup here: http://www.dotnetguy.co.uk/post/2010/03/29/c-httpruntime-simple-cache/

Be sure to use CacheItemPriority.NotRemovable to ensure that it maintains state during memory reclamation. The cache would be maintained within the scope of the app domain. You could retrieve and update counters (its thread safe!) in the cache and query its status from presumably a stats page or some other option. However if the data needs to be persisted beyond the scope of runtime then the strategy you're already using is sufficient.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure how the mechanisms described in these article address the concurrency issues described above.
I updated my answer to focus on utilizing the cache as a solution.
1

Actually I think you have no need to wary about performance to much before you do not have enough info from tests and profiler tools.

But if you're working with EF, so you have deals with DataContext, which is the Unit Of Work pattern implementation described by Martin Fowler in his book. The main idea of such a pattern is reducing amount of requesting to database and operating the data in-memory as much as possible until you do not commit all your changes. So my short advice will be just using your EF entities in standard way, but not committing changes each time when data updates, but with the some intervals, for example after the 100 changes, storing data between requests in Session, Application session, Cache or somewhere else. The only thing you should care about is you using proper DataContext object each time, and do not forget disposed it when you no need it any more.

1 Comment

I'm adopting the approach you describe in general, but I don't believe EF can be used for Sessions. This table will will hold millions of records and will have jobs working on it in the background, so I'm afraid that working directly with the database, together with some some form of caching (not sure which yet), will be required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.