PHPackages                             andp97/laravel-locky - 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. andp97/laravel-locky

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

andp97/laravel-locky
====================

A simple Laravel package for distributed lock

v0.1(7mo ago)20[4 PRs](https://github.com/andp97/laravel-locky/pulls)MITPHPPHP ^8.4|^8.3CI passing

Since Sep 30Pushed 1mo agoCompare

[ Source](https://github.com/andp97/laravel-locky)[ Packagist](https://packagist.org/packages/andp97/laravel-locky)[ Docs](https://github.com/andp97/laravel-locky)[ GitHub Sponsors]()[ RSS](/packages/andp97-laravel-locky/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (12)Versions (6)Used By (0)

[![locky-image.png](locky-image.png)](locky-image.png)

A simple Laravel package for distributed lock
=============================================

[](#a-simple-laravel-package-for-distributed-lock)

[![Latest Version on Packagist](https://camo.githubusercontent.com/baf1f90a344c6a5bfc2242057cae6c2942d0bd79aa549f3a855d026ea669af38/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616e647039372f6c61726176656c2d6c6f636b792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andp97/laravel-locky)[![GitHub Tests Action Status](https://camo.githubusercontent.com/9353b3ac5efa2e2e08c39ae591807c0356e777a84d95ae93586fbd472dfb816e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616e647039372f6c61726176656c2d6c6f636b792f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/andp97/laravel-locky/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/40cc3c2484879b57d6d19d5745dd14be7a58be0959e014b781f8825733319ec9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616e647039372f6c61726176656c2d6c6f636b792f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/andp97/laravel-locky/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/c5f763be36356292149156ac526b3ea8610b69a13bc9be4a0b00c8c32653ade2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616e647039372f6c61726176656c2d6c6f636b792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andp97/laravel-locky)

A simple Laravel package for distributed lock around Redis atomic locks with retry + backoff (with jitter). It’s easy to read, chainable, and testable.

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

[](#installation)

You can install the package via composer:

```
composer require andp97/laravel-locky
```

You can publish the config file with:

```
php artisan vendor:publish --tag="laravel-locky-config"
```

Usage
-----

[](#usage)

### Basic

[](#basic)

```
use Pavons\Locky\Facades\Locky;

$result = Locky::make("widgets:{$id}")
    ->ttl(10)
    ->run(function () use ($id) {
        processWidget($id);
        return true;
    });
```

#### With retries, exponential backoff, and **full jitter**

[](#with-retries-exponential-backoff-and-full-jitter)

```
use Pavons\Locky\Facades\Locky;

Locky::make("widgets:{$orderId}")
    ->ttl(12)
    ->attempts(7)
    ->baseDelayMs(75)     // 75, 150, 300, 600, 1200, 2000…
    ->multiplier(2.0)
    ->maxDelayMs(2000)
    ->jitter('full')      // none|equal|full
    ->onRetry(function ($key, $attempt, $sleepMs) {
        logger()->notice("Retrying lock", compact('key', 'attempt', 'sleepMs'));
    })
    ->onFail(function ($key, $attempts) {
        // Optional graceful fallback
        report(new \RuntimeException("Lock failed for {$key} after {$attempts} attempts"));
        return null; // Returning here prevents throwing; omit to throw
    })
    ->run(function () use ($orderId) {
        settleOrder($orderId);
    });
```

#### In a queued job (recommended)

[](#in-a-queued-job-recommended)

```
public function handle(): void
{
    Locky::make("job:import:{$this->batchId}")
        ->ttl(15)
        ->attempts(8)
        ->jitter('equal')
        ->run(function () {
            $this->performImport(); // exclusive section
        });
}
```

### Notes &amp; best practices

[](#notes--best-practices)

- **Use Redis** (`CACHE_DRIVER=redis`) so `Cache::lock()` is truly distributed.
- **Pick TTL &gt; worst-case critical section**, but keep it tight; split long work to keep the locked part short.
- **Jitter**:

    - `full`: best at smoothing thundering herds (default).
    - `equal`: narrows variance while avoiding sync.
    - `none`: only if you *really* want deterministic waits.
- Hooks (`onRetry`, `onFail`) make it easy to add logs/metrics without cluttering business code.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Andrea Pavone](https://github.com/andp97)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance79

Regular maintenance activity

Popularity3

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 88.9% 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

Unknown

Total

1

Last Release

224d ago

### Community

Maintainers

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

---

Top Contributors

[![andp97](https://avatars.githubusercontent.com/u/17993831?v=4)](https://github.com/andp97 "andp97 (16 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

laravelAndrea Pavonelaravel-locky

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/andp97-laravel-locky/health.svg)

```
[![Health](https://phpackages.com/badges/andp97-laravel-locky/health.svg)](https://phpackages.com/packages/andp97-laravel-locky)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M627](/packages/spatie-laravel-data)[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)

PHPackages © 2026

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