An external API is a dependency outside your operational control. It can become slow, unavailable, rate-limited or change behavior. A robust integration isolates provider-specific details and defines how the application behaves when the dependency cannot respond.
Set explicit timeouts
Use connection and response timeouts appropriate to the user-facing operation. An unbounded request can tie up PHP workers and cause a wider outage. Choose a timeout budget that fits the overall request deadline and leave time for the application to return a useful response.
Retry only when safe
Retries can help with transient failures, but they can also amplify an outage. Use bounded attempts with exponential backoff and jitter. Retry only operations that are idempotent or protected by an idempotency key. Do not retry validation failures or permanent authorization errors.
Apply circuit breaking and fallback behavior
A circuit breaker can stop repeated calls to a failing provider and allow the system to recover. A fallback may use cached data, queue the operation or explain temporary unavailability. Do not silently present stale or incomplete information as current.
Isolate the provider
Wrap external calls in a dedicated client or adapter. Normalize provider errors, validate responses and keep credentials in protected configuration. Record latency and failure categories without logging authorization headers or personal payloads.
Checklist
- Set connection and total request timeouts.
- Use bounded retries with backoff and jitter.
- Protect non-idempotent operations from duplicate execution.
- Define fallback and recovery behavior.
- Monitor provider latency, quotas and error rates.