Download the PHP package bradietilley/php-snowflake without Composer

On this page you can find all versions of the php package bradietilley/php-snowflake. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-snowflake

Laravel Snowflake

Snowflake IDs in PHP

Static Analysis Tests PHP Version

Introduction

Snowflake IDs come in various flavours. All variants of Snowflake IDs provide a distributed, time-based unique identifier system that avoids the pitfalls of traditional auto-incrementing IDs, and provides a cleaner alternative to large and unwieldy ULIDs/UUIDs. They enable efficient sorting, reduce database contention, and work well in distributed environments without requiring a single centralized coordination (to an extent at least).

PHP-Snowflake implements a high-precision Snowflake ID generation system that prioritizes timestamp accuracy over extensive sequencing. By using a microsecond-precise timestamp, it significantly reduces the chances of generating sequentially enumerable IDs, a common issue with standard Snowflake and ULID implementations when producing multiple IDs per second or millisecond. It should go without saying, but sequential IDs for public viewing are typically frowned upon.

This implementation dedicates 50 bits to the timestamp, leaving only 4 bits for sequencing, allowing up to 16 unique IDs per microsecond. The ID structure follows a compact schema:

By focusing on timestamp granularity, PHP-Snowflake provides a balance between uniqueness, speed, and distributed scalability while reducing the likelihood of predictable ID sequencing.

Feature Auto-Inc IDs UUIDs ULIDs Traditional Snowflake IDs Snowflake IDs (This package)
Easy to read/copy/relay ✅ Yes ❌ No ❌ No ✅ Yes ✅ Yes
Stored as BIGINT ✅ Yes ❌ No (String/Binary) ❌ No (String/Binary) ✅ Yes ✅ Yes
Hides table count ❌ No ✅ Yes ✅ Yes ✅ Yes ✅ Yes
Globally scalable ❌ No ✅ Yes ✅ Yes ✅ Yes ✅ Yes
Protects enumeration... ❌ No ✅ Yes (ms) ✅ Yes (ms) ✅ Yes (ms) ✅ Yes (μs)
Sortable by time ✅ Yes ✅ Yes (if time-sorted) ✅ Yes ✅ Yes ✅ Yes
Suitable for Primary/Foreign Key ✅ Yes ❌ No ❌ No ✅ Yes ✅ Yes
Can be sent as number in JSON ✅ Yes (until 2^53) ❌ String ❌ String ❌ String ❌ String

Installation

Documentation

Generating a Snowflake ID

To generate a unique Snowflake ID, simply call:

This will produce a 64-bit integer, uniquely identifying the event, with microsecond precision.

Configuring the Snowflake Generator

This package supports 35 years of IDs with microsecond precision. To maximize the lifespan of your Snowflake IDs, you should set the epoch (start time) to a recent timestamp.

Since the timestamp component of the Snowflake ID is based on the time elapsed since the epoch, choosing an older epoch unnecessarily reduces the system’s usable lifespan. A fresh epoch ensures the ID generator will remain valid for decades.

You can configure the epoch, as well as the cluster ID and worker ID, using the configure() method:

Understanding the Configuration Parameters:

⚠️ Important: The Epoch Should Never Change

Concurrency

Within a single process, it's nearly impossible for two Snowflake IDs to have the same timestamp. However, in high-concurrency environments where multiple requests occur simultaneously, it's possible for multiple IDs to share the same timestamp, potentially causing conflicts. This is where sequencing is used.

While avoiding sequencing is preferred to reduce ID predictability, it becomes necessary when generating multiple IDs within the same microsecond.

To ensure uniqueness across concurrent requests, you should use a sequence resolver that implements locking. While a cache-based lock may be slower than traditional database Auto Increment IDs, it enables global scalability, where a single database column is not sufficient.

By default, PHP Snowflake will use a FileResolver which implements locking, however it will point to a file within this package (for guaranteed readability from the php process). Should you need to, you can configure the path or swap it out entirely (it's recommended to explicitly configure this).

See Laravel Snowflake package for Laravel-specific sequence resolvers.

Testing

You may wish to override the ID generator in test environments. You can do this by swapping out the IdentifierResolver with your own:

Author


All versions of php-snowflake with dependencies

PHP Build Version
Package Version
Requires php Version ^8.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package bradietilley/php-snowflake contains the following files

Loading the files please wait ....