Giter Site home page Giter Site logo

snowfake's Introduction

Snowfake

Build Status GoDoc Coverage Go Report Card

Snowfake is just a Twitter Snowflake IDs alternative for generating unique short ID at high scale.

Overview

The objective of Snowfake is to generate smaller ID compare with the original Snowflake. It's suitable for generating short IDs at scale. Case study: short link eg. bit.ly

Snowfake also guarantees lifetime 136 years (original Snowflake 69 years). By default all IDs will be ran out in 2159. (Epoch is configurable).

By default, a Snowfake ID is composed of

32 bits for time in units of second
5 bits for node/machine numbers (configurable)
27 bits for sequence numbers (configurable)

With this configuration, Snowfake allows you to generate 2^27 unique IDs per second per machine. It also provides 2^5 distributed machines works together without additional config.

Both machine numbers and sequence numbers are configurable. To achieve smaller ID you can minimize bits allocation by reducing node bits and sequence bits.

Installation

go get github.com/deryrahman/snowfake

Usage

package main

import (
	"github.com/deryrahman/snowfake"
)

func main() {
	nodeID := uint64(1)
	sf, _ := snowfake.New(nodeID)

	sfID := sf.GenerateID()
	println(snowfake.EncodeBase58(sfID)) // encoding base58
}

To generate smaller ID, tune the node bits and seq bits numbers as small as possible

package main

import (
	"github.com/deryrahman/snowfake"
)

func main() {
	snowfake.SetEpoch(1577836800) // timestamp start from 01/01/2020 @ 12:00am (UTC)
	snowfake.SetNodeBits(1)       // reserve 2^1 machine numbers
	snowfake.SetSeqBits(4)        // ~2^4 unique ID per second
	_ = snowfake.Init()           // must be called to instantiate new config

	nodeID := uint64(1)
	sf, _ := snowfake.New(nodeID)

	sfID := sf.GenerateID()
	println(snowfake.EncodeBase58(sfID)) // encoding base58
}

Contribute

Just make a pull request with a clear description โ˜•๏ธ

snowfake's People

Contributors

deryrahman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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