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 5 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
9 changes: 4 additions & 5 deletions src/SqlPersistence/Outbox/OutboxPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@ class OutboxPersister : IOutboxStorage
{
readonly IConnectionManager connectionManager;
readonly SqlDialect sqlDialect;
readonly int cleanupBatchSize;
readonly OutboxCommands outboxCommands;
readonly Func<ISqlOutboxTransaction> outboxTransactionFactory;

public OutboxPersister(IConnectionManager connectionManager, SqlDialect sqlDialect, OutboxCommands outboxCommands,
Func<ISqlOutboxTransaction> outboxTransactionFactory,
int cleanupBatchSize = 10000)
Func<ISqlOutboxTransaction> outboxTransactionFactory)
{
this.connectionManager = connectionManager;
this.sqlDialect = sqlDialect;
this.outboxCommands = outboxCommands;
this.outboxTransactionFactory = outboxTransactionFactory;
this.cleanupBatchSize = cleanupBatchSize;
}

public Task<IOutboxTransaction> BeginTransaction(ContextBag context, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -126,6 +123,8 @@ public Task Store(OutboxMessage message, IOutboxTransaction outboxTransaction, C

public async Task RemoveEntriesOlderThan(DateTime dateTime, CancellationToken cancellationToken = default)
{
const int CleanupBatchSize = 4000; // Keep below 4000 to prevent lock escalation

using (var connection = await connectionManager.OpenNonContextualConnection(cancellationToken).ConfigureAwait(false))
{
var continuePurging = true;
Expand All @@ -137,7 +136,7 @@ public async Task RemoveEntriesOlderThan(DateTime dateTime, CancellationToken ca
{
command.CommandText = outboxCommands.Cleanup;
command.AddParameter("DispatchedBefore", dateTime);
command.AddParameter("BatchSize", cleanupBatchSize);
command.AddParameter("BatchSize", CleanupBatchSize);
var rowCount = await command.ExecuteNonQueryEx(cancellationToken).ConfigureAwait(false);
continuePurging = rowCount != 0;
}
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