Download the PHP package erikwang2013/snowflake-php without Composer

On this page you can find all versions of the php package erikwang2013/snowflake-php. 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 snowflake-php

Snowflake PHP


The mascot ships with the code too — echo Snowflake::MASCOT; prints it in any terminal.

A distributed unique ID generator based on Twitter's Snowflake algorithm, compatible with Laravel, Yii2, Yii3, Webman, ThinkPHP, and Hyperf.

日本語

About

Snowflake PHP generates 64-bit, k-ordered, globally unique IDs without requiring a central coordinator. Each ID is composed of a timestamp, datacenter ID, worker ID, and sequence number — allowing well over a million IDs per second per node with no database round-trips.

Key features:

Project Structure

Architecture

Four layers, with dependencies pointing in one direction only:

Feature Design

Features group into three domains: core (generation, bit allocation, parsing), extension (pluggable resolvers, clock-drift handling, framework adapters), and engineering (strict config validation, semantic exceptions, tests and release automation).

ID Lifecycle

Every id() call walks the same path:

  1. Read the clock and check for backward drift — tolerated up to clock_tolerance_ms; beyond that clock_drift_strategy decides whether to wait for the clock to catch up ('wait') or refuse to generate ('throw').
  2. Convert to an epoch offset and reject offsets that are negative or past the timestamp limit.
  3. Ask the sequence resolver for the next slot in this millisecond; when all 4096 slots are used, spin to the next millisecond and retry once.
  4. Assemble (offset << timestampShift) | fixedBits | sequence, advance lastTimestamp, and return the ID.

Instance state (lastTimestamp plus the resolver cursor) lives in memory and is never shared across processes or coroutines.

Requirements

Installation

Quick Start

With custom worker and datacenter IDs:

Configuration Reference

Key Type Default Description
epoch int 1704067200000 Custom epoch in ms (default: 2024-01-01 UTC)
worker_id int 0 Worker/node identifier
datacenter_id int 0 Datacenter identifier
worker_bits int 5 Bits for worker ID
datacenter_bits int 5 Bits for datacenter ID
sequence_bits int 12 Bits for sequence number
sequence_resolver string SequentialSequenceResolver FQCN of SequenceResolver
clock_tolerance_ms int 0 Max backward clock drift (0 = strict)
clock_drift_strategy string 'throw' 'throw' refuses to generate when the clock moves backwards beyond the tolerance; 'wait' spins until the wall clock catches up, giving up after clock_drift_wait_ms and then throwing ClockDriftException
clock_drift_wait_ms int 1000 How long the 'wait' strategy waits before giving up

Bit Layout

Default layout (63 data bits + 1 sign bit = 64 bits total):

Maximum lifespan with default epoch: ~69 years (until ~2093).

Every bit handed to the node id or the sequence is taken from the timestamp, so a wide sequence silently shortens the generator's life:

worker + datacenter + sequence bits timestamp bits usable lifespan
5 + 5 + 12 (default) 41 ~69.7 years
7 + 7 + 10 39 ~17.4 years
5 + 5 + 16 37 ~4.4 years
5 + 5 + 20 33 ~99 days

Ask for the limit of any layout:

Snowflake::lifespanMs(int $workerBits = 5, int $datacenterBits = 5, int $sequenceBits = 12): int returns the maximum timestamp offset in milliseconds for a layout; the arguments default to the default layout. Once the offset reaches that limit the epoch is exhausted — a stale epoch whose window has already closed makes the very first id() call throw TimestampOverflowException.

Using Configuration Array

Framework Integration

Laravel

The package supports Laravel auto-discovery. After installation:

  1. Publish the config (optional):

  2. Configure environment variables in .env:

  3. Use the Facade or dependency injection:

Webman

  1. Copy the plugin config to your project:

  2. Register a singleton in process.php or bootstrap:

  3. Usage:

ThinkPHP 6+

  1. Copy the config file to your project:

  2. Register the service in app/service.php:

  3. Usage:

Hyperf

  1. Publish the config:

  2. Register the DI binding in config/autoload/dependencies.php:

  3. Usage via constructor injection:

Yii2

  1. Copy the config file to your application:

  2. Register the generator as a container singleton in config/web.php (and config/console.php when the console generates IDs too):

  3. Usage:

The copied config reads the same SNOWFLAKE_* environment variables as the other adapters. The container config section needs Yii 2.0.11+.

Yii3

Yii3 wires itself up: composer.json declares the package config in extra.config-plugin, so the yiisoft/config plugin merges src/Adapters/Yii3/config/params.php and di.php into the application's params and di groups on install. No registration step.

  1. Optionally override the defaults in your application's config/common/params.php — the keys you list win over the package's, anything you leave out falls back to the generator's built-in defaults:

  2. Use the container or constructor injection:

params.php lists every available key with its default.

PSR-11 containers

Symfony, Slim, Laminas and any other container: register the factory. It depends on nothing, so any container works — psr/container is not required:

SnowflakeFactory::fromEnvironment() reads the same SNOWFLAKE_* variables the Laravel adapter uses. A PSR-11 container calls the factory object itself, so a Symfony service definition is one line:

Native PHP (no framework)

Nothing in this package needs a framework — the adapters above only wire Snowflake into a container for you. Without one, build it yourself:

A runnable version of this — including a framework-free lazy singleton and the invariants it checks — lives in docs/examples/plain-php.php:

Choosing a lifetime

The instance keeps lastTimestamp and the sequence cursor in memory, so how long it lives is the one thing to get right:

Runtime Build the instance
PHP-FPM, mod_php, CLI Inline, per request or command — nothing is shared between them.
Swoole, ReactPHP, RoadRunner, FrankenPHP Once per worker process, from the worker-start callback, with a unique (datacenter_id, worker_id) pair.

Never share one instance between coroutines or threads: id() reads and writes its own state, so two concurrent calls can interleave and hand out the same sequence number. Create one instance per coroutine, or guard the shared one with a mutex.

ID Parsing

Decompose a Snowflake ID into its components:

The datetime member is formatted with PHP's date() in the server's default timezone, so two hosts in different timezones render the same ID differently. timestamp_ms is the timezone-independent absolute value — compare that one when reconciling IDs across machines.

Sequence Resolvers

Three built-in implementations:

SequentialSequenceResolver (default)

Classic Snowflake behavior. Sequence starts at 0 each millisecond and increments sequentially. Guarantees monotonically increasing IDs within a single node.

RandomSequenceResolver

Starts each millisecond at a random sequence number, then increments. Less predictable than sequential IDs while keeping IDs within a millisecond monotonic.

Custom Resolver

Implement Erikwang2013\Snowflake\Contracts\SequenceResolver:

RedisSequenceResolver

The in-process resolvers keep the sequence in memory, so processes sharing a node id can hand out the same sequence number. RedisSequenceResolver keeps the counter in Redis instead — the one to use when several processes share a (datacenter_id, worker_id) pair:

__construct(object $client, string $keyPrefix = 'snowflake:seq:', int $ttlSeconds = 1) — the client is injected, so neither the redis extension nor Predis is required. A long TTL is safe: the counter then keeps growing inside the same millisecond, which correctly yields null until the next millisecond begins.

Exception Handling

Exception When
InvalidWorkerIdException Worker ID exceeds 2^worker_bits - 1
InvalidDatacenterIdException Datacenter ID exceeds 2^datacenter_bits - 1
ClockDriftException System clock moved backwards beyond tolerance
TimestampOverflowException Epoch has been exhausted (lifespan ended)
SnowflakeException Base exception for all package exceptions

Distributed Deployment

When running across multiple servers or processes, ensure each instance uses a unique (datacenter_id, worker_id) pair:

With the default 5+5 bit layout, you can support up to 32 datacenters × 32 workers = 1024 unique nodes.

To support more workers, adjust bit allocation:

Performance

IDs are generated entirely in-process with no external dependencies, so throughput is bounded by PHP's own microtime() call plus a handful of integer operations.

Measured on one core of a developer machine (PHP 8.3.7, Xdebug disabled, 300k iterations, best of 5):

Operation Throughput Per call
microtime(true) alone — the floor 10.3M/s 97 ns
id() — default 5+5+12 layout 1.6M/s 633 ns
id() + parseId() 282k/s 3.5 µs
Snowflake::fromConfig() 167k/s 6.0 µs

Generation costs about six times a bare clock call, and a node's sequence ceiling (4096 IDs/ms = 4.1M/s) stays well above what one PHP process can consume. Parsing and construction are diagnostic operations, not hot paths — build the instance once per process and keep parseId() out of tight loops.

Reproduce it on your own machine:

It prints ops/sec and ns/op against a bare microtime() baseline, best-of-N with the spread. Two things decide whether the absolute numbers mean anything: Xdebug (it can cost an order of magnitude — the header reports when it is loaded) and a busy or virtualised host, whose own clock call can dominate the measurement. Compare against the baseline rather than reading any single number as a promise.

Support Welcome

WeChat Pay Alipay

If this project helps you, feel free to show your support~


License

MIT — Copyright (c) 2026 erik https://erik.xyz


All versions of snowflake-php with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0
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 erikwang2013/snowflake-php contains the following files

Loading the files please wait ...