<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mohsen Sheikhzadeh</title>
    <description>The latest articles on DEV Community by Mohsen Sheikhzadeh (@aramdata).</description>
    <link>https://dev.to/aramdata</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F633828%2F5b9b44a1-82f6-49a8-8fd3-6e8d99bc5221.jpg</url>
      <title>DEV Community: Mohsen Sheikhzadeh</title>
      <link>https://dev.to/aramdata</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aramdata"/>
    <language>en</language>
    <item>
      <title>Boost Your .NET App's Performance: Optimizing EF Core Postgres Connection Pooling</title>
      <dc:creator>Mohsen Sheikhzadeh</dc:creator>
      <pubDate>Thu, 22 Feb 2024 19:10:57 +0000</pubDate>
      <link>https://dev.to/aramdata/boost-your-net-apps-performance-optimizing-ef-core-postgres-connection-pooling-3m34</link>
      <guid>https://dev.to/aramdata/boost-your-net-apps-performance-optimizing-ef-core-postgres-connection-pooling-3m34</guid>
      <description>&lt;p&gt;Entity Framework Core (EF Core) is an object-relational mapper (ORM) that simplifies data access in .NET applications. Connection pooling is a technique used to improve the performance of database interactions by reusing existing connections instead of creating new ones for each request. This can significantly reduce overhead and improve application scalability.&lt;br&gt;
How does EF Core connection pooling work with Postgres?&lt;br&gt;
EF Core uses the Npgsql provider for interacting with Postgres databases. Npgsql implements its own connection pool that can be configured to meet the specific needs of your application. Some key configuration options include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Minimum pool size: The minimum number of connections to keep open in the pool, even when idle.&lt;/li&gt;
&lt;li&gt;Maximum pool size: The maximum number of connections allowed in the pool.&lt;/li&gt;
&lt;li&gt;Connection lifetime: The maximum time a connection can remain idle in the pool before being closed.
Benefits of using EF Core connection pooling with Postgres&lt;/li&gt;
&lt;li&gt;Improved performance: By reusing connections, EF Core can avoid the overhead of creating new connections for each request. This can lead to significant performance improvements, especially for applications that make many database calls.&lt;/li&gt;
&lt;li&gt;Reduced database load: By reusing connections, EF Core reduces the number of connections that need to be established and closed to the database server. This can help to reduce the load on the database server and improve overall system scalability.&lt;/li&gt;
&lt;li&gt;Simplified code: EF Core handles connection pooling automatically, so you don't need to write any code to manage connections yourself.
Considerations for using EF Core connection pooling with Postgres&lt;/li&gt;
&lt;li&gt;Connection pool size: It is important to choose the right connection pool size for your application. Too small a pool can lead to performance bottlenecks, while too large a pool can waste resources.&lt;/li&gt;
&lt;li&gt;Connection lifetime: The connection lifetime should be set to a value that is long enough to be useful, but not so long that idle connections are wasting resources.&lt;/li&gt;
&lt;li&gt;Monitoring: It is important to monitor your connection pool usage to ensure that it is performing as expected. You can use tools like pg_stat_activity 
to view information about the connections in your pool.
By following these tips, you can effectively use EF Core connection pooling with Postgres to improve the performance, scalability, and manageability of your .NET applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In ASP.NET Core applications, dependency injection is a common practice for managing DbContext instances. AddDbContextPool registers DbContext as a pooled service, improving performance by reusing connections:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Service Registration
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddDbContextPool&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MyContext&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;UseNpgsql&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;connectionString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Usage in Controllers:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyController&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;MyContext&lt;/span&gt; &lt;span class="n"&gt;_context&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MyController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MyContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_context&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IActionResult&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;MyAction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MyEntities&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToListAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Improved Performance: Reusing connections reduces database connection overhead.&lt;/li&gt;
&lt;li&gt;Scalability: The pool can handle multiple requests concurrently.&lt;/li&gt;
&lt;li&gt;Thread Safety: DbContext instances are thread-safe within the scope.
Remember:&lt;/li&gt;
&lt;li&gt;Avoid sharing DbContext instances across request scopes.&lt;/li&gt;
&lt;li&gt;Consider connection pool configuration for optimal performance.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>postgressql</category>
      <category>efcore</category>
    </item>
  </channel>
</rss>
