Snowflake implemented in GO (Golang)

snowflake

Snowflake is a fast, goroutine-safe unique ID generator built for distributed systems

GoDoc Go report Coverage

Key concepts

Snowflake

Snowflakes are int64s. uint64 is not used for interoperability reasons, and to allow for correct sorting. Snowflakes can be naturally sorted by time. The smaller the snowflake, the earlier it was created. A snowflake can be broken down into these parts:

  • 1 unused bit
  • timestamp (ms) since custom epoch (customisable size)
  • node id (customisable size)
  • counter (customisable size)

Due to the space restrictions of an int64, the timestamp, node id and counter must share 63 bits. It is recommended to plan for the future: the amount of bits allocated for the timestamp should be as large as possible. A simple formula to determine how many bits to allocate is this: ceil(log2(years * 3,154e+10)). years represents how long in the future you wish for the ids to generate properly.

Node

A Node generates IDs - it is recommended to make use of one Node per machine, although there are situations where it may also be acceptable to use more.

A Node coordinates the generation of IDs. It maintains a counter to ensure that IDs are unique. A Node also stores a custom epoch, which should be defined by the user.

Examples

ID Generation

The following example allocates:

  • 43 bits timestamp (valid for 278.7 years + customEpoch)
  • 10 bits node id (1024 nodes)
  • 10 bits counter (max. 1024 snowflakes/ms/node)
node, err := snowflake.NewNode(0, customEpoch, 43, 10, 10)
if err != nil {
    panic(err)
}

id := node.Generate()

Base64

Snowflakes can be converted to base64 (mathematical base using 0-9a-zA-Z+/). This is more space-efficient than base64 because it excludes padding.

// Snowflake to base64
s := snowflake.Snowflake(4294967296)
fmt.Println(s.Base64()) // 400000

// Base64 to snowflake
s = snofwlake.ParseBase64("400000")
fmt.Println(s) // 4294967296

Benchmarks

To test the performance on your local machine, clone this repository and run go test -bench=..

cpu: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
BenchmarkNode_Generate-12       51937034                23.24 ns/op            0 B/op          0 allocs/op
BenchmarkParseBase64-12         34151838                35.10 ns/op            0 B/op          0 allocs/op
Similar Resources

Golang implementation of the Optimal Reciprocal Collision Avoidance (ORCA) algorithm

Golang implementation of the Optimal Reciprocal Collision Avoidance (ORCA) algorithm

go-orca Golang implementation of the Optimal Reciprocal Collision Avoidance (ORCA) algorithm Disclaimer This project is under active development and i

Nov 22, 2022

Snowflake grafana datasource plugin allows Snowflake data to be visually represented in Grafana dashboards.

Snowflake grafana datasource plugin allows Snowflake data to be visually represented in Grafana dashboards.

Snowflake Grafana Data Source With the Snowflake plugin, you can visualize your Snowflake data in Grafana and build awesome chart. Get started with th

Dec 29, 2022

Snowflake - Simple twitter's snowflake uniquely identifiable descriptors (IDs) format generator for Go

Snowflake Dead simple and fast Twitter's snowflake id generator in Go. Installation go get github.com/HotPotatoC/snowflake Usage Generating a snowflak

Oct 6, 2022

Snowflake - An implement of snowflake by go, use atomic instead of mutex

snowflake an implement of snowflake by go, use atomic instead of mutex 雪花算法的一种go

Feb 4, 2022

Snowflake implemented in GO (Golang)

snowflake Snowflake is a fast, goroutine-safe unique ID generator built for distributed systems Key concepts Snowflake Snowflakes are int64s. uint64 i

Oct 27, 2022

A simple to use Go (golang) package to generate or parse Twitter snowflake IDs

snowflake snowflake is a Go package that provides A very simple Twitter snowflake generator. Methods to parse existing snowflake IDs. Methods to conve

Dec 30, 2022

❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).

❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).

❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).

Dec 14, 2022

✨ Generate unique IDs (Port of Node package "generate-snowflake" to Golang)

✨ Generate Snowflake Generate unique IDs. Inspired by Twitter's Snowflake system. 📦 Installation Initialize your project (go mod init example.com/exa

Feb 11, 2022

Golang wrapper for the Snowflake Api.

GoSnowflakeApi GoSnowflakeApi is an api wrapper for the snowflake api written in golang for golang developers. Example package main import ( "fmt

Jul 25, 2021

A distributed unique ID generator inspired by Twitter's Snowflake

Sonyflake is a distributed unique ID generator inspired by Twitter's Snowflake.

Jan 2, 2023

Snowflake By Go.

snow Snowflake by Go. package main import "github.com/czasg/snow" func main() { // default WorkID & DataCenterID is 0. snowID := snow.Next()

Dec 4, 2022

Snowflake algorithm generation worker Id sequence

sequence snowflake algorithm generation worker Id sequence 使用雪花算法生成ID,生成100万个只需要

Jan 21, 2022

A network service for generating unique ID numbers inspired by Twitter's Snowflake.

Hanabira Hanabira is a network service for generating unique ID numbers inspired by Twitter's Snowflake. How to run hanabira-cluster and etcd-cluster

Jan 13, 2022

A CLI tool implemented by Golang to manage `CloudComb` resource

CloudComb CLI tool: comb Get Started comb is a CLI tool for manage resources in CloudComb base on cloudcomb-go-sdk. Support Mac, Linux and Windows. We

Jan 4, 2021

High-performance minimalist queue implemented using a stripped-down lock-free ringbuffer, written in Go (golang.org)

This project is no longer maintained - feel free to fork the project! gringo A high-performance minimalist queue implemented using a stripped-down loc

Oct 24, 2022

CHANGELOG generator implemented in Go (Golang).

CHANGELOG generator implemented in Go (Golang).

git-chglog CHANGELOG generator implemented in Go (Golang). Anytime, anywhere, Write your CHANGELOG. Table of Contents git-chglog Table of Contents Fea

Jan 7, 2023

Snake game implemented in golang

little_pineapple(Snake game implemented in golang) 贪吃蛇golang实现 Snake game implemented in golang 数据结构:链表&数组 Data structures used: linked list&array 使用方

Aug 18, 2020

communicate with iOS devices implemented with Golang

Golang-iDevice much more easy to use 👉 electricbubble/gidevice-cli Installation go get github.com/electricbubble/gidevice Devices package main impor

Dec 18, 2022
Snowflake - An implement of snowflake by go, use atomic instead of mutex

snowflake an implement of snowflake by go, use atomic instead of mutex 雪花算法的一种go

Feb 4, 2022
Snowflake implemented in GO (Golang)

snowflake Snowflake is a fast, goroutine-safe unique ID generator built for distributed systems Key concepts Snowflake Snowflakes are int64s. uint64 i

Oct 27, 2022
A simple to use Go (golang) package to generate or parse Twitter snowflake IDs

snowflake snowflake is a Go package that provides A very simple Twitter snowflake generator. Methods to parse existing snowflake IDs. Methods to conve

Dec 30, 2022
❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).
❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).

❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).

Dec 14, 2022
✨ Generate unique IDs (Port of Node package "generate-snowflake" to Golang)

✨ Generate Snowflake Generate unique IDs. Inspired by Twitter's Snowflake system. ?? Installation Initialize your project (go mod init example.com/exa

Feb 11, 2022
Golang wrapper for the Snowflake Api.

GoSnowflakeApi GoSnowflakeApi is an api wrapper for the snowflake api written in golang for golang developers. Example package main import ( "fmt

Jul 25, 2021
A network service for generating unique ID numbers inspired by Twitter's Snowflake.

Hanabira Hanabira is a network service for generating unique ID numbers inspired by Twitter's Snowflake. How to run hanabira-cluster and etcd-cluster

Jan 13, 2022
An extremely fast UUID alternative written in golang

Overview WUID is a globally unique number generator, while it is NOT a UUID implementation. WUID is 10-135 times faster than UUID and 4600 times faste

Dec 9, 2022
An extremely fast UUID alternative written in golang

Overview WUID is a globally unique number generator, while it is NOT a UUID implementation. WUID is 10-135 times faster than UUID and 4600 times faste

May 10, 2021
UUID package for Golang

UUID package for Go language This package provides pure Go implementation of Universally Unique Identifier (UUID). Supported both creation and parsing

Dec 9, 2021