Skip to content

Commit

Permalink
Optimize TSQL outbox cleanup by reducing batch size and adding rowloc…
Browse files Browse the repository at this point in the history
…k hint (#1249)
  • Loading branch information
ramonsmits authored Sep 12, 2023
1 parent 52f1e15 commit 1e88b7b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
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
{
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

0 comments on commit 1e88b7b

Please sign in to comment.