Coder Social home page Coder Social logo

snowflake's Introduction

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.

snowflake's People

Contributors

3n0ugh avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.