PHPackages                             rtckit/react-redlock - 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. [Caching](/categories/caching)
4. /
5. rtckit/react-redlock

ActiveLibrary[Caching](/categories/caching)

rtckit/react-redlock
====================

Asynchronous distributed locks with Redis and ReactPHP

2.1.0(4y ago)177.9k21MITPHPPHP &gt;=7.2

Since Nov 20Pushed 4y ago2 watchersCompare

[ Source](https://github.com/rtckit/reactphp-redlock)[ Packagist](https://packagist.org/packages/rtckit/react-redlock)[ Docs](https://github.com/rtckit/reactphp-redlock)[ RSS](/packages/rtckit-react-redlock/feed)WikiDiscussions main Synced today

READMEChangelog (2)Dependencies (5)Versions (4)Used By (1)

Distributed locks with Redis and ReactPHP
=========================================

[](#distributed-locks-with-redis-and-reactphp)

Asynchronous [Redlock](https://redis.io/topics/distlock) algorithm implementation for PHP

[![Build Status](https://camo.githubusercontent.com/2d8fa0f5e4c1ee09db8275cc8a3522ed8d6a29cdf4193368de5a1370d4397563/68747470733a2f2f7472617669732d63692e636f6d2f7274636b69742f72656163747068702d7265646c6f636b2e7376673f6272616e63683d6d61696e)](https://travis-ci.com/rtckit/reactphp-redlock)[![Latest Stable Version](https://camo.githubusercontent.com/32e313ede0e7f69ed14c8a33e3dd0385f09e0a706bfbc9e96e3b697578db7ae0/68747470733a2f2f706f7365722e707567782e6f72672f7274636b69742f72656163742d7265646c6f636b2f762f737461626c652e706e67)](https://packagist.org/packages/rtckit/react-redlock)[![Test Coverage](https://camo.githubusercontent.com/4e7836059a15b34eae971259dad805f2df7333cec055f8a045a52c97882a32da/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f61666635656538653865663362353136383963322f746573745f636f766572616765)](https://codeclimate.com/github/rtckit/reactphp-redlock/test_coverage)[![Maintainability](https://camo.githubusercontent.com/79003d1c55c8cbb4a78ac8922d4450bcd3ce128249784dbf0c662130c19487d2/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f61666635656538653865663362353136383963322f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/rtckit/reactphp-redlock/maintainability)[![License](https://camo.githubusercontent.com/b8cadaa967891081f8f165695470689986c028821dd8a040132f6e661795dc0d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c7565)](LICENSE)

Quickstart
----------

[](#quickstart)

Once [installed](#installation), you can incorporate Redlock in your projects by instantiating its *Custodian*; this entity is responsible for lock orchestration and it requires access to a Redis client object instance, e.g.

```
/* Instantiate prerequisites */
$factory = new \Clue\React\Redis\Factory();
$client = $factory->createLazyClient('127.0.0.1');

/* Instantiate our lock custodian */
$custodian = new \RTCKit\React\Redlock\Custodian($client);
```

#### Acquiring locks

[](#acquiring-locks)

For use cases where a binary outcome is desirable, the `acquire()` method works best, e.g.:

```
/**
 * @param string $resource Redis key name
 * @param float $ttl Lock's time to live (in seconds)
 * @param ?string $token Unique identifier for lock in question
 * @return PromiseInterface
 */
$custodian->acquire('MyResource', 60, 'r4nd0m_token')
    ->then(function (?Lock $lock) {
        if (is_null($lock)) {
            // Ooops, lock could not be acquired for MyResource
        } else {
            // Awesome, MyResource is locked for a minute
            // ...

            // Be nice and release the lock when done
            $custodian->release($lock);
        }
    });
```

#### Spinlocks

[](#spinlocks)

The `spin()` method is designed for situations where a process should keep trying acquiring a lock, e.g.

```
/**
 * @param int $attempts Maximum spin/tries
 * @param float $interval Spin/try interval (in seconds)
 * @param string $resource Redis key name
 * @param float $ttl Lock's time to live (in seconds)
 * @param ?string $token Unique identifier for lock in question
 * @return PromiseInterface
 */
$custodian->spin(100, 0.5, 'HotResource', 10, 'r4nd0m_token')
    ->then(function (?Lock $lock): void {
        if (is_null($lock)) {
            // Wow, after 100 tries (with a gap of 0.5 seconds) I've
            // given up acquiring a lock on HotResource
        } else {
            // Awesome, HotResource is locked for 10 seconds
            // ...

            // Again, be nice and release the lock when done
            $custodian->release($lock);
        }
    })
```

Lastly, the provided [examples](examples) are a good starting point.

Requirements
------------

[](#requirements)

Redlock is compatible with PHP 7.2+ and requires the [clue/reactphp-redis](https://github.com/clue/reactphp-redis) library.

Installation
------------

[](#installation)

You can add the library as project dependency using [Composer](https://getcomposer.org/):

```
composer require rtckit/react-redlock
```

If you only need the library during development, for instance when used in your test suite, then you should add it as a development-only dependency:

```
composer require --dev rtckit/react-redlock
```

Tests
-----

[](#tests)

To run the test suite, clone this repository and then install dependencies via Composer:

```
composer install
```

Then, go to the project root and run:

```
php -d memory_limit=-1 ./vendor/bin/phpunit -c ./etc/phpunit.xml.dist
```

### Static Analysis

[](#static-analysis)

In order to ensure high code quality, Redlock uses [PHPStan](https://github.com/phpstan/phpstan) and [Psalm](https://github.com/vimeo/psalm):

```
php -d memory_limit=-1 ./vendor/bin/phpstan analyse -c ./etc/phpstan.neon -n -vvv --ansi --level=max src
php -d memory_limit=-1 ./vendor/bin/psalm --config=./etc/psalm.xml --show-info=true
```

License
-------

[](#license)

MIT, see [LICENSE file](LICENSE).

### Acknowledgments

[](#acknowledgments)

- [antirez](http://antirez.com/news/77) - Original blog post
- [ReactPHP Project](https://reactphp.org/)
- [clue/reactphp-redis](https://github.com/clue/reactphp-redis) - Async Redis client implementation

### Contributing

[](#contributing)

Bug reports (and small patches) can be submitted via the [issue tracker](https://github.com/rtckit/reactphp-redlock/issues). Forking the repository and submitting a Pull Request is preferred for substantial patches.

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~142 days

Total

3

Last Release

1765d ago

Major Versions

1.0.0 → 2.0.02021-08-17

### Community

Maintainers

![](https://www.gravatar.com/avatar/d9f358b63c4304f3ad919493eba06c1566279c434fd69a5286abda691a05b92b?d=identicon)[cip](/maintainers/cip)

---

Top Contributors

[![cdosoftei](https://avatars.githubusercontent.com/u/7636091?v=4)](https://github.com/cdosoftei "cdosoftei (17 commits)")

---

Tags

composer-libraryconcurrencydistributed-lockdistributed-systemsphp-libraryreactphpredisredlockreactphpredismutexredlockreactlock

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/rtckit-react-redlock/health.svg)

```
[![Health](https://phpackages.com/badges/rtckit-react-redlock/health.svg)](https://phpackages.com/packages/rtckit-react-redlock)
```

###  Alternatives

[malkusch/lock

Mutex library for exclusive code execution.

96410.1M29](/packages/malkusch-lock)[arvenil/ninja-mutex

Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking

1963.8M30](/packages/arvenil-ninja-mutex)[predis/predis-async

Asynchronous version of Predis

370354.5k](/packages/predis-predis-async)[clue/redis-server

A redis server implementation in pure PHP

1941.3k](/packages/clue-redis-server)[icecave/chastity

Distributed advisory locking for PHP.

1513.3k](/packages/icecave-chastity)[avtonom/semaphore-bundle

A generic locking Symfony bundle for PHP, that uses named locks for semaphore acquire and release, to guarantee atomicity

112.9k](/packages/avtonom-semaphore-bundle)

PHPackages © 2026

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