PHPackages                             freerkminnema/synchronized - 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. freerkminnema/synchronized

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

freerkminnema/synchronized
==========================

A Laravel package that provides a `synchronized` function that uses atomic locks to prevent a critical section of code from running in parallel across multiple requests.

3.0.0(2w ago)24.5k↑30.7%MITPHPPHP ^8.1CI passing

Since Jan 1Pushed 7mo ago2 watchersCompare

[ Source](https://github.com/freerkminnema/synchronized)[ Packagist](https://packagist.org/packages/freerkminnema/synchronized)[ RSS](/packages/freerkminnema-synchronized/feed)WikiDiscussions main Synced 2w ago

READMEChangelog (3)Dependencies (10)Versions (6)Used By (0)

```
                      _                     _             _
                     | |                   (_)           | |
 ___ _   _ _ __   ___| |__  _ __ ___  _ __  _ _______  __| |
/ __| | | | '_ \ / __| '_ \| '__/ _ \| '_ \| |_  / _ \/ _` |
\__ \ |_| | | | | (__| | | | | | (_) | | | | |/ /  __/ (_| |
|___/\__, |_| |_|\___|_| |_|_|  \___/|_| |_|_/___\___|\__,_|
      __/ |
     |___/                                       for Laravel

```

This package installs a global `synchronized` function into your Laravel application that ensures that the given callable is always executed one at a time, even if multiple requests are coming in.

So if your application receives ten requests in parallel, and part of your code is wrapped in the `synchronized` function, that block will be executed sequentially.

Requires a supported cache driver
---------------------------------

[](#requires-a-supported-cache-driver)

This function uses the Cache Atomic Locks feature of Laravel. To utilize this feature, your application must be using the `memcached`, `redis`, `dynamodb`, `database`, or `file` cache driver as your application's default cache driver. In addition, if relevant, all web servers must be communicating with the same central cache server.

Example usage
-------------

[](#example-usage)

In its most elegant form, you can pass a simple closure to the `synchronized` function:

```
$ticketNumber = synchronized(fn () => Cache::increment('ticket-number'));
```

How does it work?
-----------------

[](#how-does-it-work)

Internally, `synchronized` generates an *Atomic Lock Key* (which is simply a hashed string) based on the location of and variables in the callable. This is just like how the ✨magic✨ `once` function works.

Providing your own *Atomic Lock Key*
------------------------------------

[](#providing-your-own-atomic-lock-key)

In some cases, you may want to provide your own *Atomic Lock Key*. A contrived example is provided below:

```
$nameOnTicket = Request::get('name-on-ticket');
$ticket = synchronized(function () use ($nameOnTicket) {
    // This is bad, because everytime $nameOnTicket has a
    // different value, the Atomic Lock Key will be different.
    return [
        'name' => $nameOnTicket,
        'number' => Cache::increment('ticket-number'),
    ];
});
```

Everytime `$nameOnTicket` has a different value, a different *Atomic Lock Key* will be autogenerated, and it will probably not work as you intended.

To resolve this, you may tweak your closure to be less dependent on outside variables **OR** provide your own `$key` as the second variable.

```
$nameOnTicket = Request::get('name-on-ticket');
$ticket = synchronized(function () use ($nameOnTicket) {
    // Now it doesn't matter what the value of
    // $nameOnTicket is, since the Atomic Lock Key is fixed.
    return [
        'name' => $nameOnTicket,
        'number' => Cache::increment('ticket-number'),
    ];
}, 'atomic-ticket-number-increment');
```

Providing an Eloquent model as *Atomic Lock Key*
------------------------------------------------

[](#providing-an-eloquent-model-as-atomic-lock-key)

Alternatively, you may provide an instance of a saved Eloquent model to use as the *Atomic Lock Key*. This approach means the callback will be executed one at a time for every unique record in your database.

```
use App\Models\TicketDispenser;

$dispenser = TicketDispenser::find(Request::get('ticket-dispenser-id'));
$ticket = synchronized(fn () => $dispenser->nextTicket(), $dispenser);
```

###  Health Score

44

—

FairBetter than 90% of packages

Maintenance77

Regular maintenance activity

Popularity25

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity52

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

Total

4

Last Release

18d ago

Major Versions

1.0.0 → 2.0.02025-02-27

2.1.0 → 3.0.02026-07-30

### Community

Maintainers

![](https://www.gravatar.com/avatar/122868d1b9f790eef44c9c187be0719d717f2f8bd0c6b23d413b6c329fecfadd?d=identicon)[freerkminnema](/maintainers/freerkminnema)

---

Top Contributors

[![freerkminnema](https://avatars.githubusercontent.com/u/41089591?v=4)](https://github.com/freerkminnema "freerkminnema (31 commits)")

---

Tags

laravelsemaphoremutexparallellockatomiccritical-sectionsynchronizedconcurrency control

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/freerkminnema-synchronized/health.svg)

```
[![Health](https://phpackages.com/badges/freerkminnema-synchronized/health.svg)](https://phpackages.com/packages/freerkminnema-synchronized)
```

###  Alternatives

[laravolt/avatar

Turn name, email, and any other string into initial-based avatar or gravatar.

2.0k5.9M43](/packages/laravolt-avatar)[toin0u/geocoder-laravel

Geocoder Service Provider for Laravel

7515.5M20](/packages/toin0u-geocoder-laravel)[propaganistas/laravel-disposable-email

Disposable email validator

6023.2M7](/packages/propaganistas-laravel-disposable-email)[glorand/laravel-model-settings

Model Settings for your Laravel app

9171.2M4](/packages/glorand-laravel-model-settings)[harris21/laravel-fuse

Circuit breaker for Laravel queue jobs. Protect your workers from cascading failures.

46273.9k](/packages/harris21-laravel-fuse)[spatie/laravel-rdap

Perform RDAP queries in a Laravel app

72167.1k3](/packages/spatie-laravel-rdap)

PHPackages © 2026

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