You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enlist to ambient TransactionScope by default #1483
Enlisting is currently opt-in, either via Enlist=true on the connection string or via DbConnection.Enlist(). Check what today's standard is with ADO.NET and maybe make Enlist=true the default.
As I understand msdn, it should be enabled by default.
Automatic enlistment is the default (and preferred) way of integrating ADO.NET connections with System.Transactions. A connection object will automatically enlist in an existing distributed transaction if it determines that a transaction is active, which, in System.Transaction terms, means that Transaction.Current is not null. Automatic transaction enlistment occurs when the connection is opened. It will not happen after that even if a command is executed inside of a transaction scope. You can disable auto-enlistment in existing transactions by specifying Enlist=false as a connection string parameter for a SqlConnection.ConnectionString, or OLE DB Services=-7 as a connection string parameter for an OleDbConnection.ConnectionString.
At least that is the current behavior of most connections: SqlConnection, SQLiteConnection, OdbcConnection, OleDbConnection, OracleConnection. (That is not the case of FbConnection either (FireBird).)
Auto-enlistment is the default for most other data-providers. So it seems very strange for it to have a significant impact on performances in real case usages.
Anyway users having issues with it can easily disable it, since it is a connexion string parameter.
Disabling it by default re-introduces a discrepancy in behavior with most other ADO data providers, breaking the "least surprise" principle for users having to work with many databases. In my view the first performance metric is favoring correct programs, and this move favors bugs for users used to other databases. (Especially in the case of a program able to run on multiple databases, this change will be a trap each time it has to be deployed for a PostgreSQL DB using Npgsql.)
@fredericDelaporte don't worry, this is a temporary thing :) I'm just about to release 4.0.0-preview1, where I haven't had time to deal with this. That's why this issue is reopened and still on the 4.0.0 milestone - it will be fixed by the final release.
I looked at this again, to try to switch back to enlisting by default - the problem isn't simple.
The reason I disabled enlisting by default, is that it requires Npgsql to check whether an ambient transaction exists when opening a pooled connection (i.e. checking whether Transaction.Current). The problem is that calling the getter is Transaction.Current is actually a perf-wise non-trivial operation - at the heart is an AsyncLocal, surrounded by some lookups and diagnostics functions.
I admit I don't have a benchmark at hand to show the perf impact, but a lot of the work for Npgsql 4.0 has been to make pooled open/close cycles as fast as possible (e.g a the pool was replaced with a lock-free implementation). I'm reluctant to pay the price of this check for every open, especially when TransactionScope usage doesn't seem to be that pervasive, and of course it's possible to opt into it either via the connection string or programmatically.
It could be argued that enlisting should be on by default, and that high-perf scenarios should disable it, but in this case I'm leaning towards making the default more high-perf.
It could be argued that enlisting should be on by default, and that high-perf scenarios should disable it
Precisely what I think! But moreover, I doubt any real-world high-perf scenario would indeed benefit from this optimization:
The problem is that calling the getter is Transaction.Current is actually a perf-wise non-trivial operation
This has been seen at NHibernate side, because it has started enlisting by itself the connection in its 5.0 version, but the enlistment checks were done quite a bit too redundantly. (Not only once per operation on the session, but potentially many times). Once this point fixed (nhibernate/nhibernate-core#1455), the enlistment check cost disappeared again from the performance profiling.
Moreover, this performance issue was witnessed only with a benchmark strongly deviating from a normal ORM usage. Normal usages were not showing any significant impact of the redundant checks NHibernate was doing in its 5.0.0 and 5.0.1 versions.
So I have no difficulties believing you can see a performance impact when benchmarking just the Npgsql connection opening. But it seems likely to me this performance impact would vanish in a benchmark involving doing actually something with the connection, not just opening it.
I personally do not think that benchmarks on a method are interesting. Who opens connections just for the sake of opening them in a business application? Such a benchmark tends to cause people focusing on points which may not help actually gaining performances, because in real usage case they are always bound to other operations weighting thousands more. (Opening a pooled connection has normally a trivial cost compared to actually querying the database, and opening a connection without querying the database seems a bit moot.)
e.g a the pool was replaced with a lock-free implementation
Thankfully, since locks can cause locks contention, highly reducing concurrency. But that is not the case of Transaction.Current. I do not think it is comparable.
The bulk of my high-perf work for 4.0 was running the TechEmpower fortunes scenario, which does execute a query - although it's true that the query results will typically be cached in memory on the PostgreSQL, and the connection was a (low-latency) 10Gbps link. As to whether that's a realistic scenario we want to optimize for - that's a valid question. I also don't see it as very dramatic if enlistment is opt-in rather than opt-out - sure, there's a small surprise effect but it's very fail-fast and clear where it's coming from.
But I do partially agree with you, and it's possible I'm being a bit too high-perf on this. At the very least I'd want to run TechEmpower fortunes to measure the overall impact of checking Transaction.Current - the idea is to do that after preview1 (which I plan to release tomorrow).
FWIW I do agree that Transaction.Current is quite different from the contention caused by a locking pool (contention vs. pure CPU). However, I also found myself doing other, non-contention-related optimizations (e.g. eliminating CPU overhead created by async calls) and saw some real performance benefits for my scenario.
I agree with @fredericDelaporte. Enlisting should be opt-out. In my mind usability comes before performance.
EF currently doesn't have a way to opt-out of ambient transaction checks and the perf benefit should be considerable to even add that option. Because inevitably it will get into a "Top 10 hacks to make EF faster" list and used without full understanding of the implications.
@roji While I am not able to find any documentation that points that ADO.NET providers should auto-enlist by default, this has had me a bit concerned for a while.
When you are using a provider that doesn't support System.Transactions (or that doesn't enlist by default), the calls to System.Transactions APIs become no-op. You would need to write a test that rolls back changes made to the database to be sure.
In fact this would have been a good reason not to bring System.Transactions at all into the .NET Core world, but with the emphasis on compatibility it happened anyway.
Once the API exists I am not sure documenting its limitations is going to be enough. The behavior is very common among .NET Framework providers, and I think most existing users of the System.Transactions APIs give it for granted.
So far we have made mixed decisions on this (e.g. https://github.com/aspnet/Microsoft.Data.Sqlite/issues/294 was moved to the backlog), and I don't have a super strong feeling about what the best choice if for Npgsql, but I think having it on by default and switching it off in the connection string if you care for getting the best performance is a reasonable compromise (it seems to me that there are already other things that you want to change in the connection string if you want the best performance anyway).
Activity
fredericDelaporte commentedon May 24, 2017
As I understand msdn, it should be enabled by default.
At least that is the current behavior of most connections:
SqlConnection,SQLiteConnection,OdbcConnection,OleDbConnection,OracleConnection. (That is not the case ofFbConnectioneither (FireBird).)[-]Enlist to TransactionScope by default?[/-][+]Enlist to ambient TransactionScope by default[/+]roji commentedon Mar 18, 2018
This has been rolled back for now, for performance reasons.
fredericDelaporte commentedon Mar 18, 2018
This sounds as a strange development to me:
roji commentedon Mar 18, 2018
@fredericDelaporte don't worry, this is a temporary thing :) I'm just about to release 4.0.0-preview1, where I haven't had time to deal with this. That's why this issue is reopened and still on the 4.0.0 milestone - it will be fixed by the final release.
roji commentedon Mar 18, 2018
I looked at this again, to try to switch back to enlisting by default - the problem isn't simple.
The reason I disabled enlisting by default, is that it requires Npgsql to check whether an ambient transaction exists when opening a pooled connection (i.e. checking whether
Transaction.Current). The problem is that calling the getter isTransaction.Currentis actually a perf-wise non-trivial operation - at the heart is anAsyncLocal, surrounded by some lookups and diagnostics functions.I admit I don't have a benchmark at hand to show the perf impact, but a lot of the work for Npgsql 4.0 has been to make pooled open/close cycles as fast as possible (e.g a the pool was replaced with a lock-free implementation). I'm reluctant to pay the price of this check for every open, especially when
TransactionScopeusage doesn't seem to be that pervasive, and of course it's possible to opt into it either via the connection string or programmatically.It could be argued that enlisting should be on by default, and that high-perf scenarios should disable it, but in this case I'm leaning towards making the default more high-perf.
Let me know what you think.
fredericDelaporte commentedon Mar 18, 2018
Precisely what I think! But moreover, I doubt any real-world high-perf scenario would indeed benefit from this optimization:
This has been seen at NHibernate side, because it has started enlisting by itself the connection in its 5.0 version, but the enlistment checks were done quite a bit too redundantly. (Not only once per operation on the session, but potentially many times). Once this point fixed (nhibernate/nhibernate-core#1455), the enlistment check cost disappeared again from the performance profiling.
Moreover, this performance issue was witnessed only with a benchmark strongly deviating from a normal ORM usage. Normal usages were not showing any significant impact of the redundant checks NHibernate was doing in its 5.0.0 and 5.0.1 versions.
So I have no difficulties believing you can see a performance impact when benchmarking just the Npgsql connection opening. But it seems likely to me this performance impact would vanish in a benchmark involving doing actually something with the connection, not just opening it.
I personally do not think that benchmarks on a method are interesting. Who opens connections just for the sake of opening them in a business application? Such a benchmark tends to cause people focusing on points which may not help actually gaining performances, because in real usage case they are always bound to other operations weighting thousands more. (Opening a pooled connection has normally a trivial cost compared to actually querying the database, and opening a connection without querying the database seems a bit moot.)
fredericDelaporte commentedon Mar 18, 2018
Thankfully, since locks can cause locks contention, highly reducing concurrency. But that is not the case of
Transaction.Current. I do not think it is comparable.roji commentedon Mar 18, 2018
The bulk of my high-perf work for 4.0 was running the TechEmpower fortunes scenario, which does execute a query - although it's true that the query results will typically be cached in memory on the PostgreSQL, and the connection was a (low-latency) 10Gbps link. As to whether that's a realistic scenario we want to optimize for - that's a valid question. I also don't see it as very dramatic if enlistment is opt-in rather than opt-out - sure, there's a small surprise effect but it's very fail-fast and clear where it's coming from.
But I do partially agree with you, and it's possible I'm being a bit too high-perf on this. At the very least I'd want to run TechEmpower fortunes to measure the overall impact of checking Transaction.Current - the idea is to do that after preview1 (which I plan to release tomorrow).
FWIW I do agree that
Transaction.Currentis quite different from the contention caused by a locking pool (contention vs. pure CPU). However, I also found myself doing other, non-contention-related optimizations (e.g. eliminating CPU overhead created by async calls) and saw some real performance benefits for my scenario.ajcvickers commentedon Mar 19, 2018
@divega @anpete @AndriySvyryd Interesting perf discussion here on ambient transaction enlistment.
roji commentedon Mar 19, 2018
@ajcvickers @divega @anpete @AndriySvyryd it would be good to hear your input on this
AndriySvyryd commentedon Mar 19, 2018
I agree with @fredericDelaporte. Enlisting should be opt-out. In my mind usability comes before performance.
EF currently doesn't have a way to opt-out of ambient transaction checks and the perf benefit should be considerable to even add that option. Because inevitably it will get into a "Top 10 hacks to make EF faster" list and used without full understanding of the implications.
divega commentedon Mar 19, 2018
@roji While I am not able to find any documentation that points that ADO.NET providers should auto-enlist by default, this has had me a bit concerned for a while.
When you are using a provider that doesn't support System.Transactions (or that doesn't enlist by default), the calls to System.Transactions APIs become no-op. You would need to write a test that rolls back changes made to the database to be sure.
In fact this would have been a good reason not to bring System.Transactions at all into the .NET Core world, but with the emphasis on compatibility it happened anyway.
Once the API exists I am not sure documenting its limitations is going to be enough. The behavior is very common among .NET Framework providers, and I think most existing users of the System.Transactions APIs give it for granted.
So far we have made mixed decisions on this (e.g. https://github.com/aspnet/Microsoft.Data.Sqlite/issues/294 was moved to the backlog), and I don't have a super strong feeling about what the best choice if for Npgsql, but I think having it on by default and switching it off in the connection string if you care for getting the best performance is a reasonable compromise (it seems to me that there are already other things that you want to change in the connection string if you want the best performance anyway).
roji commentedon Mar 19, 2018
Thanks for your inputs, I'll switch enlisting to on by default and document the performance aspects.
roji commentedon Mar 19, 2018
Related pull requests: