Home

Awesome

cuid.rb - CUID ported to Ruby

Last updated: December 16, 2012

Version: 1.0.1 Build Status

Documentation: http://rubydoc.info/gems/cuid Repository: http://github.com/iyshannon/cuid

Introduction

cuid.rb is a ruby library that provides collision-resistant ids optimized for horizontal scaling and sequential lookup performance.

This is just going to cover the basics and the Ruby-specific implementation details.

Please refer to the main project site for the full story.

Sample CUID

ch72gsb320000udocl363eofy

Explanation

c - h72gsb32 - 0000 - udoc - l363eofy

The groups, in order, are:

Performance

My development system runs Ubuntu 12.04 on an Intel Wolfdale E5200 Dual Core 2.5GHz with 4GB RAM. I did some very unscientific testing and the results were as follows:

Each test was run three times and the mean is reported.

Analysis

The timestamp segment of the cuid is measured in milliseconds. In the faster test result, it generated about 74 cuids per millisecond. Even without considering the random segment of the cuid, one process could safely generate up to 1,679,615 unique cuids per millisecond (36^4 - 1) without a collision due to the counter segment.

System Requirements

This gem was developed under ruby1.9.3-p286 but is tested under multiple versions thanks to Travis CI.

Tested under:

Installation

gem install cuid

Example Usage

irb(main):001:0> require "cuid" 
=> true
irb(main):002:0> g = Cuid::generate
=> "ch8qypsnz0000a4welw8anyra"
irb(main):003:0> Cuid::validate(g)
=> true
irb(main):004:0> Cuid::generate(4)
=> ["ch8qyq35f0002a4wekjcwmh30", "ch8qyq35f0003a4weewy22izq", "ch8qyq35f0004a4webzzelvdv", "ch8qyq35f0005a4wesg3tfjk5"]    

Credit

MIT Open Source License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.