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
<palign="center">Generate X/Twitter <ahref="https://en.wikipedia.org/wiki/Snowflake_ID">Snowflake identifiers</a> in Laravel.</p>
6
5
7
-
This package enables a Laravel application to create [Snowflake IDs](https://en.wikipedia.org/wiki/Snowflake_ID).
8
-
It is a very thin wrapper around the excellent [godruoyi/php-snowflake](https://github.com/godruoyi/php-snowflake) library.
9
6
10
7
## What are Snowflakes?
11
8
12
9
Snowflakes are a form of unique identifier devised by X/Twitter and are used by many companies, including Instagram and Discord, to generate unique IDs for their entities.
13
10
14
11
Some of the benefits of using Snowflakes (over alternatives such as UUID/ULID) include:
15
12
16
-
- They consist entirely of integers.
17
-
- They use less space (16 characters, so it fits in a `BIGINT`).
18
-
- Indexing of integers is much faster than indexing a string.
19
-
- Keys begin with a timestamp, so are sortable.
20
-
- Keys end with a random number, so guessing table size is not possible.
21
-
- Databases handle integers more efficiently than strings.
22
-
- Generation of new keys is faster (less than 1 ms).
13
+
-**Timestamp Component:** Extract creation time directly from the ID.
14
+
-**Uniqueness Across Distributed Systems:** Ensures unique IDs without coordination.
15
+
-**Orderability:** Roughly ordered by creation time for easy sorting.
16
+
-**Compactness:** 64-bit size, more compact than 128-bit UUIDs.
17
+
-**Performance:** Faster and less resource-intensive generation.
18
+
-**Configurability:** Flexible bit allocation for specific needs.
19
+
-**Storage Efficiency:** More efficient storage compared to larger identifiers.
20
+
-**Database Indexing:** Faster indexing and query performance.
21
+
-**Human Readability:** More compact and readable than longer identifiers.
23
22
24
23
## Installation
25
24
26
-
Pull in the package using Composer:
25
+
First pull in the package using Composer:
27
26
28
27
```bash
29
28
composer require calebdw/laraflake
30
29
```
31
30
32
-
## Configuration
33
-
34
-
Snowflake includes a configuration file with several settings that you can use to initialize the Snowflake service.
35
-
You should begin by publishing this configuration file:
31
+
And then publish the package's configuration file:
The 41-bit timestamp encoded in the Snowflake is the difference between the time of creation and a given starting epoch/timestamp.
44
42
Snowflakes can be generated for up to 69 years past the given epoch.
45
-
46
-
The default epoch is `2024-01-01`, but in most cases you should set this value to the current date using a format of `YYYY-MM-DD`.
43
+
In most cases you should set this value to the current date using a format of `YYYY-MM-DD`.
47
44
48
45
> **Note**:
49
-
> Do not set the timestamp to a date in the future, as that won't achieve anything.
50
-
> You should also avoid using a date far in the past (such as the Unix epoch `1970-01-01`), as that may reduce the number of years for which you can generate timestamps.
46
+
> Future dates will throw an error and you should avoid using a date far in the past (such as the Unix epoch `1970-01-01`)
47
+
as that may reduce the number of years for which you can generate timestamps.
51
48
52
49
### Data Center & Worker IDs
53
50
54
-
If using a distributed architectural setup, you'll need to set the data center and worker IDs that the application should use when generating Snowflakes.
55
-
These are both set to `0` by default, as that is a good starting point, but you are free to increase these numbers as you add more workers and data centers.
56
-
57
-
The maximums for each of these configuration values is `31`. This gives you up to 32 workers per data center, and 32 data centers in total.
58
-
Therefore, you can have up `1024` workers each generating unique Snowflakes.
59
-
60
-
### Sequence resolver
61
-
62
-
In order to handle the generation of unique keys within the same millisecond, the service uses a sequence resolver.
63
-
There are several to choose from, however they each have dependencies, such as Redis.
64
-
You are free to use any of them, however the default option is a good choice, as it **doesn't** have any dependencies.
51
+
If using distributed systems, you'll need to set the data center and worker IDs that the application should use when generating Snowflakes.
52
+
These are used to ensure that each worker generates unique Snowflakes and can range from `0` to `31` (up to `1024` unique workers).
65
53
66
54
## Usage
67
55
68
-
> **WARNING**: Do not create new instances of the Snowflake service, as doing so risks generating matching keys / introducing collisions.
69
-
> Instead, always resolve the Snowflake singleton out of the container. You can also use the global helper method (see below).
56
+
> **WARNING**: Do not create new instances of the Snowflake generator (as this could cause collisions), always use the Snowflake singleton from the container.
70
57
71
-
72
-
You can generate a Snowflake by resolving the service out of the container and calling its `id` method:
73
-
Since this is a little cumbersome, the package also registers a global `snowflake()` helper method that you can use anywhere.
58
+
You can generate a Snowflake by resolving the singleton from the container and calling its `id` method:
Please see [godruoyi/php-snowflake](https://github.com/godruoyi/php-snowflake) for more information on the available sequence resolvers and their dependencies.
211
+
172
212
## Contributing
173
213
174
214
Thank you for considering contributing! You can read the contribution guide [here](CONTRIBUTING.md).
@@ -180,3 +220,4 @@ Laraflake is open-sourced software licensed under the [MIT license](LICENSE).
180
220
## Acknowledgements
181
221
182
222
Derived from [caneara/snowflake](https://github.com/caneara/snowflake) which is no longer maintained.
223
+
The actual Snowflake generation is handled by the excellent [godruoyi/php-snowflake](https://github.com/godruoyi/php-snowflake) library.
0 commit comments