Crates.io | hora-id |
lib.rs | hora-id |
version | 0.3.0 |
created_at | 2025-01-17 08:48:36.868173+00 |
updated_at | 2025-09-18 08:52:16.995787+00 |
description | A 64-bit time-based sorted unique ID generator that includes the current time in the ID |
homepage | https://github.com/RustyFarmer101/hora-id |
repository | https://github.com/RustyFarmer101/hora-id |
max_upload_size | |
id | 1520420 |
size | 28,564 |
A time-sorted 8-byte unique ID generator for distributed systems, micro-services and B-Tree databases.
HoraID supports upto 256 unique machines and can theoretically generate 16.7 million unique IDs per second per machine. That's a total of maximum 4.2 billion IDs per second. However, modern computers are not able to hit this limit. See the performance section below for more details.
HoraID has 4 parts:
When generating ID with rand()
method, it has:
cargo add hora_id
# if `to_datetime` or `to_utc` methods are needed
cargo add hora_id --features chrono
Generate IDs in a distributed system
use hora_id::{HoraGenerator, HoraId};
let machine_id = 1; // You'll ideally get this from environment variable or configuration
let mut generator: HoraGenerator = HoraGenerator::new(machine_id).unwrap();
let id: HoraId = generator.next();
println!("{}", id.to_string()); // example: '00cd01daff010002'
println!("{}", id.to_u64()); // example: 57704355272392706
println!("{}", id.to_datetime()); // example: 2025-01-01 14:00:00
println!("{}", id.to_utc()); // example: 2025-01-01 14:00:00 UTC
Quickly generate a new ID.
use hora_id::HoraId;
let id = HoraId::rand().unwrap();
Note: rand()
uses the system random number generator to generate a random machine ID and sequence.
This method is not optimal for generating large number of IDs per second.
On a Macbook Pro with M1 Max chip, the included benchmark in
src/bin/bench.rs
produces 7.4 Million IDs per second on a single thread.
Given that the theoretical limit is 16.7 Million IDs per second,
the package will scale well with future CPUs.
In the benchmark example, M1 Max chip only hits 44% of the limit.
To run the benchmark, execute cargo r --bin bench --release
on your system.
rand()
method to quickly generate a random ID