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.
Download erikwang2013/snowflake-php
More information about erikwang2013/snowflake-php
Files in erikwang2013/snowflake-php
Package snowflake-php
Short Description A distributed unique ID generator based on Twitter's Snowflake algorithm, compatible with Laravel, Webman, ThinkPHP, and Hyperf.
License MIT
Informations about the package snowflake-php
Snowflake PHP
A distributed unique ID generator based on Twitter's Snowflake algorithm, compatible with Laravel, Webman, ThinkPHP, and Hyperf.
中文文档请参阅 README.zh-CN.md
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 tens of thousands of IDs per second per node with no database round-trips.
Key features:
- Pure PHP, zero dependencies — no extensions or external services required
- Pluggable sequence resolvers — built-in sequential and random strategies, or bring your own
- Flexible bit allocation — adjust timestamp/worker/datacenter/sequence bits to fit your scale
- Clock drift tolerance — configurable tolerance window for NTP adjustments
- Framework agnostic with first-class adapters for Laravel, ThinkPHP, Webman, and Hyperf
- ID parsing — decompose generated IDs back into timestamp, node, and sequence components
Requirements
- PHP >= 8.0
- 64-bit system (required for native 64-bit integer operations)
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) |
Bit Layout
Default layout (63 data bits + 1 sign bit = 64 bits total):
Maximum lifespan with default epoch: ~69 years (until ~2093).
Using Configuration Array
Framework Integration
Laravel
The package supports Laravel auto-discovery. After installation:
-
Publish the config (optional):
-
Configure environment variables in
.env: - Use the Facade or dependency injection:
Webman
-
Copy the plugin config to your project:
-
Register a singleton in
process.phpor bootstrap: - Usage:
ThinkPHP 6+
-
Copy the config file to your project:
-
Register the service in
app/service.php: - Usage:
Hyperf
-
Publish the config:
-
Register the DI binding in
config/autoload/dependencies.php: - Usage via constructor injection:
ID Parsing
Decompose a Snowflake ID into its components:
Sequence Resolvers
Two 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 with a random sequence number. Makes IDs less predictable (prevents enumeration) but IDs within the same millisecond are not monotonic.
Custom Resolver
Implement Snowflake\Contracts\SequenceResolver:
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
Typical throughput on modern hardware: ~500,000 IDs/second (single process).
IDs are generated purely in-process with no external dependencies. The primary bottleneck is PHP's microtime() call and integer bit operations, both of which are O(1).
开源不易,欢迎支持
| 微信 | 支付宝 |
|---|---|
License
MIT — Copyright (c) 2026 erik https://erik.xyz