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.

2.1.0(4mo ago)23.5k—10%MITPHPPHP ^8.1CI passing

Since Jan 1Pushed 4mo ago2 watchersCompare

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

READMEChangelog (3)Dependencies (5)Versions (5)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

42

—

FairBetter than 90% of packages

Maintenance74

Regular maintenance activity

Popularity25

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity50

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

Total

3

Last Release

146d ago

Major Versions

1.0.0 → 2.0.02025-02-27

### 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

[symfony/lock

Creates and manages locks, a mechanism to provide exclusive access to a shared resource

514127.6M459](/packages/symfony-lock)[laravolt/avatar

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

2.0k5.4M31](/packages/laravolt-avatar)[recca0120/laravel-parallel

64116.7k](/packages/recca0120-laravel-parallel)[interaction-design-foundation/laravel-geoip

Support for multiple Geographical Location services.

17221.0k3](/packages/interaction-design-foundation-laravel-geoip)[zerkalica/semaphore

This library provides an api for semaphore acquire and release

1119.0k1](/packages/zerkalica-semaphore)[pudongping/hyperf-wise-locksmith

A mutex library provider for the Hyperf framework, designed to enable serialized execution of PHP code in high-concurrency scenarios.

106.3k2](/packages/pudongping-hyperf-wise-locksmith)

PHPackages © 2026

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