Skip to content

Conversation

@ziadoz
Copy link
Contributor

@ziadoz ziadoz commented Jan 2, 2026

This PR adds a backoff parameter to DB::transaction() that allows a delay to be added between deadlock attempts. For example, if there were 3 attempts, there would be 2 delays (between 1-2 and 2-3).

The $backoff parameter can be an integer, array or closure:

// Wait 1,000ms between each attempt...
DB::transaction(fn () => ..., attempts: 3, backoff: 1000);

// Wait 1,000ms between attempt 1-2, then 2,000ms between all subsequent attempts...
DB::transaction(fn () => ..., attempts: 4, backoff: [1000, 2000]);

// Wait a custom amount of time between attempts...
DB::transaction(fn () => ..., attempts: 3, backoff: function ($exception, $currentAttempt, $maxAttempts) {
    return random_int(100, 250) + ($currentAttempt * 100);
});

For non-sqlsrv SQL Server driver connections backoffs won't work, and an exception is thrown if one is configured.

@github-actions
Copy link

github-actions bot commented Jan 2, 2026

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@ziadoz ziadoz changed the title [12.x] Add backoff functionality to DB::transaction() [13.x] Add backoff functionality to DB::transaction() Jan 9, 2026
@ziadoz ziadoz changed the base branch from 12.x to master January 9, 2026 22:44
@tpetry
Copy link
Contributor

tpetry commented Jan 10, 2026

That fuether complicates the implementation. In my opinion you should just wrap the transaction into a retry function.

@ziadoz
Copy link
Contributor Author

ziadoz commented Jan 10, 2026

@tpetry I had been thinking similarly, which is why I had left it in draft.

@ziadoz ziadoz closed this Jan 10, 2026
@ziadoz ziadoz deleted the db-trans-backoff branch January 10, 2026 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants