Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize TSQL outbox cleanup by reducing batch size and adding rowlock hint #1249

Merged
merged 7 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

delete top (@BatchSize) from [TheSchema].[TheTablePrefixOutboxData]
delete top (@BatchSize) from [TheSchema].[TheTablePrefixOutboxData] with (rowlock)
where Dispatched = 'true' and
DispatchedAt < @DispatchedBefore
2 changes: 1 addition & 1 deletion src/SqlPersistence/Outbox/OutboxPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class OutboxPersister : IOutboxStorage

public OutboxPersister(IConnectionManager connectionManager, SqlDialect sqlDialect, OutboxCommands outboxCommands,
Func<ISqlOutboxTransaction> outboxTransactionFactory,
int cleanupBatchSize = 10000)
int cleanupBatchSize = 4000) // Keep below 4000 to prevent lock escalation
ramonsmits marked this conversation as resolved.
Show resolved Hide resolved
{
this.connectionManager = connectionManager;
this.sqlDialect = sqlDialect;
Expand Down
3 changes: 2 additions & 1 deletion src/SqlPersistence/Outbox/SqlDialect_MsSqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ internal override string GetOutboxPessimisticCompleteCommand(string tableName)

internal override string GetOutboxCleanupCommand(string tableName)
{
// Rowlock hint to prevent lock escalation which can result in INSERT's and competing cleanup to dead-lock
return $@"
delete top (@BatchSize) from {tableName}
delete top (@BatchSize) from {tableName} with (rowlock)
where Dispatched = 'true' and
DispatchedAt < @DispatchedBefore";
}
Expand Down