Retry retryable network failures and HTTP 429/503 with capped exponential backoff. The examples separate the essential operation from the conditions a production implementation must enforce.
What It Does
Retry retryable network failures and HTTP 429/503 with capped exponential backoff.
Prerequisites
Use a dedicated development environment with test accounts and records. Separate authorization, validation and error handling from response formatting; never copy demonstration-only secrets into production.
Example 1: Core Behavior
The first example isolates the core operation so its result is easy to understand.
<?php
$attempt=2; $delay=min(8, (2**$attempt)+random_int(0,100)/100);
echo 'Delay: '.$delay.'s';
Example 2: Alternative or Failure Case
The second example demonstrates an alternative outcome, rule or failure condition.
<?php
$status=429; $retryAfter='3';
$delay=ctype_digit($retryAfter)?min(30,(int)$retryAfter):5;
echo $status===429?"Wait $delay seconds":'Do not retry';
Practical Example: Applying the Rule
This practical workflow performs the relevant operation, with strict error checks. Configure the required transport or database driver before running it.
<?php
$url = 'https://api.example.test/status';
for ($attempt = 0; $attempt < 3; $attempt++) {
$ch = curl_init($url);
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 4, CURLOPT_CONNECTTIMEOUT => 2, CURLOPT_FOLLOWLOCATION => false]);
$body = curl_exec($ch);
$status = $body === false ? 0 : curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($status >= 200 && $status < 300) { echo $body; break; }
if ($attempt === 2 || !in_array($status, [0, 429, 503], true)) throw new RuntimeException('Request failed');
usleep((int) ((2 ** $attempt) * 100000 + random_int(0, 100000)));
}
Important Behavior and Edge Cases
Respect Retry-After, avoid retrying unsafe writes without idempotency keys and set a total deadline.
Comparison with Related Functions or Patterns
| Approach | When it differs |
|---|---|
| PHP API Client Retries with Exponential Backoff | Retry retryable network failures and HTTP 429/503 with capped exponential backoff. |
| Related API or pattern | Circuit breakers prevent hammering a persistently failing dependency. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Automatically retrying a payment POST without an idempotency key | Respect Retry-After, avoid retrying unsafe writes without idempotency keys and set a total deadline. |
| Substituting a related API without checking its semantics | Circuit breakers prevent hammering a persistently failing dependency. |
Key Takeaways
- Retry retryable network failures and HTTP 429/503 with capped exponential backoff.
- Respect Retry-After, avoid retrying unsafe writes without idempotency keys and set a total deadline.
- Circuit breakers prevent hammering a persistently failing dependency.
Select this API or pattern based on its documented behavior and the trust boundaries in your own application, and test all external integrations against their actual deployments.