All Projects → bwmarrin → Snowflake

bwmarrin / Snowflake

Licence: bsd-2-clause
A simple to use Go (golang) package to generate or parse Twitter snowflake IDs

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Labels

Projects that are alternatives of or similar to Snowflake

IdGenerator
💎迄今为止最全面的分布式主键ID生成器。 💎优化的雪花算法(SnowFlake)——雪花漂移算法,在缩短ID长度的同时,具备极高瞬时并发处理能力(50W/0.1s)。 💎原生支持 C#/Java/Go/Rust/C/SQL 等多语言,且提供 PHP 扩展及 Python、Node.js、Ruby 多线程安全调用动态库(FFI)。💎支持容器环境自动扩容(自动注册 WorkerId ),单机或分布式唯一IdGenerator。💎顶尖优化,超强效能。
Stars: ✭ 548 (-58.3%)
Mutual labels:  snowflake
Sqlpad
Web-based SQL editor run in your own private cloud. Supports MySQL, Postgres, SQL Server, Vertica, Crate, ClickHouse, Trino, Presto, SAP HANA, Cassandra, Snowflake, BigQuery, SQLite, and more with ODBC
Stars: ✭ 4,113 (+213.01%)
Mutual labels:  snowflake
Tbls
tbls is a CI-Friendly tool for document a database, written in Go.
Stars: ✭ 940 (-28.46%)
Mutual labels:  snowflake
dbd
dbd is a database prototyping tool that enables data analysts and engineers to quickly load and transform data in SQL databases.
Stars: ✭ 30 (-97.72%)
Mutual labels:  snowflake
Snowflake
Twitter的分布式自增ID雪花算法snowflake (Java版)
Stars: ✭ 336 (-74.43%)
Mutual labels:  snowflake
Snowflake
❄️ A React-Native Android iOS Starter App/ BoilerPlate / Example with Redux, RN Router, & Jest with the Snowflake Hapi Server running locally or on RedHat OpenShift for the backend, or a Parse Server running locally or remotely on Heroku
Stars: ✭ 4,576 (+248.25%)
Mutual labels:  snowflake
pre-commit-dbt
🎣 List of `pre-commit` hooks to ensure the quality of your `dbt` projects.
Stars: ✭ 149 (-88.66%)
Mutual labels:  snowflake
Locopy
locopy: Loading/Unloading to Redshift and Snowflake using Python.
Stars: ✭ 73 (-94.44%)
Mutual labels:  snowflake
Mindsdb
Predictive AI layer for existing databases.
Stars: ✭ 4,199 (+219.56%)
Mutual labels:  snowflake
Winterlayout
WinterLayout with the help of y = a*sin(x) function
Stars: ✭ 25 (-98.1%)
Mutual labels:  snowflake
tellery
Tellery lets you build metrics using SQL and bring them to your team. As easy as using a document. As powerful as a data modeling tool.
Stars: ✭ 219 (-83.33%)
Mutual labels:  snowflake
Distributedid
基于twitter的雪花算法(SnowFlake)来产生分布式ID,支持SDK、HTTP方式接入
Stars: ✭ 273 (-79.22%)
Mutual labels:  snowflake
Idgen
Twitter Snowflake-alike ID generator for .Net
Stars: ✭ 581 (-55.78%)
Mutual labels:  snowflake
growthbook
Open Source Feature Flagging and A/B Testing Platform
Stars: ✭ 2,342 (+78.23%)
Mutual labels:  snowflake
Redissnowflake
Twitter's Snowflake based ID generation as a redis module
Stars: ✭ 66 (-94.98%)
Mutual labels:  snowflake
monoton
Highly scalable, single/multi node, sortable, predictable and incremental unique id generator with zero allocation magic on the sequential generation
Stars: ✭ 21 (-98.4%)
Mutual labels:  snowflake
Neural
高并发、高可用的微服务架构中的分布式治理利刃,提供了分布式限流、服务降级、熔断器、重试器等容错特性,并提供了SPI、过滤器和JWT等功能。此外还提供了很多小的黑科技(如:IP黑白名单、UUID加强版、Snowflake和大并发时间戳获取等)。
Stars: ✭ 373 (-71.61%)
Mutual labels:  snowflake
Sonyflake Rs
🃏 A distributed unique ID generator inspired by Twitter's Snowflake.
Stars: ✭ 91 (-93.07%)
Mutual labels:  snowflake
Sql Runner
Run templatable playbooks of SQL scripts in series and parallel on Redshift, PostgreSQL, BigQuery and Snowflake
Stars: ✭ 68 (-94.82%)
Mutual labels:  snowflake
Gonet
go分布式服务器,基于内存mmo
Stars: ✭ 804 (-38.81%)
Mutual labels:  snowflake

snowflake

GoDoc Go report Coverage Build Status Discord Gophers

snowflake is a Go package that provides

  • A very simple Twitter snowflake generator.
  • Methods to parse existing snowflake IDs.
  • Methods to convert a snowflake ID into several other data types and back.
  • JSON Marshal/Unmarshal functions to easily use snowflake IDs within a JSON API.
  • Monotonic Clock calculations protect from clock drift.

For help with this package or general Go discussion, please join the Discord Gophers chat server.

Status

This package should be considered stable and completed. Any additions in the future will strongly avoid API changes to existing functions.

ID Format

By default, the ID format follows the original Twitter snowflake format.

  • The ID as a whole is a 63 bit integer stored in an int64
  • 41 bits are used to store a timestamp with millisecond precision, using a custom epoch.
  • 10 bits are used to store a node id - a range from 0 through 1023.
  • 12 bits are used to store a sequence number - a range from 0 through 4095.

Custom Format

You can alter the number of bits used for the node id and step number (sequence) by setting the snowflake.NodeBits and snowflake.StepBits values. Remember that There is a maximum of 22 bits available that can be shared between these two values. You do not have to use all 22 bits.

Custom Epoch

By default this package uses the Twitter Epoch of 1288834974657 or Nov 04 2010 01:42:54. You can set your own epoch value by setting snowflake.Epoch to a time in milliseconds to use as the epoch.

Custom Notes

When setting custom epoch or bit values you need to set them prior to calling any functions on the snowflake package, including NewNode(). Otherwise the custom values you set will not be applied correctly.

How it Works.

Each time you generate an ID, it works, like this.

  • A timestamp with millisecond precision is stored using 41 bits of the ID.
  • Then the NodeID is added in subsequent bits.
  • Then the Sequence Number is added, starting at 0 and incrementing for each ID generated in the same millisecond. If you generate enough IDs in the same millisecond that the sequence would roll over or overfill then the generate function will pause until the next millisecond.

The default Twitter format shown below.

+--------------------------------------------------------------------------+
| 1 Bit Unused | 41 Bit Timestamp |  10 Bit NodeID  |   12 Bit Sequence ID |
+--------------------------------------------------------------------------+

Using the default settings, this allows for 4096 unique IDs to be generated every millisecond, per Node ID.

Getting Started

Installing

This assumes you already have a working Go environment, if not please see this page first.

go get github.com/bwmarrin/snowflake

Usage

Import the package into your project then construct a new snowflake Node using a unique node number. The default settings permit a node number range from 0 to 1023. If you have set a custom NodeBits value, you will need to calculate what your node number range will be. With the node object call the Generate() method to generate and return a unique snowflake ID.

Keep in mind that each node you create must have a unique node number, even across multiple servers. If you do not keep node numbers unique the generator cannot guarantee unique IDs across all nodes.

Example Program:

package main

import (
	"fmt"

	"github.com/bwmarrin/snowflake"
)

func main() {

	// Create a new Node with a Node number of 1
	node, err := snowflake.NewNode(1)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Generate a snowflake ID.
	id := node.Generate()

	// Print out the ID in a few different ways.
	fmt.Printf("Int64  ID: %d\n", id)
	fmt.Printf("String ID: %s\n", id)
	fmt.Printf("Base2  ID: %s\n", id.Base2())
	fmt.Printf("Base64 ID: %s\n", id.Base64())

	// Print out the ID's timestamp
	fmt.Printf("ID Time  : %d\n", id.Time())

	// Print out the ID's node number
	fmt.Printf("ID Node  : %d\n", id.Node())

	// Print out the ID's sequence number
	fmt.Printf("ID Step  : %d\n", id.Step())

  // Generate and print, all in one.
  fmt.Printf("ID       : %d\n", node.Generate().Int64())
}

Performance

With default settings, this snowflake generator should be sufficiently fast enough on most systems to generate 4096 unique ID's per millisecond. This is the maximum that the snowflake ID format supports. That is, around 243-244 nanoseconds per operation.

Since the snowflake generator is single threaded the primary limitation will be the maximum speed of a single processor on your system.

To benchmark the generator on your system run the following command inside the snowflake package directory.

go test -run=^$ -bench=.

If your curious, check out this commit that shows benchmarks that compare a few different ways of implementing a snowflake generator in Go.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].