EF Core AddAsync and SaveChangesAsync slightly slower after application is idle for a long time
I am currently doing benchmark for my APIs and I noticed a bit delay on the first save operation using EF Core. The situation is after some period of inactivity (let's say 18 hours), the first save operation would take 1.5 seconds to execute, and subsequently, it is 100 ms at most. Below is a snippet of the code,
public async Task SaveLog(Log log_item)
{
// Set created and updated date/time
log_item.created = DateTime.UtcNow;
log_item.updated = DateTime.UtcNow;
if (!(log_item.Month % 2 == 0))
// Odd
{
var log_item_odd = log_item.ConvertLogToOdd();
await _dbContext.Log_Odd.AddAsync(log_item_odd);
}
else
// Even
{
var log_item_even = log_item.ConvertLogToEven();
await _dbContext.Log_Even.AddAsync(log_item_even);
}
await _dbContext.SaveChangesAsync();
}
Below are the execution time for this operation, as well as subsequent updates performed on the log that I captured, in ms.
[2018-12-29 06:25:00.199] [ADD_LOG] SaveLog 1556
[2018-12-29 06:25:01.056] [UPDATE_LOG] UpdateLog 362
[2018-12-29 06:27:13.652] [ADD_LOG] SaveLog 4
[2018-12-29 06:27:13.886] [UPDATE_LOG] UpdateLog 9
[2018-12-29 06:29:01.633] [ADD_LOG] SaveLog 4
[2018-12-29 06:29:01.846] [UPDATE_LOG] UpdateLog 4
[2018-12-29 06:56:21.544] [ADD_LOG] SaveLog 53
[2018-12-29 06:56:21.996] [UPDATE_LOG] UpdateLog 5
[2018-12-29 07:11:58.813] [ADD_LOG] SaveLog 94
[2018-12-29 07:11:59.051] [UPDATE_LOG] UpdateLog 5
[2018-12-29 07:16:12.241] [ADD_LOG] SaveLog 3
[2018-12-29 07:16:12.639] [UPDATE_LOG] UpdateLog 4
I could not find anything related to EF Core requiring some time to boot up after some period of inactivity, is there an explanation for this? *I am using PostgreSQL as the database.
Any help is appreciated, thank you.
c# entity-framework-core
add a comment |
I am currently doing benchmark for my APIs and I noticed a bit delay on the first save operation using EF Core. The situation is after some period of inactivity (let's say 18 hours), the first save operation would take 1.5 seconds to execute, and subsequently, it is 100 ms at most. Below is a snippet of the code,
public async Task SaveLog(Log log_item)
{
// Set created and updated date/time
log_item.created = DateTime.UtcNow;
log_item.updated = DateTime.UtcNow;
if (!(log_item.Month % 2 == 0))
// Odd
{
var log_item_odd = log_item.ConvertLogToOdd();
await _dbContext.Log_Odd.AddAsync(log_item_odd);
}
else
// Even
{
var log_item_even = log_item.ConvertLogToEven();
await _dbContext.Log_Even.AddAsync(log_item_even);
}
await _dbContext.SaveChangesAsync();
}
Below are the execution time for this operation, as well as subsequent updates performed on the log that I captured, in ms.
[2018-12-29 06:25:00.199] [ADD_LOG] SaveLog 1556
[2018-12-29 06:25:01.056] [UPDATE_LOG] UpdateLog 362
[2018-12-29 06:27:13.652] [ADD_LOG] SaveLog 4
[2018-12-29 06:27:13.886] [UPDATE_LOG] UpdateLog 9
[2018-12-29 06:29:01.633] [ADD_LOG] SaveLog 4
[2018-12-29 06:29:01.846] [UPDATE_LOG] UpdateLog 4
[2018-12-29 06:56:21.544] [ADD_LOG] SaveLog 53
[2018-12-29 06:56:21.996] [UPDATE_LOG] UpdateLog 5
[2018-12-29 07:11:58.813] [ADD_LOG] SaveLog 94
[2018-12-29 07:11:59.051] [UPDATE_LOG] UpdateLog 5
[2018-12-29 07:16:12.241] [ADD_LOG] SaveLog 3
[2018-12-29 07:16:12.639] [UPDATE_LOG] UpdateLog 4
I could not find anything related to EF Core requiring some time to boot up after some period of inactivity, is there an explanation for this? *I am using PostgreSQL as the database.
Any help is appreciated, thank you.
c# entity-framework-core
1
The connection pooler removes a connection from the pool after it has been idle for approximately 4-8 minutes, or if the pooler detects that the connection with the server has been severed. ADO.NET Connection Pooling
– Reza Aghaei
Dec 29 '18 at 8:45
The database connection is not continuous connection for long time, after few minutes the connection has been refused and application needs to connect to database again.
– isaeid
Dec 29 '18 at 9:07
not only the connection has to be re-opened after a prolonged idle time, possibly the context cache is not valid anymore which would require a re-computation of the query (+maybe query plan on dbms side), possibly even a re-run of the model creation+check step.
– DevilSuichiro
Dec 29 '18 at 9:44
add a comment |
I am currently doing benchmark for my APIs and I noticed a bit delay on the first save operation using EF Core. The situation is after some period of inactivity (let's say 18 hours), the first save operation would take 1.5 seconds to execute, and subsequently, it is 100 ms at most. Below is a snippet of the code,
public async Task SaveLog(Log log_item)
{
// Set created and updated date/time
log_item.created = DateTime.UtcNow;
log_item.updated = DateTime.UtcNow;
if (!(log_item.Month % 2 == 0))
// Odd
{
var log_item_odd = log_item.ConvertLogToOdd();
await _dbContext.Log_Odd.AddAsync(log_item_odd);
}
else
// Even
{
var log_item_even = log_item.ConvertLogToEven();
await _dbContext.Log_Even.AddAsync(log_item_even);
}
await _dbContext.SaveChangesAsync();
}
Below are the execution time for this operation, as well as subsequent updates performed on the log that I captured, in ms.
[2018-12-29 06:25:00.199] [ADD_LOG] SaveLog 1556
[2018-12-29 06:25:01.056] [UPDATE_LOG] UpdateLog 362
[2018-12-29 06:27:13.652] [ADD_LOG] SaveLog 4
[2018-12-29 06:27:13.886] [UPDATE_LOG] UpdateLog 9
[2018-12-29 06:29:01.633] [ADD_LOG] SaveLog 4
[2018-12-29 06:29:01.846] [UPDATE_LOG] UpdateLog 4
[2018-12-29 06:56:21.544] [ADD_LOG] SaveLog 53
[2018-12-29 06:56:21.996] [UPDATE_LOG] UpdateLog 5
[2018-12-29 07:11:58.813] [ADD_LOG] SaveLog 94
[2018-12-29 07:11:59.051] [UPDATE_LOG] UpdateLog 5
[2018-12-29 07:16:12.241] [ADD_LOG] SaveLog 3
[2018-12-29 07:16:12.639] [UPDATE_LOG] UpdateLog 4
I could not find anything related to EF Core requiring some time to boot up after some period of inactivity, is there an explanation for this? *I am using PostgreSQL as the database.
Any help is appreciated, thank you.
c# entity-framework-core
I am currently doing benchmark for my APIs and I noticed a bit delay on the first save operation using EF Core. The situation is after some period of inactivity (let's say 18 hours), the first save operation would take 1.5 seconds to execute, and subsequently, it is 100 ms at most. Below is a snippet of the code,
public async Task SaveLog(Log log_item)
{
// Set created and updated date/time
log_item.created = DateTime.UtcNow;
log_item.updated = DateTime.UtcNow;
if (!(log_item.Month % 2 == 0))
// Odd
{
var log_item_odd = log_item.ConvertLogToOdd();
await _dbContext.Log_Odd.AddAsync(log_item_odd);
}
else
// Even
{
var log_item_even = log_item.ConvertLogToEven();
await _dbContext.Log_Even.AddAsync(log_item_even);
}
await _dbContext.SaveChangesAsync();
}
Below are the execution time for this operation, as well as subsequent updates performed on the log that I captured, in ms.
[2018-12-29 06:25:00.199] [ADD_LOG] SaveLog 1556
[2018-12-29 06:25:01.056] [UPDATE_LOG] UpdateLog 362
[2018-12-29 06:27:13.652] [ADD_LOG] SaveLog 4
[2018-12-29 06:27:13.886] [UPDATE_LOG] UpdateLog 9
[2018-12-29 06:29:01.633] [ADD_LOG] SaveLog 4
[2018-12-29 06:29:01.846] [UPDATE_LOG] UpdateLog 4
[2018-12-29 06:56:21.544] [ADD_LOG] SaveLog 53
[2018-12-29 06:56:21.996] [UPDATE_LOG] UpdateLog 5
[2018-12-29 07:11:58.813] [ADD_LOG] SaveLog 94
[2018-12-29 07:11:59.051] [UPDATE_LOG] UpdateLog 5
[2018-12-29 07:16:12.241] [ADD_LOG] SaveLog 3
[2018-12-29 07:16:12.639] [UPDATE_LOG] UpdateLog 4
I could not find anything related to EF Core requiring some time to boot up after some period of inactivity, is there an explanation for this? *I am using PostgreSQL as the database.
Any help is appreciated, thank you.
c# entity-framework-core
c# entity-framework-core
asked Dec 29 '18 at 8:41
YtanYtan
99210
99210
1
The connection pooler removes a connection from the pool after it has been idle for approximately 4-8 minutes, or if the pooler detects that the connection with the server has been severed. ADO.NET Connection Pooling
– Reza Aghaei
Dec 29 '18 at 8:45
The database connection is not continuous connection for long time, after few minutes the connection has been refused and application needs to connect to database again.
– isaeid
Dec 29 '18 at 9:07
not only the connection has to be re-opened after a prolonged idle time, possibly the context cache is not valid anymore which would require a re-computation of the query (+maybe query plan on dbms side), possibly even a re-run of the model creation+check step.
– DevilSuichiro
Dec 29 '18 at 9:44
add a comment |
1
The connection pooler removes a connection from the pool after it has been idle for approximately 4-8 minutes, or if the pooler detects that the connection with the server has been severed. ADO.NET Connection Pooling
– Reza Aghaei
Dec 29 '18 at 8:45
The database connection is not continuous connection for long time, after few minutes the connection has been refused and application needs to connect to database again.
– isaeid
Dec 29 '18 at 9:07
not only the connection has to be re-opened after a prolonged idle time, possibly the context cache is not valid anymore which would require a re-computation of the query (+maybe query plan on dbms side), possibly even a re-run of the model creation+check step.
– DevilSuichiro
Dec 29 '18 at 9:44
1
1
The connection pooler removes a connection from the pool after it has been idle for approximately 4-8 minutes, or if the pooler detects that the connection with the server has been severed. ADO.NET Connection Pooling
– Reza Aghaei
Dec 29 '18 at 8:45
The connection pooler removes a connection from the pool after it has been idle for approximately 4-8 minutes, or if the pooler detects that the connection with the server has been severed. ADO.NET Connection Pooling
– Reza Aghaei
Dec 29 '18 at 8:45
The database connection is not continuous connection for long time, after few minutes the connection has been refused and application needs to connect to database again.
– isaeid
Dec 29 '18 at 9:07
The database connection is not continuous connection for long time, after few minutes the connection has been refused and application needs to connect to database again.
– isaeid
Dec 29 '18 at 9:07
not only the connection has to be re-opened after a prolonged idle time, possibly the context cache is not valid anymore which would require a re-computation of the query (+maybe query plan on dbms side), possibly even a re-run of the model creation+check step.
– DevilSuichiro
Dec 29 '18 at 9:44
not only the connection has to be re-opened after a prolonged idle time, possibly the context cache is not valid anymore which would require a re-computation of the query (+maybe query plan on dbms side), possibly even a re-run of the model creation+check step.
– DevilSuichiro
Dec 29 '18 at 9:44
add a comment |
1 Answer
1
active
oldest
votes
Usually all providers have some implementations to prune idle connections after a period of idle time.
The first command takes a bit more time to create and open the connection. The next commands will use the pooled connection and they are faster. Then after being idle for a specific amount of time, providers prune the pool and close idle connections. Then the next command after idle time, needs to open a connection which again takes time.
For example for Npgsql
, there is a PruneIdleConnectors
in ConnectorPool
which does the clean up based on the value of NpgsqlConnectionStringBuilder.ConnectionIdleLifetime
which is 5 minutes by default. Here are related pooling parameters:
Connection Idle Lifetime: The time (in seconds) to wait before closing idle connections in the pool if the count of all connections exceeds MinPoolSize. Since 3.1 only. 300 seconds by default.
Connection Pruning Interval: How many seconds the pool waits before attempting to prune idle connections that are beyond idle lifetime (see ConnectionIdleLifetime). Since 3.1 only. 10 seconds by default.
For ADO.NET SqlConnection
pooling this time is approximately 4-8 minutes:
The connection pooler removes a connection from the pool after it has been idle for approximately 4-8 minutes, or if the pooler detects that the connection with the server has been severed.
Note: It's already mentioned in comments as well, there are some other factors which may affect first execution time, including EF Context Pooling and Query Execution Plan cache in DBMS. But usually the most time-consuming of them is re-opening connection.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968034%2fef-core-addasync-and-savechangesasync-slightly-slower-after-application-is-idle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Usually all providers have some implementations to prune idle connections after a period of idle time.
The first command takes a bit more time to create and open the connection. The next commands will use the pooled connection and they are faster. Then after being idle for a specific amount of time, providers prune the pool and close idle connections. Then the next command after idle time, needs to open a connection which again takes time.
For example for Npgsql
, there is a PruneIdleConnectors
in ConnectorPool
which does the clean up based on the value of NpgsqlConnectionStringBuilder.ConnectionIdleLifetime
which is 5 minutes by default. Here are related pooling parameters:
Connection Idle Lifetime: The time (in seconds) to wait before closing idle connections in the pool if the count of all connections exceeds MinPoolSize. Since 3.1 only. 300 seconds by default.
Connection Pruning Interval: How many seconds the pool waits before attempting to prune idle connections that are beyond idle lifetime (see ConnectionIdleLifetime). Since 3.1 only. 10 seconds by default.
For ADO.NET SqlConnection
pooling this time is approximately 4-8 minutes:
The connection pooler removes a connection from the pool after it has been idle for approximately 4-8 minutes, or if the pooler detects that the connection with the server has been severed.
Note: It's already mentioned in comments as well, there are some other factors which may affect first execution time, including EF Context Pooling and Query Execution Plan cache in DBMS. But usually the most time-consuming of them is re-opening connection.
add a comment |
Usually all providers have some implementations to prune idle connections after a period of idle time.
The first command takes a bit more time to create and open the connection. The next commands will use the pooled connection and they are faster. Then after being idle for a specific amount of time, providers prune the pool and close idle connections. Then the next command after idle time, needs to open a connection which again takes time.
For example for Npgsql
, there is a PruneIdleConnectors
in ConnectorPool
which does the clean up based on the value of NpgsqlConnectionStringBuilder.ConnectionIdleLifetime
which is 5 minutes by default. Here are related pooling parameters:
Connection Idle Lifetime: The time (in seconds) to wait before closing idle connections in the pool if the count of all connections exceeds MinPoolSize. Since 3.1 only. 300 seconds by default.
Connection Pruning Interval: How many seconds the pool waits before attempting to prune idle connections that are beyond idle lifetime (see ConnectionIdleLifetime). Since 3.1 only. 10 seconds by default.
For ADO.NET SqlConnection
pooling this time is approximately 4-8 minutes:
The connection pooler removes a connection from the pool after it has been idle for approximately 4-8 minutes, or if the pooler detects that the connection with the server has been severed.
Note: It's already mentioned in comments as well, there are some other factors which may affect first execution time, including EF Context Pooling and Query Execution Plan cache in DBMS. But usually the most time-consuming of them is re-opening connection.
add a comment |
Usually all providers have some implementations to prune idle connections after a period of idle time.
The first command takes a bit more time to create and open the connection. The next commands will use the pooled connection and they are faster. Then after being idle for a specific amount of time, providers prune the pool and close idle connections. Then the next command after idle time, needs to open a connection which again takes time.
For example for Npgsql
, there is a PruneIdleConnectors
in ConnectorPool
which does the clean up based on the value of NpgsqlConnectionStringBuilder.ConnectionIdleLifetime
which is 5 minutes by default. Here are related pooling parameters:
Connection Idle Lifetime: The time (in seconds) to wait before closing idle connections in the pool if the count of all connections exceeds MinPoolSize. Since 3.1 only. 300 seconds by default.
Connection Pruning Interval: How many seconds the pool waits before attempting to prune idle connections that are beyond idle lifetime (see ConnectionIdleLifetime). Since 3.1 only. 10 seconds by default.
For ADO.NET SqlConnection
pooling this time is approximately 4-8 minutes:
The connection pooler removes a connection from the pool after it has been idle for approximately 4-8 minutes, or if the pooler detects that the connection with the server has been severed.
Note: It's already mentioned in comments as well, there are some other factors which may affect first execution time, including EF Context Pooling and Query Execution Plan cache in DBMS. But usually the most time-consuming of them is re-opening connection.
Usually all providers have some implementations to prune idle connections after a period of idle time.
The first command takes a bit more time to create and open the connection. The next commands will use the pooled connection and they are faster. Then after being idle for a specific amount of time, providers prune the pool and close idle connections. Then the next command after idle time, needs to open a connection which again takes time.
For example for Npgsql
, there is a PruneIdleConnectors
in ConnectorPool
which does the clean up based on the value of NpgsqlConnectionStringBuilder.ConnectionIdleLifetime
which is 5 minutes by default. Here are related pooling parameters:
Connection Idle Lifetime: The time (in seconds) to wait before closing idle connections in the pool if the count of all connections exceeds MinPoolSize. Since 3.1 only. 300 seconds by default.
Connection Pruning Interval: How many seconds the pool waits before attempting to prune idle connections that are beyond idle lifetime (see ConnectionIdleLifetime). Since 3.1 only. 10 seconds by default.
For ADO.NET SqlConnection
pooling this time is approximately 4-8 minutes:
The connection pooler removes a connection from the pool after it has been idle for approximately 4-8 minutes, or if the pooler detects that the connection with the server has been severed.
Note: It's already mentioned in comments as well, there are some other factors which may affect first execution time, including EF Context Pooling and Query Execution Plan cache in DBMS. But usually the most time-consuming of them is re-opening connection.
edited Dec 29 '18 at 10:02
answered Dec 29 '18 at 9:16
Reza AghaeiReza Aghaei
65.4k853159
65.4k853159
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968034%2fef-core-addasync-and-savechangesasync-slightly-slower-after-application-is-idle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The connection pooler removes a connection from the pool after it has been idle for approximately 4-8 minutes, or if the pooler detects that the connection with the server has been severed. ADO.NET Connection Pooling
– Reza Aghaei
Dec 29 '18 at 8:45
The database connection is not continuous connection for long time, after few minutes the connection has been refused and application needs to connect to database again.
– isaeid
Dec 29 '18 at 9:07
not only the connection has to be re-opened after a prolonged idle time, possibly the context cache is not valid anymore which would require a re-computation of the query (+maybe query plan on dbms side), possibly even a re-run of the model creation+check step.
– DevilSuichiro
Dec 29 '18 at 9:44