Giter Site home page Giter Site logo

flake's Introduction

Flake

Decentralized, k-ordered unique ID generator.

This is a Clojure implementation of Boundary's Erlang Flake ID service.

Installation

flake is available via Clojars:

[flake "0.2.0"]

Usage

New flake IDs can be generated with the generate fn from flake's core namespace.

Note that in order to prevent generation of duplicate IDs, init! should be called prior to generating IDs for the first time to prevent potential duplicate IDs.

For example:

=> (require '[flake.core :as flake])
=> (flake/init!)
=> (take 3 (repeatedly flake/generate))
(25978563106299135585558915252224N
 25978563106299135585558915252225N
 25978563106299135585558915252226N)

Here we have generated three BigIntegers which are flake IDs. Note how they are ordered.

It may be desirable to encode these IDs in a shorter representation, such as Base62. The utils namespace provides an encoder:

=> (require '[flake.utils :as utils])
=> (utils/base62-encode (flake/generate))
"8mwFA958SJ2CZVu9nk"

Exceptional cases

When generating flakes, if system time flows backwards then an IllegalStateException will be thrown. In the extremely unlikely event that you generate more than 65,535 flakes on one machine in a millisecond (65 million flakes/second) then an IllegalArgumentException will be thrown as the flake sequence id (of type short) overflows.

flake's People

Contributors

maxcountryman avatar danielcompton avatar

Watchers

Jan Stępień avatar James Cloos 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.