Entity Framework Query Caching
Senior Application Development Manager, Deepak Malik, explains the Query caching behavior, performance implications, and extensibility provided in the latest release of Entity Framework
Caching entities or query results has been a recommended practice for application development to improve an application’s performance especially in data centric applications. It is very simple to understand. The first time a query is invoked, data is retrieved from the database and stored in memory before being returned. The compiled queries could be cached for the life of an app pool instance and provided to the Entity Framework for low latency performance of data access.
Before Entity Framework 6, the framework has a hard coded limit of 800 Compiled Queries and removes additional compiled queries after that threshold is exceeded which could cause significant performance issues. The EF QueryCacheManager will continually trim this cache every minute which can cause further locking issues under load.
These symptoms might occur frequently during load conditions when the number of objects in cache crosses the threshold and may impact interactive users. Operationally, the increased processing volumes may cause an application to use two to three times (2x-3x) more private memory then normally used. Under these conditions, there is more memory pressure and Garbage Collector (GC) overhead (which impacts all operations) which results in higher latency.
EF QueryCacheManager has these values hard coded as follows:
Figure 1- https://referencesource.microsoft.com/#System.Data.Entity/System/Data/Common/QueryCache/QueryCacheManager.cs
The creation of the QueryCacheManager is done with those constants:
private QueryCacheManager(int maximumSize, float loadFactor, int recycleMillis)
// Load hardcoded defaults
this._maxNumberOfEntries = maximumSize;
// set sweeping high mark trigger value
this._sweepingTriggerHighMark = (int)(_maxNumberOfEntries * loadFactor);
// Initialize Recycler
this._evictionTimer = new EvictionTimer(this, recycleMillis);
In EF 6.1.2 and later versions the parameters of the query cache can be configured only via the EntityFramework section in the application’s .config file. For example, an initial size of 10,000 entries and a clearing interval of two minutes can be specified like this:
<queryCache size=’10000′ cleaningIntervalInSeconds=’120’/>
Currently, there is no other method nor programmatic API to configure the cache during runtime with EF6.
Premier Support for Developers provides strategic technology guidance, critical support coverage, and a range of essential services to help teams optimize development lifecycles and improve software quality. Contact your Application Development Manager (ADM) or email us to learn more about what we can do for you.
Shouldn’t the current schema of the `entityFramework` configuration section define the `queryCache` element?