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 namespace App\Http\Controllers;
Building a filtered product index in Laravel
query-builder
validation
conditional-queries
Intermediate
9 steps
rust
use axum::{ body::Bytes, extract::State, http::StatusCode,
Handling raw byte uploads in Axum
extractors
shared-state
request-limits
Intermediate
7 steps
java
@Service public class PaymentGatewayClient { private static final Logger log = LoggerFactory.getLogger(PaymentGatewayClient.class);
Resilient payment calls with Spring Retry
retry
backoff
fault-tolerance
Intermediate
7 steps
php
<?php namespace App\Models\Scopes;
How a tenant global scope works in Laravel
multi-tenancy
global-scope
query-builder
Intermediate
5 steps
javascript
const RETRIABLE_STATUS = new Set([408, 429, 500, 502, 503, 504]); function sleep(ms, signal) { return new Promise((resolve, reject) => {
Retrying fetch with exponential backoff
retry
exponential-backoff
abort-signal
Advanced
8 steps
rust
use axum::{ extract::{Query, State}, http::StatusCode, Json,
Paginated, filtered product listing in Axum
pagination
query-parameters
sql-filtering
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.