PHPackages                             yriveiro/php-backoff - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yriveiro/php-backoff

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yriveiro/php-backoff
====================

Simple backoff / retry functionality

2.1.1(5y ago)2679.3k↓38.4%11MITPHPPHP &gt;=7.0CI failing

Since Jan 31Pushed 5y ago2 watchersCompare

[ Source](https://github.com/yriveiro/php-backoff)[ Packagist](https://packagist.org/packages/yriveiro/php-backoff)[ RSS](/packages/yriveiro-php-backoff/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (3)Versions (12)Used By (1)

Backoff, Simple backoff / retry functionality
=============================================

[](#backoff-simple-backoff--retry-functionality)

[![License](https://camo.githubusercontent.com/da10c915e4f84168dbb7270f7146284c78515c6a664dd6f3e15c7a3766f6536c/68747470733a2f2f706f7365722e707567782e6f72672f797269766569726f2f7068702d6261636b6f66662f6c6963656e7365)](https://packagist.org/packages/yriveiro/php-backoff) [![Build Status](https://camo.githubusercontent.com/59bcab6f0c58ece74f8546ca99375a9ee5053482fb97dacd9b5cb634b39effe7/68747470733a2f2f7472617669732d63692e6f72672f797269766569726f2f7068702d6261636b6f66662e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/yriveiro/php-backoff) [![Coverage Status](https://camo.githubusercontent.com/2662dcc404341709ad67de3f75cd2ed8458139ff7a63680873eee2b0d3be903b/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f797269766569726f2f7068702d6261636b6f66662f62616467652e737667)](https://coveralls.io/github/yriveiro/php-backoff) [![Total Downloads](https://camo.githubusercontent.com/fea8a2116f0593e43058b912d8393954f5cd25d896055dab704fe83da8caa711/68747470733a2f2f706f7365722e707567782e6f72672f797269766569726f2f7068702d6261636b6f66662f646f776e6c6f616473)](https://packagist.org/packages/yriveiro/php-backoff) [![SensioLabsInsight](https://camo.githubusercontent.com/5e7b1b6dd8b7300d3a345c16f3b77ba9c6cb2c01d4a07f1a0871d014ffd6c650/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f30663161376234342d393865392d343537372d383139662d3764663831313333383038322f6d696e692e706e67)](https://insight.sensiolabs.com/projects/0f1a7b44-98e9-4577-819f-7df811338082)

**NOTE**: to use php-backoff with PHP 5.x please use the lastet release of branch 1.x

API
===

[](#api)

### getDefaultOptions():

[](#getdefaultoptions)

This method is static and returns an array with the default options:

- `cap`: Max duration allowed (in microseconds). If backoff duration is greater than cap, cap is returned, default is `1000000` microseconds.
- `maxAttempts`: Number of attempts before thrown an Yriveiro\\Backoff\\BackoffException. Default is `0`, no limit.

### exponential($attempt):

[](#exponentialattempt)

This method use and exponential function `E(attempt) = (2**attempt - 1)` to calculate backoff time.

#### Parameters

[](#parameters)

- `attempt`: incremental value that represents the current retry number.

### equalJitter($attempt);

[](#equaljitterattempt)

Exponential backoff has one disadvantage. In high concurrence, we can have multiples calls with the same backoff time due the time is highly bound to the current attempt, different calls could be in the same attempt.

To solve this we can add a jitter value to allow some randomization.

`equalJitter` uses the function: `E(attempt) = min(((2**attempt - 1) / 2), random(0, ((2**attempt - 1) / 2)))`.

#### Parameters

[](#parameters-1)

- `attempt`: incremental value that represents the current retry number.

### fullJitter($attempt);

[](#fulljitterattempt)

Full jitter behaves like `equalJitter` method, the main difference between them is the way in how the jitter value is calculated.

`fullJitter` uses the function: `E(attempt) = min(random(0, (2**attempt - 1) / 2))`.

#### Parameters

[](#parameters-2)

- `attempt`: incremental value that represents the current retry number.

Usage
=====

[](#usage)

### Zero configuration examples:

[](#zero-configuration-examples)

With zero configuration, we will never stop to try fetch data. The exit condition is your responsibility.

```
$attempt = 1;
$backoff = new Backoff();

$response = $http->get('http://myservice.com/user/1');

while (!$response) {
    $time = $backoff->exponential($attempt);
    $attempt++;

    usleep($time);

    $response = $http->get('http://myservice.com/user/1');
}
```

### With configuration examples:

[](#with-configuration-examples)

```
$attempt = 1;
$options = Backoff::getDefaultOptions();
$options['maxAttempts'] = 3;

$backoff = new Backoff($options);

$response = $http->get('http://myservice.com/user/1');

try
    while (!$response) {
        $time = $backoff->fullJitter($attempt);
        $attempt++;

        usleep($time);

        $response = $http->get('http://myservice.com/user/1');
    }
} catch (Yriveiro\Backoff\BackoffException $e) {
    // Handle the exception
}
```

Installation
============

[](#installation)

The recommended way to install this package is through [Composer](http://getcomposer.org/download/).

```
php composer.phar require "yriveiro/php-backoff"
```

Tests
=====

[](#tests)

Tests are performed using the `phpunit` library, to run them:

```
php vendor/bin/phpunit tests
```

Know issues
===========

[](#know-issues)

None.

How to contribute
=================

[](#how-to-contribute)

Have an idea? Found a bug?, contributions are welcome :)

License
=======

[](#license)

Backoff is licensed under MIT license.

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity40

Moderate usage in the ecosystem

Community14

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 97.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~179 days

Recently: every ~317 days

Total

11

Last Release

1993d ago

Major Versions

0.0.1 → 1.0.02017-02-17

1.0.2 → 2.0.02017-07-04

1.x-dev → 2.0.22018-01-13

PHP version history (2 changes)2.0.0PHP &gt;=7.0

1.1.0PHP &gt;=5.3.3 &lt;7.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/934763?v=4)[Yago Riveiro](/maintainers/yriveiro)[@yriveiro](https://github.com/yriveiro)

---

Top Contributors

[![yriveiro](https://avatars.githubusercontent.com/u/934763?v=4)](https://github.com/yriveiro "yriveiro (75 commits)")[![tstrijdhorst](https://avatars.githubusercontent.com/u/1564322?v=4)](https://github.com/tstrijdhorst "tstrijdhorst (1 commits)")[![webarchitect609](https://avatars.githubusercontent.com/u/11293610?v=4)](https://github.com/webarchitect609 "webarchitect609 (1 commits)")

---

Tags

backoffbackoff-algorithmsjitterretryretry-libraryretryJitterbackoffexponential backoff

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/yriveiro-php-backoff/health.svg)

```
[![Health](https://phpackages.com/badges/yriveiro-php-backoff/health.svg)](https://phpackages.com/packages/yriveiro-php-backoff)
```

###  Alternatives

[eventsauce/backoff

Back-off strategy interface

70834.8k8](/packages/eventsauce-backoff)[vkartaviy/retry

The library for repeatable and retryable operations

29229.5k2](/packages/vkartaviy-retry)[tobion/retry

A generic library to retry an operation in case of an error. You can configure the behavior like the exceptions to retry on.

15450.7k](/packages/tobion-retry)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
