How to Generate Distributed Unique IDs with Snowflake in PHP
Learn the fundamentals of Twitter's Snowflake distributed ID algorithm, its 64‑bit structure, and step‑by‑step PHP implementation—including installation via Composer, basic usage, custom data‑center and worker IDs, start timestamps, Laravel integration, and custom sequence resolvers—to generate globally unique, time‑ordered identifiers.
Introduction
In distributed systems a globally unique identifier is often required. Snowflake, an open‑source algorithm from Twitter, generates 64‑bit IDs that are unique, roughly time‑ordered, and do not rely on a central server. This article explains the algorithm and shows a PHP implementation.
Snowflake Structure
Snowflake IDs consist of 64 bits, divided as follows:
1 bit sign | 41 bits timestamp | 10 bits worker ID | 12 bits sequenceSign bit : Always 0 to ensure the ID is a positive integer.
Timestamp : 41 bits representing the current time in milliseconds, allowing about 69 years of timestamps.
Worker ID : 10 bits to distinguish nodes; the high 5 bits can be a data‑center ID and the low 5 bits a machine ID, supporting up to 32 data centers with 32 machines each.
Sequence : 12 bits to resolve collisions within the same millisecond, permitting up to 4096 IDs per millisecond per node.
Installation
composer require godruoyi/php-snowflake -vvvBasic Usage
<?php
/**
* @desc Snowflake example
*/
require '../vendor/autoload.php';
$snowflake = new \Godruoyi\Snowflake\Snowflake;
echo $snowflake->id() . PHP_EOL;Sample output:
663985260597348711Specifying Data‑Center and Worker IDs
$datacenterId = time();
$workerId = '1000000000000001';
$snowflake = new \Godruoyi\Snowflake\Snowflake($datacenterId, $workerId);
echo $snowflake->id() . PHP_EOL;Sample output:
663986323404837079Setting a Custom Start Timestamp
$snowflake = new \Godruoyi\Snowflake\Snowflake;
$snowflake->setStartTimeStamp(strtotime('2020-08-15') * 1000);
echo $snowflake->id() . PHP_EOL;Sample output:
528938994126294755Advanced Usage
Using in Laravel
Integrate Snowflake via a service provider:
// App\Providers\AppServiceProvider
use Godruoyi\Snowflake\Snowflake;
use Godruoyi\Snowflake\LaravelSequenceResolver;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('snowflake', function ($app) {
return (new Snowflake())
->setStartTimeStamp(strtotime('2019-10-10') * 1000)
->setSequenceResolver(new LaravelSequenceResolver($app->get('cache.store')));
});
}
}Custom Sequence Resolver
Implement the Godruoyi\Snowflake\SequenceResolver interface:
class YourSequence implements SequenceResolver
{
public function sequence(int $currentTime)
{
// simple test implementation
return mt_rand(0, 1);
}
}
// usage
$snowflake->setSequenceResolver(new YourSequence);
$snowflake->id();Or provide a closure directly:
$snowflake = new \Godruoyi\Snowflake\Snowflake;
$snowflake->setSequenceResolver(function ($currentTime) {
static $lastTime;
static $sequence;
if ($lastTime == $currentTime) {
++$sequence;
} else {
$sequence = 0;
}
$lastTime = $currentTime;
return $sequence;
})->id();Conclusion
The article presented the Snowflake algorithm’s 64‑bit layout and demonstrated a complete PHP implementation, including Composer installation, basic generation, custom data‑center/worker IDs, start timestamps, Laravel integration, and custom sequence resolvers. Snowflake provides a reliable way to generate globally unique, monotonically increasing identifiers for distributed applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
