Giter Site home page Giter Site logo

small-uid's Introduction

Small UID

โš ๏ธ This project is in experimental phase, the API may may be subject to change.

Latest Version on Packagist Total Downloads Software License

UUIDs are frequently used as database Primary Key in software development. However, they aren't the best choice mainly due to their random sorting and the resulting fragmentation in databases indexes.

Using ULIDs is generally a very good alternative, solving most of UUID flaws.

Small UIDs are also an ideal alternative when you do not need as much uniqueness and want shorter "user-friendly" encoded strings.

Introduction

Small UIDs are short unique identifiers especially designed to be used as efficient database Primary Key:

  • Half smaller than UUID / ULID (64-bit)
  • Lexicographically sortable
  • Encodable as a short user-friendly and URL-safe base-62 string (a-zA-Z0-9)
  • User-friendly strings are generated in a way to be always very different (no shared prefix due to similar timestamps)
Small UID ULID UUID v4
Size 64 bits 128 bits 128 bits
Monotonic sort order Yes *** Yes No
Random bits 20 80 122
Collision odds ** 1,024 / ms* 1.099e+12 / ms* 2.305e+18

* theorical number of generated uids before the first expected collision.
** the uid includes a timestamp, so collisions may occur only during the same millisecond.
*** monotonic sort order, but random order when generated at the same millisecond.

They are internally stored as 64-bit integers (44-bit timestamp followed by 20 random bits):

|-----------------------|  |------------|
        Timestamp            Randomness
         44 bits               20 bits

The random number suffix still guarantees a decent amount of uniqueness when many ids are created in the same millisecond (up to 1,048,576 different values) and you may only expect collision if you're generating more than 1024 random ids during the same millisecond.

Sorting

Because of the sequential timestamp, Small UIDs are naturally sorted chronologically. It improves indexing when inserting values in databases, new ids being appended to the end of the table without reshuffling existing data (read more in this article).

However, sort order within the same millisecond is not guaranteed because of the random bits suffix.

Installation

This package requires PHP (64-bit) 7.4+ and GMP extension.

Add it as Composer dependency:

$ composer require mediagone/small-uid

If you're using Doctrine ORM, you'll probably want to install also appropriate custom types:

$ composer require mediagone/small-uid-doctrine

Examples of usage

Generating Small UIDs

$uid = SmallUid::random();

// from a given base-62 string
$uid = SmallUid::fromString('1GUDp752fwX');

Converting Small UIDs

(string)$uid; // string(11) "1GUDp752fwX"

License

Small UID is licensed under MIT license. See LICENSE file.

small-uid's People

Contributors

mediagone avatar

Stargazers

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

Watchers

 avatar

Forkers

fbeppu

small-uid's Issues

Why value length varies? Can I set a fixed length?

If I generate multiple ids, some have different lengths.

Can I set the length to a fixed number?

$list = [];
foreach (range(0, 10) as $i) {
  $uid = SmallUid::random();
  $val = (string)$uid;
  $hex = (string)$uid->toHex();
  $list[] = ['val' => $val, 'hex' => $hex, 'len' => strlen($val)];
}

In this case, the length of the first value is 10, while the others is 11:

[
  [
    "val" => "QvgsOfS1x3",
    "hex" => "185cb8707fd2f050",
    "len" => 10,
  ],
  [
    "val" => "19YmwgRJ5jl",
    "hex" => "185cb8707fd917d0",
    "len" => 11,
  ],
  [
    "val" => "KmgTohaPbzF",
    "hex" => "185cb8707fd3912f",
    "len" => 11,
  ],
  [
    "val" => "1aosJi8Frgv",
    "hex" => "185cb8707fd90921",
    "len" => 11,
  ],
  [
    "val" => "LuPBmpmAZwP",
    "hex" => "185cb8707fd523ff",
    "len" => 11,
  ],
  [
    "val" => "BKi8xxXjevB",
    "hex" => "185cb8707fdc3048",
    "len" => 11,
  ],
  [
    "val" => "I4uVEA8Mn4r",
    "hex" => "185cb8707fd6492d",
    "len" => 11,
  ],
  [
    "val" => "BWFmtfbp2PJ",
    "hex" => "185cb8707fdee268",
    "len" => 11,
  ],
  [
    "val" => "FkU5aXrL0Ur",
    "hex" => "185cb8707fd7277b",
    "len" => 11,
  ],
  [
    "val" => "A1Xajyi4a6T",
    "hex" => "185cb8707fd04c47",
    "len" => 11,
  ],
  [
    "val" => "6JSFSjzUfs9",
    "hex" => "185cb8707fd6a894",
    "len" => 11,
  ],
]

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.