Quick tip for concurrency when using Entity Framework

Entity Framework does a great job at simplifying the way we interact with storage. Sometimes we need to use parallelism to speed up the completion of a task. This is usually done by means of multithreading (note: always prefer to use a new context per request, instead of a context per thread) or with async. In both cases we are attempting to scale our application, and in both cases Entity Framework has been tuned to scale linearly. However, some users might experience resource contention that limits the parallelism they are expecting when the Garbage Collector is not properly configured.

Whenever EF is used in a multithreaded scenario, or in any application that resembles a server system, make sure to enable server garbage collection. This is done via a simple setting in your application config file:

<?xmlversion="1.0" encoding="utf-8" ?>

<configuration>

        <runtime>

               <gcServer enabled="true" />

        </runtime>

</configuration>

This should decrease your thread contention and increase your throughput by up to 30% in CPU saturated scenarios.