fastid

Snowflake ID generating in Go

APACHE-2.0 License

Stars
112

FastID -- Snowflake ID generating in Go

FastID is a pluggable unique ID generator in Go.

  • Under 64 bits (Long Integer)
  • K-Ordered
  • Lock-free (using atomic CAS)
  • Decentralized

Installation

go get github.com/beinan/fastid

Quick Start

Generate an ID


import (
  "fmt"
  "github.com/beinan/fastid"
)

func ExampleGenInt64ID() {
  id := fastid.CommonConfig.GenInt64ID()
  fmt.Printf("id generated: %v", id)
}

Recommended Settings

  • 40 bits timestamp (34 years from 2018-06-01)
  • 16 bits machine ID (using lower 16 bits of IP v4 addr as default)
  • 7 bits sequence number

With this setting, FastID is able to generate 128(2^7) unique IDs per millisecond (1.048576 millisecond, 2^10 nanosecond).

Customized Settings

See the examples in GoDoc

Benchmarks

Benchmark Settings

  • 40 bits timestamp (34 years from 2018-06-01)
  • 8 bits machine ID
  • 15 bits sequence number
go test -bench=.
goos: linux
goarch: amd64
pkg: github.com/beinan/fastid
BenchmarkGenID-4        20000000                79.7 ns/op
BenchmarkGenIDP-4       20000000               141 ns/op
PASS
ok      github.com/beinan/fastid        4.779s
Package Rankings
Top 3.57% on Proxy.golang.org
Badges
Extracted from project README
Go Report Card codecov