15 Mayıs 2023 Pazartesi

Twitter Snowflake Unique ID Generator

Giriş
Twitter Snowflake  servisini durdurdu. Ancak entellektüel açıdan ilginç olabilir diye not almak istedim.
Şeklen şöyle
| 1 bit | 41 bits   | 5 bits         | 5 bits     | 12 bits         |
|-------|-----------|----------------|------------|-----------------|
| 0     | timestamp | data center id | machine id | sequence number |
Açıklaması şöyle
1. Sign Bit:
The sign bit is never used. Its value is always zero. This bit is just taken as a backup that will be used at some time in the future.

2. Timestamp Bits:
This time is the epoch time. Previously the benchmark time for calculating the time elapsed was from 1st Jan 1970. But Twitter changed this benchmark time to 4th November 2010.

3. Data center Bits:
5 bits are reserved for this, which means that there can be (2⁵) = 32 data centers.

4. Machine ID Bits:
5 bits are reserved for this, which again means that there can be (2⁵) = 32 machines per data center.

5. Sequence No Bits:
These bits are kept to ensure the uniqueness property to be maintained when multiple requests land on the same machine on the same data center at the same timestamp. So, these bits are used for generating sequence numbers for IDs that are generated at the same timestamp. The sequence number is reset to zero at every millisecond. Since we have reserved 12 bits for this, we can have (2¹²) = 4096 sequence numbers which are certainly more than the IDs that are generated every millisecond by every single machine.
Timestamp Bits
Açıklaması şöyle
41 bits which gives us 69 years for any custom epoch
Sequence No Bits:
Açıklaması şöyle
The sequence number is incremented by 1 for every ID generated on the machine. Resets to 0 every millisecond. In theory, we can support 2¹² = 4096 new IDs every millisecond.
Similar Approaches Based On Snowflake
Açıklamasıı şöyle
Baidu UID generator
Sonyflake




Hiç yorum yok:

Yorum Gönder