Twitter's snowflake algorithm

Source: Internet
Author: User

Twitter's engineer Snowflake also proposed a method of generating a unique sequence in a distributed system;

The UUID in Java is also a desirable method, and his disadvantage is that the serial number is too long.

The most primitive form of the snowflake algorithm is written in the Scala language https://github.com/twitter/snowflake/releases/tag/snowflake-2010

The core code for its Idworker this class implementation, its principle structure is as follows, respectively, with a 0 to represent a bit, with-split open part of the role:

1 0---00000000000000000000 0000000000 0000000000 0 --- 00000 ---00000---000000000000

In the above string, the first bit is unused (which can actually also be used as a long symbol bit), the next 41 bits are the millisecond time, then the 5-bit datacenter identifies the bit, the 5-bit machine ID (not the identifier, is actually the thread identity), and then 12 bits within the millisecond of the current count of milliseconds, Add up to just 64 bits, for a long type.

Java implementations:

 Packagemine;Importutil. Trie;/**Snowflake*/ Public classIdworker {Private Final LongTwepoch = 1288834974657L; Private Final LongWorkeridbits = 5L; Private Final LongDatacenteridbits = 5L; Private Final LongMaxworkerid = -1l ^ ( -1l <<workeridbits); Private Final LongMaxdatacenterid = -1l ^ ( -1l <<datacenteridbits); Private Final LongSequencebits = 12L; Private Final LongWorkeridshift =sequencebits; Private Final LongDatacenteridshift = Sequencebits +workeridbits; Private Final LongTimestampleftshift = sequencebits + workeridbits +datacenteridbits; Private Final LongSequencemask = -1l ^ ( -1l <<sequencebits); Private LongWorkerid; Private LongDatacenterid; Private Longsequence = 0L; Private LongLasttimestamp = -1l;  PublicIdworker (LongWorkerid,LongDatacenterid) {    if(Workerid > Maxworkerid | | Workerid < 0) {      Throw NewIllegalArgumentException (String.Format ("worker Id can ' t is greater than%d or less than 0", Maxworkerid)); }    if(Datacenterid > Maxdatacenterid | | Datacenterid < 0) {      Throw NewIllegalArgumentException (String.Format ("Datacenter Id can ' t is greater than%d or less than 0", Maxdatacenterid)); }     This. Workerid =Workerid;  This. Datacenterid =Datacenterid; }    Public synchronized LongNextID () {Longtimestamp =Timegen (); if(Timestamp <Lasttimestamp) {      Throw NewRuntimeException (String.Format ("Clock moved backwards. Refusing to generate ID for%d milliseconds ", Lasttimestamp-timestamp)); }    if(Lasttimestamp = =timestamp) {Sequence= (sequence + 1) &Sequencemask; if(Sequence = = 0) {timestamp=Tilnextmillis (Lasttimestamp); }    } Else{sequence= 0L; } Lasttimestamp=timestamp; return((Timestamp-twepoch) << timestampleftshift) | (Datacenterid << Datacenteridshift) | (Workerid << Workeridshift) |sequence; }   protected LongTilnextmillis (LongLasttimestamp) {    Longtimestamp =Timegen ();  while(Timestamp <=Lasttimestamp) {Timestamp=Timegen (); }    returntimestamp; }   protected LongTimegen () {returnSystem.currenttimemillis (); }   Private StaticTrie trie=NewTrie ();  Public Static voidMain (string[] args) {LongTIME0 =System.currenttimemillis (); Idworker Idworker=NewIdworker (0, 0);  for(inti = 0; i < 3000000; i++) {          LongID =Idworker.nextid (); Trie.insert (""+ID); //System.out.println (ID);      }      LongTime = (int) ((System.currenttimemillis ()-TIME0)/1000); System.out.println ("Time:" +time+ "s Conflict:" +trie.count); }}
View Code

Test: 1 million data are not in conflict;

 Packagemine;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;Importutil. Trie; Public classIdworkertestextendsthread{Private StaticTrie trie=NewTrie (); Private StaticObject lock =NewObject (); Private Static intCount=0; StaticIdworker worker =NewIdworker (0,0);  Public voidrun () {LongSquence =Worker.nextid ();        System.out.println (squence); synchronized(lock) {Trie.insert (""+squence); Count++; if(count%100==0) {System.out.println ("Generate section" +count+ "Data conflict count:" +trie.count); }        }    }         Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intN =10000; Executorservice Executor=Executors.newfixedthreadpool (n);  for(inti=0;i<100*n;i++) {thread thread=Newidworkertest ();        Executor.execute (thread);        } executor.shutdown ();  while(!executor.isterminated ()) {} System.out.println ("Generate section" +count+ "Data conflict count:" +trie.count); System.out.println ("Done"); }}
View Code

Twitter's snowflake algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.