PHPackages                             moves/snowflake - 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. moves/snowflake

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

moves/snowflake
===============

3.0.0(3y ago)05.4kMITPHPPHP ^8.1

Since Sep 8Pushed 2y ago2 watchersCompare

[ Source](https://github.com/vetmoves/com.moves.php.snowflake)[ Packagist](https://packagist.org/packages/moves/snowflake)[ RSS](/packages/moves-snowflake/feed)WikiDiscussions 1.x Synced 3w ago

READMEChangelogDependencies (7)Versions (23)Used By (0)

Snowflake
=========

[](#snowflake)

🚨 WARNINGS 🚨
------------

[](#-warnings-)

This package is only compatible with 64-bit installations of PHP.

This package has not been tested against incredibly strenuous loads such as the actual traffic seen regularly by Twitter.

Introduction
------------

[](#introduction)

This is a PHP implementation of Twitter's Snowflake, Sony's Sonyflake, and our own custom Modelflake ID generator.

Traits are also included which allow you to directly and automatically generate IDs on Eloquent Model classes.

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

[](#installation)

To add this library into your project, run:

```
composer require moves/snowflake

```

Usage
-----

[](#usage)

### Eloquent Models

[](#eloquent-models)

#### Prerequisites

[](#prerequisites)

For each separate deployment of your application, be sure to set a unique value for the `SNOWFLAKE_MACHINE_ID`environment key.

The default implementation of the provided Eloquent Snowflake generator traits requires you to have a configured Cache driver.

🚨 **It is important to note that the `file` Cache driver is not supported for deployments on distributed infrastructure such as "Serverless"/AWS Lambdas.** 🚨

Snowflake IDs rely on the creation of a unique sequence number. On traditional deployment systems where each "environment" necessitates exactly one physical or virtual server instance, atomic sequence number generation is only necessary between threads on each individual server. However, with modern deployment systems where a single "environment" may consist of any number of physical or virtual servers, atomic locking must be ensured across all server instances. This is only possible via a dedicated microservice, or via a shared cache system.

#### Usage

[](#usage-1)

To use the ID generator of your choice on your Eloquent Model classes, simply add the corresponding trait to your Model.

`Moves\Snowflake\Traits\EloquentTwitterSnowflakeId` for Twitter Snowflake

`Moves\Snowflake\Traits\EloquentSonyflakeId` for Twitter Snowflake

`Moves\Snowflake\Traits\EloquentModelflakeId` for Twitter Snowflake

or

`Moves\Snowflake\Traits\EloquentSnowflakeId` to specify your own custom ID Generator class

```
use Illuminate\Database\Eloquent\Model;
use Moves\Snowflake\Traits\EloquentTwitterSnowflakeId;

public class MyModel extends Model
{
    use EloquentTwitterSnowflakeId;
}

```

On `create`, your model class will automatically generate a unique ID and apply it to your model instance before it is inserted into the database.

Note that JavaScript only supports up to 53-bit integers, and thus integer snowflake IDs will not be processed as the correct value when retrieved as data from your API. As such, all of the included ID generation traits also automatically add string casting on your model's key field so snowflake IDs are returned as strings when using `toArray` or `toJson`.

#### Overriding Default Sequence Number Generation

[](#overriding-default-sequence-number-generation)

The supplied traits provide a default implementation for generating a unique sequence number, relying on Laravel's Cache facade and atomic locking. However, you can override the default implementation by providing your own function closure which returns a unique sequence number. See [Providing Your Own Generator](#Providing-Your-Own-Generator) for more details.

#### Providing Your Own Generator

[](#providing-your-own-generator)

To provide your own generator class, use the `Moves\Snowflake\Traits\EloquentSnowflakeId` trait, and implement the following method header:

```
public function getSnowflakeGenerator(): ISnowflakeGenerator
{
    //TODO: Implement
}

```

You can build your own class which implements the `ISnowflakeGenerator` interface, or you can provide an instance of one of the generators from this package with alternate values passed to the constructor.

For example, this is how you might provide your own sequence number generator function to the TwitterSnowflake generator:

```
public function getSnowflakeGenerator(): ISnowflakeGenerator
{
    return new TwitterSnowflakeGenerator(
        $this->_getMachineId(),
        function (): int {
            // Closure containing your custom sequence number generation logic
        }
    );
}

```

### Direct Usage

[](#direct-usage)

This package is still usable outside of Eloquent model classes. Instantiate and call the appropriate generator class wherever you need it:

```
$generator = new TwitterSnowflakeGenerator(
    $machineId,
    $sequenceNumberClosure
);

$id = $generator->generate;

```

You can also use the generators to parse an existing Snowflake ID into its component parts:

```
$parts = $generator->parse($snowflake);
// Expected Output:
// [timestamp] => 1634677016824 (ms)
// [machine] => 1
// [sequence] => 1

```

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity69

Established project with proven stability

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

Recently: every ~75 days

Total

23

Last Release

918d ago

Major Versions

0.0.0 → 1.0.0-rc.12021-10-20

1.x-dev → 2.0.02022-09-07

2.x-dev → 3.0.02023-04-10

PHP version history (2 changes)0.0.0PHP ^7.3|^8.0

2.0.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/89610601?v=4)[moves-ext-devs](/maintainers/moves-ext-devs)[@moves-ext-devs](https://github.com/moves-ext-devs)

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/moves-snowflake/health.svg)

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

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M11](/packages/renatomarinho-laravel-page-speed)[illuminate/pipeline

The Illuminate Pipeline package.

9348.3M267](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10533.5M989](/packages/illuminate-pagination)[illuminate/redis

The Illuminate Redis package.

8314.4M362](/packages/illuminate-redis)[illuminate/cookie

The Illuminate Cookie package.

224.5M132](/packages/illuminate-cookie)

PHPackages © 2026

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