Snowflake ID

From HandWiki - Reading time: 3 min

Short description: Unique identifiers used by Twitter
Template:TwitterSnowflake/gen
Snowflake ID
Other namesTwitter Snowflake
Components of a snowflake identifier in binary

Snowflake IDs, or snowflakes, are a form of unique identifier used in distributed computing. The format was created by Twitter and is used for the IDs of tweets. It is popularly believed that every snowflake has a unique structure, so they took the name "snowflake ID". The format has been adopted by other companies, including Discord and Instagram. The Mastodon social network uses a modified version.

Format

Snowflakes are 64 bits in binary. (Only 63 are used to fit in a signed integer.) The first 41 bits are a timestamp, representing milliseconds since the chosen epoch. The next 10 bits represent a machine ID, preventing clashes. Twelve more bits represent a per-machine sequence number, to allow creation of multiple snowflakes in the same millisecond. The final number is generally serialized in decimal.[1]

Snowflakes are sortable by time, because they are based on the time they were created.[1] Additionally, the time a snowflake was created can be calculated from the snowflake. This can be used to get snowflakes (and their associated objects) that were created before or after a particular date.[2]

Fixed header format
Offsets Octet 0 1 2 3
Octet Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 0 Timestamp
4 32 Machine ID Machine Sequence Number

Example

A tweet produced by @Wikipedia in June 2022[3] has the snowflake ID 1541815603606036480. The number may be converted to binary as 00 0001 0101 0110 0101 1010 0001 0001 1111 0110 0010 00|01 0111 1010|0000 0000 0000, with pipe symbols denoting the three parts of the ID.

  • The first 41 (+ 1 top zero bit) bits convert to decimal as 367597485448. Add the value to the Twitter Epoch of 1288834974657 (in Unix time milliseconds),[4] the Unix time of the tweet is therefore 1656432460.105: June 28, 2022 16:07:40.105 UTC.
  • The middle 10 bits 01 0111 1010 are the machine ID.
  • The last 12 bits decode to all zero, meaning this tweet is the first tweet processed by the machine at the given millisecond.

Usage

The format was first announced by Twitter in June 2010.[5] Due to implementation challenges, they waited until later in the year to roll out the update.[6]

  • Twitter uses snowflake IDs for tweets, direct messages, users, lists, and all other objects available over the API.[7]
  • Discord also uses snowflakes, with their epoch set to the first second of the year 2015.[2]
  • Instagram uses a modified version of the format, with 41 bits for a timestamp, 13 bits for a shard ID, and 10 bits for a sequence number.[8]
  • Mastodon's modified format has 48 bits for a millisecond-level timestamp, as it uses the UNIX epoch. The remaining 16 bits are for sequence data.[9]

See also

References

  1. 1.0 1.1 "twitter-archive/snowflake at b3f6a3c6ca". October 1, 2012. https://github.com/twitter-archive/snowflake/tree/b3f6a3c6ca8e1b6847baa6ff42bf72201e2c2231. 
  2. 2.0 2.1 "API Reference". Discord. https://discord.com/developers/docs/reference#snowflakes. 
  3. @Wikipedia. "53 years ago today, members of the LGBTQI+ community began protesting in New York City in response to a police raid of the Stonewall Inn, a popular gay bar. The riots were a transformative event in the 20th century fight for LGBTQI+ rights in the US. (1/2)". https://twitter.com/Wikipedia/status/1541815603606036480.  Missing or empty |date= (help)
  4. "2019-08-03: TweetedAt: Finding Tweet Timestamps for Pre and Post Snowflake Tweet IDs". https://ws-dl.blogspot.com/2019/08/2019-08-03-tweetedat-finding-tweet.html. 
  5. King, Ryan (June 1, 2010). "Announcing Snowflake". Twitter. https://blog.twitter.com/engineering/en_us/a/2010/announcing-snowflake.html. 
  6. Siegler, MG (October 12, 2010). "Tweet IDs About To Get Jumbled In A Blizzard As Snowflake Is Set To Roll Live". https://techcrunch.com/2010/10/12/twitter-snowflake/. 
  7. "Twitter IDs". Twitter. https://developer.twitter.com/en/docs/twitter-ids. 
  8. "Sharding & IDs at Instagram" (in en). May 2, 2016. https://instagram-engineering.com/sharding-ids-at-instagram-1cf5a71e5a5c. 
  9. Source Code mastodon/mastodon, Mastodon, 2022-11-11, https://github.com/mastodon/mastodon/blob/5e796dc6f85b37c8378fe01cfd8ac23222c89eea/lib/mastodon/snowflake.rb, retrieved 2022-11-11 

External links




Licensed under CC BY-SA 3.0 | Source: https://handwiki.org/wiki/Snowflake_ID
15 views | Status: cached on July 16 2024 17:44:58
↧ Download this article as ZWI file
Encyclosphere.org EncycloReader is supported by the EncyclosphereKSF