Entity Framework Applications Using Distributed Cache

Entity Framework is an object-relational mapping engine that provides abstraction from underlying relational database and therefore greatly simplifies development. Because of these benefits, more and more data-centric and high transactional applications and services are developed with Entity Framework.

But, these high traffic applications are facing scalability problems. Although the application-tier level is scalable, their database or data storage cannot keep up with growing number of transactions being thrown at them.

This is where a distributed cache comes in because it allows you to cache data and reduce those expensive database trips that are causing scalability bottlenecks.  But, Entity Framework does not provide an out of the box solution that allows you to use distributed cache in your application. There are however two ways in which you can incorporate distributed cache into your Entity Framework application. One is to modify your Entity Framework application code and make direct API calls to the distributed cache. Second, is to use a distributed cache that has implemented a custom ADO.NET provider that incorporates caching behind-the-scenes.

Entity Framework has public provider model for ADO.NET providers where you can write providers for 3rd party databases. NCache has implemented a custom Entity Framework ADO.NET provider of its own through which it is able to make distributed cache calls to NCache API. This custom Entity Framework ADO.NET provider intercepts all the database query calls and puts the result-sets of these queries in a distributed cache. Then, NCache custom Entity Framework provider intercepts all subsequent query calls and simply returns the results from its distributed cache rather than making that expensive database trip. If result-set for a query does not exist in the distributed cache then query is executed against database and it’s result-set is then put in the distributed cache.

And, NCache custom Entity Framework provider also needs to ensure that data in the distributed cache is always consistent and synchronized with the database. And, for that NCache uses SqlCacheDependency provided in .NET. SqlCacheDependeny registers a SQL query with SQL Server so if any row in the dataset represented by this query is changed in the database, SQL Server throws a .NET event notification to NCache. NCache catches this .NET event and removes the corresponding result-set from the distributed cache.

Figure 1 shows how NCache Entity Framework Provider plugs into an Entity Framework application.

EF Caching
EF Caching

Figure 1 NCache Entity Framework Provider being used

 You can integrate NCache custom Entity Framework ADO.NET provider in your application in just four simple steps:

  1. Replace default provider: Replace your applications default provider with NCache Entity framework provider in app.config/web.config and .edmx file.
  2. Register NCache provider: Register your application in NCache Entity Framework config (efcaching.conf). In efcaching.config, you can easily specify log level and expiration policies etc. for your Entity Framework application.
  3. Run app in analysis mode: Run your application in analysis mode. In Analysis mode, NCache Entity Framework provider, logs Entity Framework queries executed by your application along with their frequency. Based on the logs you can scrutinize Entity Framework queries you want to cache.
  4. Run app normally: Switch to caching mode and run your application.

Hence, by using NCache Entity Framework caching provider you can easily achieve linear scalability without changing your Entity Framework application code.

So, download a fully working 60-day trial of NCache Enterprise and try it out for yourself.

Download NCache Trial | NCache Details

Avatar for Iqbal Khan


    1. Yes, It works with any Entity Framework connector driver such as SQL, Oracle, MySQL, PostreSQL etc. NCache caching provider acts in between the ADO.NET entity framework provider and the original data provider to cache the queries responses.

Leave a Reply

Your email address will not be published. Required fields are marked *