php
49 lines · 7 steps
Retry with exponential backoff in PHP
A small class that re-runs a failing operation with growing, jittered delays until it succeeds or gives up.
Explained by
highlit
1<?php
2
3namespace App\Support;
4
5use Closure;
6use Throwable;
7
8class Retryer
9{
10 public function __construct(
11 private int $maxAttempts = 5,
12 private int $baseDelayMs = 100,
13 private int $maxDelayMs = 10000,
14 private float $multiplier = 2.0,
15 ) {
16 }
17
18 public function run(Closure $operation, ?Closure $shouldRetry = null): mixed
19 {
20 $attempt = 0;
21
22 beginning:
23 try {
24 return $operation($attempt + 1);
25 } catch (Throwable $e) {
26 $attempt++;
27
28 if ($attempt >= $this->maxAttempts) {
29 throw $e;
30 }
31
32 if ($shouldRetry !== null && ! $shouldRetry($e)) {
33 throw $e;
34 }
35
36 usleep($this->delayFor($attempt) * 1000);
37 goto beginning;
38 }
39 }
40
41 private function delayFor(int $attempt): int
42 {
43 $exponential = $this->baseDelayMs * ($this->multiplier ** ($attempt - 1));
44 $capped = min($exponential, $this->maxDelayMs);
45 $jitter = random_int(0, (int) ($capped / 2));
46
47 return (int) ($capped - $jitter);
48 }
49}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Exponential backoff with a cap keeps retries from hammering a struggling dependency while bounding the worst-case wait.
- 2Adding jitter spreads concurrent retries out so many clients don't all wake at the same instant.
- 3A retry predicate lets callers distinguish transient failures worth retrying from permanent ones that should fail fast.
Related explainers
javascript
const ROLE_PERMISSIONS = { admin: ['users:read', 'users:write', 'billing:read', 'billing:write'], manager: ['users:read', 'billing:read'], member: ['users:read'],
Role-based permissions middleware in Express
authorization
middleware
rbac
Intermediate
9 steps
php
<?php namespace App\Console\Commands;
Releasing stale document locks in Laravel
artisan-command
transactions
row-locking
Intermediate
6 steps
ruby
class TemplateInterpolator PLACEHOLDER = /\{\{\s*([\w.]+)\s*\}\}/ def initialize(strict: false)
Interpolating templates with dotted keys in Ruby
regex
string-interpolation
hash-traversal
Intermediate
6 steps
php
<?php namespace App\Providers;
Subdomain multi-tenancy routing in Laravel
multi-tenancy
service-container
route-binding
Advanced
7 steps
php
<?php namespace App\Broadcasting;
Authorizing presence channels in Laravel
broadcasting
authorization
presence-channels
Intermediate
3 steps
go
package config import ( "fmt"
A thread-safe config singleton in Go
singleton
concurrency
environment-variables
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/retry-with-exponential-backoff-in-php-explained-php-1837/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.