#unique-id #id #unique #uuid #random #nanoid #distributed-systems

tempoid

Generates URL-friendly, unique, and short IDs that are sortable by time. Inspired by nanoid and UUIDv7.

2 releases

0.1.1 Sep 25, 2024
0.1.0 Sep 23, 2024

#667 in Algorithms

49 downloads per month

MIT/Apache

12KB
227 lines

Tempo ID

crate ci License: MIT

Short IDs with temporal ordering.

A library to generate URL-friendly, unique, and short IDs that are sortable by time. Inspired by nanoid and UUIDv7.

See tempoid.dev for more information.

Motivation

  • URL-friendly: The IDs are easy to select and copy.
  • Unique: The IDs are practically unique and can be used in distributed systems.
  • Short: The IDs are shorter than UUIDs because they are encoded with a larger alphabet.
  • Sortable: The IDs are sortable by time because a timestamp is encoded in the beginning of the ID.
  • Customizable: You can configure the ID length and the characters used.

Example ID:

0uoVxkjTFsrRX30O5B9fX
<------><----------->
  Time     Random

Collisions

  • Same millisecond: There can be only a collision if two IDs are generated in the same millisecond.
  • Low probability: Even if two IDs are generated in the same millisecond, the probability of a collision is very low.

The 13 random characters exceed the randomness of UUIDv7 (≈10^23 vs ≈10^22).

Getting Started

# Cargo.toml
[dependencies]
tempoid = <version>

Usage

use tempoid::{TempoId, alphabet};

fn main() {
    // generate a new ID
    let id = TempoId::generate();

    // parse an ID
    let id = TempoId::parse("0uoVxkjTFsrRX30O5B9fX");

    // convert an ID to a string
    let id = TempoId::generate();
    let string = id.to_string();

    // use a different alphabet
    let id = TempoId::generate_with_alphabet(alphabet::base64);

    // use a custom alphabet
    let id = TempoId::generate_with_alphabet("ABCDEF");
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Dependencies

~310KB