Skip to content

3n0ugh/snowflake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

snowflake

Here is the twitter blog about snowflake.

Screen Shot 2022-04-12 at 10 15 06

  • The first bit is an unused assigned bit.
  • The second part consists of a 41-bit timestamp (milliseconds) whose value is the offset of the current time relative to a certain time.
  • The 5 bits of the third and fourth parts represent the data center and worker node, and the max value is
    2^5-1 = 31.
  • The last part consists of 8 bits, which means the length of the serial number generated per millisecond per working node, a maximum of 2^8-1 = 4095 IDs can be generated in the same millisecond.
  • In a distributed environment, a five-bit data center and worker mean that can deploy 31 data centers. Each data center can deploy up to 31 nodes.
  • The binary length of 41 bits is at most 2^41-1 millisecond = 69 years. So the snowflake algorithm can be used for up to 69 years.

Usage

   // Create a node
   n, err := snowflake.NewNode(30, 3)
   if err != nil {
      fmt.Println(err)
   }
    
   // Then, generate a id
   id, err := n.Generate()
   if err != nil {
      fmt.Println(err)
   }

   fmt.Printf("ID: %d\n", id)
   fmt.Printf("String: %s\n", id.String())
   fmt.Printf("Uint64: %d\n", id.UInt64())
   
   fmt.Printf("DecomposeID: %v\n", snowflake.DecomposeID(id))

Test and Benchmarking

  • Test:
  go test -v . 
  • Benchmark:
  go test -bench=. -count=10 -benchtime=2s . 

Attention

  • If you need to handle IDs in Javascript, use a string instead of uint64. Because Javascript's maximum integer value you can safely store 53 bits.

About

Unique id generator based on Twitter Snowflake

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages