Giter Site home page Giter Site logo

id_generator's Introduction

IdGenerator

Build Status Codacy Badge

This gem provides uniq and random ids generation in distributed services but at the same time gives some ordering (timestamp) and debug (context) information

The gem is inspired by Twitter Snowflake and other algorithms.

The main difference is that ids generated by gem are not fully sequenced. We want to have large random part that can be used as a simple guard against enumeration attack.

Goals & Assumptions

While we designed format and gem we used these as requirements:

  • Every system should be able to independently (without coordination) generate id that will never (with reasonable chances) be duplicated
  • ID allows partial time ordering (timestamp in seconds)
  • ID has easy system identifier (context)
  • ID is human readable
  • ID size is not a concern
  • ID format should not be used for business/application logic - thus it remains flexible for future changes

ID Format

Suggested algorithm is based on the Twitter snowflake and other similar alghorithms:

TTTTTTTT-II-RRRRRRRRRRRRRRRRRRRRRR

  • TTTTTTTT - time in seconds since 2000 (just cool number) represented as hex - Gives us ~150 years of uniq sequences
  • II - 1 byte for System Identification in hex
  • RRRRRRRRRRRRRRRRRRRRRR - 11 bytes secure random number as hex

Code to generate is pretty straightforward:

time = format('%08x', Time.now.to_i - Time.new(2000).to_i)
id = '6a'
random = SecureRandom.hex(11)
id = "#{time}-#{id}-#{random}" # "21dc3680-6a-910df0665e5e29b9f89e21"

Installation

Add gem to your application's Gemfile:

gem 'id_generator'

Usage

# Somewhere during project start
context_id = 165 # value from 0 to 255
IdGenerator.configuration.context_id = context_id
# Or using block
IdGenerator.configure { |config| config.context_id = context_id } 

#Inside of the actual code
IdGenerator.generate

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/matic-insurance/id_generator.

License

The gem is available as open source under the terms of the MIT License.

id_generator's People

Contributors

vnovitskyi avatar volodymyr-mykhailyk avatar

Stargazers

 avatar  avatar  avatar

Watchers

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

Forkers

codacy-badger

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.