ids_service
A Rust library for generating unique, random IDs derived from a cryptographically secure random source, using a pre-filled, multi-threaded cache. Background worker threads keep the cache full so that callers receive a pre-computed ID instantly without blocking.
If the cache is temporarily exhausted (burst traffic), an ID is generated on the fly — no error, no panic, just a small latency spike.
Generated IDs are purely random and carry no temporal information, making them non-sortable by time. This is a deliberate trade-off: unlike ULID or UUID v7, which embed a timestamp to allow chronological ordering, this library prioritises unpredictability over sortability. If time-based ordering is a requirement, consider using a dedicated crate such as uuid7 or uuid with the v7 feature.
How it works
┌──────────────────────────────────────────────────────┐
│ IdsService │
│ │
│ ┌──────────┐ push ┌─────────────────────────┐ │
│ │ Worker 0 │ ────────► │ │ │
│ ├──────────┤ │ Arc<Mutex<VecDeque>> │ │ get_id()
│ │ Worker 1 │ ────────► │ (cache) │ ├──────────► caller
│ ├──────────┤ │ │ │
│ │ Worker N │ ────────► │ size = cache_size │ │
│ └──────────┘ └─────────────────────────┘ │
│ │
│ Each ID = hash( random_bytes || timestamp_ns ) │
└──────────────────────────────────────────────────────┘
Each ID is derived from:
- A random byte block (size = 2 × hash output length — see Birthday problem)
- The current timestamp in nanoseconds since the Unix epoch
Modules
crypto_hash
Uses one of the SHA-3 family algorithms from the sha3 crate:
| Mode | Output | Random block |
|---|---|---|
Sha3Mode::Sha3_224 |
28 bytes | 56 bytes |
Sha3Mode::Sha3_256 |
32 bytes | 64 bytes |
Sha3Mode::Sha3_384 |
48 bytes | 96 bytes |
Sha3Mode::Sha3_512 |
64 bytes | 128 bytes |
IDs can be encoded as:
| Method | Output |
|---|---|
.as_hex() |
Lowercase hexadecimal String |
.as_base64() |
Standard Base64 String |
.as_base64_url() |
URL-safe Base64 String |
.as_base32() |
Base32 String |
.as_json() |
JSON array of byte values |
rust_hash ⚠️ Deprecated
Deprecated since 2.1.0. Use the
cryptofeature andcrypto_hashmodule instead, or consider theuuidcrate.
Feature flags
| Flag | Default | Description |
|---|---|---|
crypto |
✅ | Enables crypto_hash module (requires sha3 dep) |
rust |
❌ | Deprecated. Enables rust_hash module |
To use only the crypto module:
= { = "2.1.1", = ["crypto"] }
Quick Start
Cryptographic hash (crypto_hash)
use ;
use ;
Or use the default configuration (SHA3-256, 100 000 items, CPU-count threads):
use IdsService;
use ;
Iterator interface
IdsService implements Iterator, yielding one ID per call:
use ;
use ;
Standalone (no service, no cache)
use ;
Performance
Benchmarks run with Criterion.rs on an
AMD Ryzen AI 7 350 — cargo bench.
| Benchmark | Median time | Throughput |
|---|---|---|
crypto_hash/standalone/sha256 |
~379 ns | ~2.64 Melem/s |
crypto_hash/standalone/sha512 |
~926 ns | ~1.08 Melem/s |
crypto_hash/service/get_id/Sha3_256 |
~149–170 ns | ~5.9–6.8 Melem/s |
crypto_hash/service/get_id/Sha3_512 |
~102–120 ns | ~8.4–9.8 Melem/s |
crypto_hash/encoding/as_hex |
~24.9 ns | — |
crypto_hash/encoding/as_base64 |
~28.4 ns | — |
crypto_hash/encoding/as_base32 |
~29.4 ns | — |
Cache hit ≈ 149–170 ns — 2–6× faster than standalone SHA3-256/SHA3-512 generation.
Run the full benchmark suite:
Results are saved as HTML reports in target/criterion/.
Examples
# Cryptographic hasher examples
# Generate 10 million IDs and measure throughput
Running tests
# All unit and doc tests
# With a specific feature set
Platforms
Compiled and tested on:
- x86_64 GNU/Linux (primary development + CI)
- macOS (Apple Silicon M1)
License
MIT — see LICENSE.