You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Snowflake is a network service for generating unique ID numbers at high scale with some simple guarantees.
31
+
Snowflake is a network service that generates unique ID numbers at high scale with simple guarantees.
32
32
33
-
* The first bit is unused sign bit.
34
-
* The second part consists of a 41-bit timestamp (milliseconds) whose value is the offset of the current time relative to a certain time.
35
-
* The 5 bits of the third and fourth parts represent data center and worker, and max value is 2^5 -1 = 31.
36
-
* 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.
37
-
* In a distributed environment, five-bit datacenter and worker mean that can deploy 31 datacenters, and each datacenter can deploy up to 31 nodes.
38
-
* 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.
33
+
1. The first bit is an unused sign bit.
34
+
2. The second part consists of a 41-bit timestamp (in milliseconds) representing the offset of the current time relative to a certain reference time.
35
+
3. The third and fourth parts are represented by 5 bits each, indicating the data centerID and workerID. The maximum value for both is 31 (2^5 -1).
36
+
4. The last part consists of 12 bits, which represents the length of the serial number generated per millisecond per working node. A maximum of 4095 IDs can be generated in the same millisecond (2^12 -1).
39
37
40
-
> You must know, The ID generated by the snowflake algorithm is not guaranteed to be unique.
41
-
> 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.
38
+
If you want to generate unique IDs using the snowflake algorithm, you must ensure that sequence numbers generated within the same millisecond on the same node are unique.
39
+
Based on this requirement, we have created this package which integrates multiple sequence number providers.
42
40
43
-
If you want to 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.
44
-
Based on this, we created this package and integrated multiple sequence-number providers into it.
* RedisSequenceResolver (based on redis psetex and incrby, **Concurrency Safety**)
49
-
* LaravelSequenceResolver (based on Laravel Cache [add](https://github.com/laravel/framework/blob/11.x/src/Illuminate/Contracts/Cache/Repository.php#L39) lock)
50
-
* SwooleSequenceResolver (based on swoole_lock)
51
-
* PredisSequenceResolver (based on redis psetex and incrby, **Concurrency Safety**)
52
-
53
-
Each provider only needs to ensure that the serial number generated in the same millisecond is different. You can get a unique ID.
54
-
55
-
56
-
> [!NOTE]
57
-
> If you want to use RedisSequenceResolver, please install the [redis](https://pecl.php.net/package/redis) extension:
58
-
> pecl install redis
59
-
>
60
-
> If you want to use SwooleSequenceResolver, please install the swoole extension:
61
-
> pecl install swoole
62
-
>
63
-
> If you want to use PredisSequenceResolver, please install the [predis/predis](https://github.com/predis/predis) package:
64
-
> composer install predis/predis
65
-
66
-
> **Warning**
67
-
> The RandomSequenceResolver does not guarantee that the generated IDs are unique, If you want to generate a unique ID, please use another resolver instead.
> The maximum value of a 41-bit timestamp (in milliseconds) can represent up to 69 years, so the Snowflake algorithm can run safely for 69 years. In order to make the most of it, we recommend setting a start time.
97
+
108
98
4. Use Sonyflake
109
99
110
100
```php
@@ -116,7 +106,7 @@ $sonyflake->id();
116
106
117
107
1. Used in Laravel.
118
108
119
-
Because the SDK is relatively simple, we don't provide an extension for Laravel. You can quickly integrate it into Laravel in the following way.
109
+
Since the SDK is quite straightforward, we do not offer a specific extension for Laravel. However, you can easily integrate it into your Laravel project by following these steps.
120
110
121
111
```php
122
112
// App\Providers\AppServiceProvider
@@ -144,7 +134,7 @@ class AppServiceProvider extends ServiceProvider
144
134
145
135
2. Custom
146
136
147
-
You can customize the sequence-number resolver by implementing the Godruoyi\Snowflake\SequenceResolver interface.
137
+
To customize the sequencenumber resolver, you need to implement the Godruoyi\Snowflake\SequenceResolver interface.
0 commit comments