Kiksen
Kiksen

Reputation: 1767

Configure SQL Connection pool in EntityFramework Core

TLDR: Is there a way to control connection pools in AspNet Core / EF CORE Sql connections. I need something similar to this:

pool: {
    min: 0,
    max: 1,
    idle: 10000
}  

The Problem:

I have a serverless api built with AspNetCore 3.1 using EF core for data access. All works fine when the server is not under heavy load. But when we get just moderate amount of requests we run out of connections to the SQL server. Because they are not released. The database is under no stress. CPU / RAM / IO are all still "sleeping" and having no trouble keeping up with the requests.

After spending some time reading up on the problem I found 2 solutions

I don't really like options A since we already have a big instance that already now is to big for it's purpose. Don't really need more CPU/RAM wasted

So my only real options is trying to gain access to the connection pool.

My Google mojo hasn't really gotten me far and I haven't been able to find anything in the public spec. All I find is oldschool ADO.NET from blogs posts ages ago. And some few blog posts that just states the connection pooling in EF Core is magic and don't worry about it. I never have worried about it until now in a serverless enviroment.

Would really hate if we had to go away from EF Core, since it would be a major rewrite of the app.

Upvotes: 6

Views: 8100

Answers (1)

matt
matt

Reputation: 183

If you're using SQL server, then that needs to be handled by the Min Pool Size, Max Pool Size, and Connection Lifetime parameters in your connection string. I'm not seeing a direct way to configure idle timeout, but the connection lifetime should provide a good proxy.

If you're using postgres via npgsql, their connection string parameters would be Minimum Pool Size, Maximum Pool Size, and Connection Idle Lifetime.

I'd expect other providers to have similar options, but it looks like connection pooling isn't handled at the general EFCore-level.

Upvotes: 8

Related Questions