Giter VIP home page Giter VIP logo

snowid's Introduction

Snowid

Snowid is a service which generates unique sortable time-based 64 bit long identifiers.

Format

[ 00000000000000000000000000000000000000000    0    0000000000 000000000000 ]
  |--------------(1)----------------------| |-(2)-| |---(3)--| |---(4)----|
  1. 41 bits for Unix Timestamp with milliseconds
  2. Reserved bit
  3. 10 bits for Machine ID
  4. 12 bits for incrementing number
field max value (dec) max value (hex)
Timestamp 2199023255551 0x1FFFFFFFFFF
Machine ID 1023 0x3FF
Number 4095 0xFFF

The format is based on idea that each service in the cluster has its unique machine ID. It makes some points for running service. For example, if you run snowid in Kubernetes cluster, you should use StatefulSet instead of a regular Deployment.

The service also supports custom epoch to use it in the timer. Be careful with changing epoch setting for the existing services as it may bring collisions. usually, you don't need to change epoch during the lifetime.

Installing

With docker

  1. Clone the repository
  2. Run docker build . -t snowid:latest

From sources

go get github.com/azzz/snowid/cmd/snowid

Running

Configuration

The service is configured by setting environment variables.

Environment Variable Optional Description Default Example
LOG_LEVEL optional Log level info info
LISTEN required Host and port the service runs on ":8080"
EPOCH required Epoch the timer start counting time from 20210413001805
MACHINE_ID required Machine/service id. Must be unique in the scope of cluster 42

Example

LOG_LEVEL=info LISTEN=":8080" EPOCH="20210413001805" MACHINE_ID=333 snowid`

Usage

HTTP

The service can be called by HTTP

GET /id64

description: Return an ID in both numeric and string formats. The string format might be useful if the language does not support 64 bit numbers. For example, JavaScript expects the numbers are 52 bits long.

content-type: application/json

Example:

{"numeric":41859390008987648,"string":"41859390008987648"}

Examples

> curl -i http://localhost:8080/id64
HTTP/1.1 200 OK
Date: Wed, 09 Jun 2021 18:25:13 GMT
Content-Length: 58
Content-Type: text/plain; charset=utf-8

{"numeric":41859390008987648,"string":"41859390008987648"}

snowid's People

Contributors

azzz avatar

Stargazers

Dzyanis Kuzmenka avatar Farhad Eyvazli avatar

Watchers

 avatar

snowid's Issues

Borrow ids from a next millisecond

At the current moment, the service responds with StatusServiceUnavailable when the incremental number gets overflown.
It works well in general, but becomes too noisy when the service experiences a burst growth of requests.
In such cases, the service should borrow ids from a next millisecond and handle it carefully, it must not generate another ID from an already borrowed timestamp when the burst stops.

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.