[9.x] Use readpast query hint instead of holdlock for sqlsrv database queue #43259
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Back in the days, the
database
queue driver was recommended only for development due to performance and deadlock issues. Quite some time ago, somewhen during the life of Laravel 6, performance improvements were introduced for thedatabase
queue driver in combination with MySQL>= 8.0.1
and PostgreSQL>= 9.5
. This improvement was later extended to cover MariaDB>= 10.6.0
as well. But until this day, there is no special handling for thesqlsrv
database driver and deadlocks are a frequent issue, even though SQL Server supports a similar feature. This PR therefore adds the special lock flagreadpast
for thesqlsrv
driver.The speed improvement of this change is quite significant, especially when running a lot of workers.
Old behavior:
Currently, the
database
queue driver withsqlsrv
as database driver is using the default update lock flagswith(rowlock,updlock,holdlock)
, which are defined in theSqlServerGrammar
.The default update lock flags are used because of
return true
in theDatabaseQueue::getLockForPopping()
method which are passed toBuilder::lock($bool)
inDatabaseQueue::getNextAvailableJob()
.New behavior:
With this change, we now pass
with(rowlock,updlock,readpast)
instead ofwith(rowlock,updlock,holdlock)
when retrieving the next job to process.The meaning of
READPAST
according to the official documentation is very similar to the other DBMS:The
READPAST
flag cannot be combined withHOLDLOCK
.Test coverage:
This PR does not include any tests since concurrency is extremely hard to test and there do not seem to be any SQL Server tests for the queue system so far. The locking behavior of SQL Server can be tested manually with the following two snippets which are to be executed in this order concurrently: