Giter Site home page Giter Site logo

snowflake's Introduction

snowflake

Snowflake is a go implementation of Twitter's Snowflake service. Like its namespace, Snowflake is also a network service for generating unique ID numbers at high scale with some simple guarantees.

https://blog.twitter.com/2010/announcing-snowflake

Concepts

Snowflake generates unique int64 ids that (unlike uuids) are loosely time sorted. Each id consists of:

Bits Field Notes
41 Timestamp in MS ~70yrs
10 Server ID Unique Server ID
13 Sequence ID sequence to disambiguate requests in the same ms

Server Usage

The simplest way to run snowflake is via docker:

docker run -p 80:80 -it --rm savaki/snowflake:1.3

To retrieve the a single id:

curl http://your-host-name?n=4
[152193159915372544]

To retrieve the N ids:

curl http://your-host-name?n=8
[152193295848570880,152193295848570881,152193295848570882,152193295848570883]

Client Usage

Snowflake implements two clients, a low level client, and a high level buffered client. In most cases, you'll want to use the buffered client. The buffered client maintains and replenishes an internal queue of ids so there should always be one available when you need it.

package main

import (
	"fmt"

	"github.com/savaki/snowflake"
)

func main() {
	client, _ := snowflake.NewClient(snowflake.WithHosts("your-host"))
	buffered := snowflake.NewBufferedClient(client)
	fmt.Println("id:", buffered.Id())
}

snowflake's People

Contributors

savaki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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