Giter Site home page Giter Site logo

snowflake's Introduction

smantic/snowflake

I thought it was weird that existing implementations of snowflake ID didn't utilize all 64 bits So I wrote a library that implements a signed timestamp snowflake ID.

format

There are only 63 bits used in this reference, the missing bit is the most significant bit, which is the sign bit.

We think of the timestamps as the number of milliseconds since a chosen epoch, so generally the timestamps in snowflake IDs are unsigned. The problem arises when we consider Some languages ( JavaScript ) which don't have unsigned int types. If we have a unsigned timestamp that uses the MSB, all the sudden our snowflake ID is a negative number, which breaks our sorting.

for an epoch of 2015

-Something < something < something
2090 < 2020 < 2021

One thing you could do is just start using signed timestamps and set the epoch way in the future. So I decided to implement a library showing this for fun.

Spaces

42 bit timestamp = ~140 years
41 Bit timestamp = ~70 Years

We lose 1/2 of our time when using 41 bits. Twitter and discord probably wont be around in 70 years, so it's likely that it wont matter. To put that in perspective Turing created the Turing Machine 85 years ago.

Signed timestamps

Signed timestamps are actually a thing but you may not have seen or used them. A negative timestamp indicates a time before your selected epoch.

To take full advantage of all 42 bits on a signed timestamp, we can set the epoch to be 69 years in advance.

Other options

We can also use a signed timestamp, but allocate a bit else where in the snowflake structure. For example we could use 13 bits instead of 12 for the sequence, allowing for 4096 (2x) more unique IDs per timestamp per machine/instance.

snowflake's People

Contributors

smantic 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.