BeakId
English | Русский
Snowflake-like 64-bit unique IDs for Rust. BeakId wraps a non-negative i64
value for PostgreSQL BIGINT.
Layout
Each ID uses this 64-bit layout:
[ reserved: 1 | timestamp: 35 | sequence: 18 | worker: 10 ]
Parts:
reserved: always0timestamp: 100ms windows since the configured custom epochsequence: per-window counterworker: generator worker id in0..=1023
Because the highest bit is always zero, generated IDs can be stored as valid
non-negative i64 values.
Configuration
The global singleton reads configuration lazily on first use:
BEAKID_EPOCH=2025-01-01T00:00:00Z
BEAKID_WORKER_ID=42
Variables:
BEAKID_EPOCH: required RFC 3339 UTC datetime, for example2025-01-01T00:00:00ZBEAKID_WORKER_ID: optionalu16, defaults to0, must fit in 10 bits
Invalid or missing epoch configuration is rejected.
API
Singleton
let id = next_id;
next_id() panics if environment configuration is invalid. Use
try_next_id() to handle errors, including temporary generator blocking:
let id = try_next_id?;
let db_id = id.as_i64;
# Ok::
Decode the absolute creation timestamp using the singleton epoch:
let id = try_next_id?;
let created_at = timestamp?;
# Ok::
Background Updater
Start the standard-thread background updater once during application startup:
start_background?;
# Ok::
The updater runs roughly every 30ms and reconciles the generator with real time. The crate does not depend on Tokio or any other async runtime.
When the generator is blocked, try_next_id() returns BeakIdError::Blocked.
Async applications should retry through runtime-aware macros so the executor can
schedule other tasks while waiting:
let id = tokio_next_id!;
let id = smol_next_id!;
Explicit Generator
use UNIX_EPOCH;
let generator = new?;
let id = generator.next_id?;
let created_at = generator.timestamp?;
# Ok::
Algorithm
Generator follows the reference beakid-rs approach:
The hot path increments one atomic ID value by 1 << 10, which advances the
sequence while preserving the worker bits. No mutexes are used.
When sequence overflow crosses a 100ms window boundary, the generator refreshes
real time. If the generated virtual window is ahead of real time, generation can
continue up to 10 virtual windows. If that limit is exhausted, generation returns
BeakIdError::Blocked. Waiting is intentionally left to runtime-aware macros or
caller code.
Storage
Use PostgreSQL BIGINT:
id BIGINT PRIMARY KEY
Generated values are sortable by approximate creation time within the configured epoch and worker-id scheme.
Use id.as_i64() before writing to the database, and BeakId::new(value) when
reading an ID back.