Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does shortflakes use WORKER_ID and DATACENTER_ID? #46

Open
webard opened this issue Feb 28, 2024 · 5 comments
Open

Does shortflakes use WORKER_ID and DATACENTER_ID? #46

webard opened this issue Feb 28, 2024 · 5 comments

Comments

@webard
Copy link

webard commented Feb 28, 2024

Hello,

I use shortflakes in my app due to compatibility with laravel nova and some other apps (it is just simpler in Javascript API clients) and I wonder that Shortflakes use WORKER_ID and DATACENTER_ID from config?

In this code:

public function toShortflakeId(int $currentTime, int $sequenceId)
{
return ($currentTime << self::SEQUENCE_BITS) | ($sequenceId);
}

i do not see logic that is present in toSnowflakeId method.

So how Shortflake works for now, what are benefits from using shortflakes?

Best

@kra8
Copy link
Owner

kra8 commented Feb 29, 2024

Hello,

Thank you for using this library. In the current implementation, the values of WORKER_ID and DATACENTER_ID are not used in generating Shortflake IDs, and IDs are primarily generated based on time and sequence ID.

Generating shorter IDs can be convenient, especially when dealing with long integers in JavaScript clients. However, as a trade-off, the number of unique IDs that can be generated per millisecond is reduced, and the ability to distribute ID generation across multiple workers and data centers is lost.

If your application requires the use of WORKER_ID and DATACENTER_ID in a more distributed environment or needs to increase the uniqueness of the IDs, please consider using the toSnowflakeId method instead and managing the IDs as strings.

Please let us know if you have any other questions or if there's anything else we can help with.

@webard
Copy link
Author

webard commented Feb 29, 2024

What do you think about this implementation: https://samifayoumi.ca/blog/001_53bit-snowflakeid ? It is possible to cut-off sequence id to introduce at least worker id

@kra8
Copy link
Owner

kra8 commented Mar 1, 2024

I found this implementation very interesting. Introducing the worker ID by truncating the sequence ID, as proposed, seems like a good idea to enhance the scalability of the system.

I would love to incorporate this mechanism into this library, but currently, I'm having difficulty finding enough time. If you're in a hurry, I would appreciate it if you could submit a PR.

@webard
Copy link
Author

webard commented Mar 1, 2024

I can manage this, but how name this implementation? I wouldn't change the current implementation of Shortflake, but introduce something like TaggedShortflake, ScalableShortflake, ShortflakeV2, Snowflake53? Do you have an idea for the best fit?

@kra8
Copy link
Owner

kra8 commented Mar 4, 2024

I couldn't think of a good name. Instead, we could keep the name as it is and introduce the new implementation as a major update, marking it as version 3.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants