PHPackages                             ballyhoo-technic/laravel-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. ballyhoo-technic/laravel-redlock

ActiveLibrary[Caching](/categories/caching)

ballyhoo-technic/laravel-redlock
================================

Redis distributed locks for laravel

5.0.0(1y ago)02.1k1MITPHPPHP &gt;=8.3

Since Dec 14Pushed 1y agoCompare

[ Source](https://github.com/ballyhoo-technic/laravel-redlock)[ Packagist](https://packagist.org/packages/ballyhoo-technic/laravel-redlock)[ RSS](/packages/ballyhoo-technic-laravel-redlock/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (24)Used By (0)

Laravel RedLock
===============

[](#laravel-redlock)

Provides a generic locking mechanism using Redis. Implements the locking standard proposed by Redis.

### Acknowledgements

[](#acknowledgements)

This library was originally built by LibiChai based on the Redlock algorithm developed by antirez. The library was reworked by the team at That's Us, Inc.

Regarding the new laravel 6.x LTS releases, Ballyhoo Werbeagentur GmbH did this fork, to provide a compatible version. All credits still goes to LibiChai and That's Us, Inc. Thank you for the greate work.

### Installation

[](#installation)

1. `composer require ballyhoo-technic/laravel-redlock`
2. Add `ThatsUs\RedLock\RedLockServiceProvider::class,` to the `providers` array in config/app.php
3. Enjoy!

### It's Simple!

[](#its-simple)

Set a lock on any scalar. If the `lock()` method returns false, you did not acquire the lock.

Store results of the `lock()` method. Use this value to release the lock with `unlock()`.

### Example

[](#example)

This example sets a lock on the key "1" with a 3 second expiration time.

If it acquired the lock, it does some work and releases the lock.

```
 use ThatsUs\RedLock\Facades\RedLock;

 $product_id = 1;

 $lock_token = RedLock::lock($product_id, 3000);

 if ($lock_token) {

     $order->submit($product_id);

     RedLock::unlock($lock_token);
 }
```

### Refresh

[](#refresh)

Use `refreshLock()` to reacquire and extend the time of your lock.

```
 use ThatsUs\RedLock\Facades\RedLock;

 $product_ids = [1, 2, 3, 5, 7];

 $lock_token = RedLock::lock('order-submitter', 3000);

 while ($product_ids && $lock_token) {

     $order->submit(array_shift($product_ids));

     $lock_token = RedLock::refreshLock($lock_token);
 }

 RedLock::unlock($lock_token);
```

### Even Easier with Closures

[](#even-easier-with-closures)

Use `runLocked()` for nicer syntax. The method returns the results of the closure, or else false if the lock could not be acquired.

```
 use ThatsUs\RedLock\Facades\RedLock;

 $product_id = 1;

 $result = RedLock::runLocked($product_id, 3000, function () use ($order, $product_id) {
     $order->submit($product_id);
     return true;
 });

 echo $result ? 'Worked!' : 'Lock not acquired.';
```

### Refresh with Closures

[](#refresh-with-closures)

You can easily refresh your tokens when using closures. The first parameter to your closure is `$refresh`. Simply call it when you want to refresh. If the lock cannot be refreshed, `$refresh()` will break out of the closure.

```
 use ThatsUs\RedLock\Facades\RedLock;

 $product_ids = [1, 2, 3, 5, 7];

 $result = RedLock::runLocked($product_id, 3000, function ($refresh) use ($order, $product_ids) {
     foreach ($product_ids as $product_id) {
         $refresh();
         $order->submit($product_id);
     }
     return true;
 });

 echo $result ? 'Worked!' : 'Lock lost or never acquired.';
```

### Lock Queue Jobs Easily

[](#lock-queue-jobs-easily)

If you're running jobs on a Laravel queue, you may want to avoid queuing up the same job more than once at a time.

The `ThatsUs\RedLock\Traits\QueueWithoutOverlap` trait provides this functionality with very few changes to your job. Usually only two changes are necessary.

1. `use ThatsUs\RedLock\Traits\QueueWithoutOverlap` as a trait
2. Change the `handle()` method to `handleSync()`

```
use ThatsUs\RedLock\Traits\QueueWithoutOverlap;

class OrderProductJob
{
    use QueueWithoutOverlap;

    public function __construct($order, $product_id)
    {
        $this->order = $order;
        $this->product_id = $product_id;
    }

    public function handleSync()
    {
        $this->order->submit($this->product_id);
    }

}
```

Sometimes it's also necessary to specify a `getLockKey()` method. This method must return the string that needs to be locked.

This is typically unnecessary because the lock key can be generated automatically. But if the job's data is not easy to stringify, you must define the `getLockKey()` method.

This trait also provides a refresh method called `refreshLock()`. If `refreshLock()` is unable to refresh the lock, an exception is thrown and the job fails.

Finally, you can change the lock time-to-live from the default 300 seconds to another value using the `$lock_time` property.

```
use ThatsUs\RedLock\Traits\QueueWithoutOverlap;

class OrderProductsJob
{
    use QueueWithoutOverlap;

    protected $lock_time = 600; // 10 minutes in seconds

    public function __construct($order, array $product_ids)
    {
        $this->order = $order;
        $this->product_ids = $product_ids;
    }

    // We need to define getLockKey() because $product_ids is an array and the
    // automatic key generator can't deal with arrays.
    protected function getLockKey()
    {
        $product_ids = implode(',', $this->product_ids);
        return "OrderProductsJob:{$this->order->id}:{$product_ids}";
    }

    public function handleSync()
    {
        foreach ($this->product_ids as $product_id) {
            $this->refreshLock();
            $this->order->submit($product_id);
        }
    }

}
```

### Contribution

[](#contribution)

If you find a bug or want to contribute to the code or documentation, you can help by submitting an [issue](https://github.com/thatsus/laravel-redlock/issues) or a [pull request](https://github.com/thatsus/laravel-redlock/pulls).

### License

[](#license)

[MIT](http://opensource.org/licenses/MIT)

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity88

Battle-tested with a long release history

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~164 days

Recently: every ~199 days

Total

18

Last Release

635d ago

Major Versions

1.0.2 → v3.0.12017-05-30

v3.0.6 → v4.0.02019-11-15

4.0.7 → 5.0.02024-08-15

PHP version history (4 changes)1.0.0PHP &gt;=5.4.0

v4.0.0PHP &gt;=7.2.0

4.0.3PHP &gt;=8.1

5.0.0PHP &gt;=8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/433f082ba319521c024c40bb88fca7c2c922d43919692ab3e401a8427fc239b2?d=identicon)[ballyhooTech](/maintainers/ballyhooTech)

---

Top Contributors

[![dankuck](https://avatars.githubusercontent.com/u/277837?v=4)](https://github.com/dankuck "dankuck (22 commits)")[![rmertins](https://avatars.githubusercontent.com/u/5141216?v=4)](https://github.com/rmertins "rmertins (20 commits)")[![libi](https://avatars.githubusercontent.com/u/7769922?v=4)](https://github.com/libi "libi (11 commits)")[![jamieburnip](https://avatars.githubusercontent.com/u/5802560?v=4)](https://github.com/jamieburnip "jamieburnip (3 commits)")[![BrandonShar](https://avatars.githubusercontent.com/u/6599653?v=4)](https://github.com/BrandonShar "BrandonShar (2 commits)")[![KarmicXKoala](https://avatars.githubusercontent.com/u/29736440?v=4)](https://github.com/KarmicXKoala "KarmicXKoala (1 commits)")[![potsky](https://avatars.githubusercontent.com/u/408237?v=4)](https://github.com/potsky "potsky (1 commits)")[![evanBurg](https://avatars.githubusercontent.com/u/4584403?v=4)](https://github.com/evanBurg "evanBurg (1 commits)")

---

Tags

redlocklaravel redis lockredis lock

### Embed Badge

![Health badge](/badges/ballyhoo-technic-laravel-redlock/health.svg)

```
[![Health](https://phpackages.com/badges/ballyhoo-technic-laravel-redlock/health.svg)](https://phpackages.com/packages/ballyhoo-technic-laravel-redlock)
```

###  Alternatives

[thatsus/laravel-redlock

Redis distributed locks for laravel

53145.2k](/packages/thatsus-laravel-redlock)[pdffiller/qless-php

PHP Bindings for qless

29113.2k1](/packages/pdffiller-qless-php)[contributte/redis

Redis client integration into Nette framework

181.6M2](/packages/contributte-redis)[rtckit/react-redlock

Asynchronous distributed locks with Redis and ReactPHP

177.8k1](/packages/rtckit-react-redlock)

PHPackages © 2026

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