jetstreamlabs/snowflake

An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).


README

68747470733a2f2f7777772e706e676b65792e636f6d2f706e672f66756c6c2f3130352d313035323233355f736e6f77666c616b652d706e672d7472616e73706172656e742d6261636b67726f756e642d736e6f77666c616b652d776974682d636c6561722d6261636b67726f756e642e706e67

An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).

Description

Snowflake is a network service for generating unique ID numbers at high scale with some simple guarantees.

  • The first bit is unused sign bit.
  • The second part consists of a 41-bit timestamp (milliseconds) whose value is the offset of the current time relative to a certain time.
  • The 5 bits of the third and fourth parts represent data center and worker, and max value is 2^5 -1 = 31.
  • The last part consists of 12 bits, its means the length of the serial number generated per millisecond per working node, a maximum of 2^12 -1 = 4095 IDs can be generated in the same millisecond.
  • In a distributed environment, five-bit datacenter and worker mean that can deploy 31 datacenters, and each datacenter can deploy up to 31 nodes.
  • The binary length of 41 bits is at most 2^41 -1 millisecond = 69 years. So the snowflake algorithm can be used for up to 69 years, In order to maximize the use of the algorithm, you should specify a start time for it.

You must know, The ID generated by the snowflake algorithm is not guaranteed to be unique. For example, when two different requests enter the same node of the same data center at the same time, and the sequence generated by the node is the same, the generated ID will be duplicated.

So if you want use the snowflake algorithm to generate unique ID, You must ensure: The sequence-number generated in the same millisecond of the same node is unique. Based on this, we created this package and integrated multiple sequence-number providers into it.

  • RandomSequenceResolver (Random)
  • RedisSequenceResolver (based on redis psetex and incrby)
  • LaravelSequenceResolver (based on redis psetex and incrby)
  • SwooleSequenceResolver (based on swoole_lock)

Each provider only needs to ensure that the serial number generated in the same millisecond is different. You can get a unique ID.

Requirement

  1. PHP >= 8.0
  2. Composer

Installation

$ composer require jetstreamlabs/snowflake 

Useage

Coming soon ...

Advanced

Coming soon ...

License

MIT

Ported and enhanced from the Godruoyi repository package.

Original Godruoyi License file included in LICENSES directory.