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
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 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.