ids_service
A Rust library for generating unique, cryptographically-sound IDs 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.
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
Uses std::collections::hash_map::DefaultHasher
(SipHash 1-3). IDs are u64 values. Faster than crypto_hash, but not
cryptographically secure — suitable for non-security-sensitive use cases.
Feature flags
| Flag | Default | Description |
|---|---|---|
all |
✅ | Enables both crypto and rust |
crypto |
✅ | Enables crypto_hash module (requires sha3 dep) |
rust |
✅ | Enables rust_hash module |
To use only one module, disable default features in Cargo.toml:
# Only the rust hasher — no sha3 dependency
= { = "2.0.1", = false, = ["rust"] }
# Only the crypto hasher
= { = "2.0.1", = false, = ["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 ;
Rust hasher (rust_hash)
use IdsService;
use ;
Standalone (no service, no cache)
use create_id;
Performance
Benchmarks run with Criterion.rs on an
AMD Ryzen AI 7 350 — cargo bench.
Results
| Benchmark | Time | Throughput |
|---|---|---|
crypto_hash/standalone/sha256 |
~830 ns | ~1.2 Melem/s |
crypto_hash/standalone/sha512 |
~845 ns | ~1.2 Melem/s |
crypto_hash/service/get_id/Sha3_256 |
~105–127 ns | ~7.9–9.5 Melem/s |
crypto_hash/service/get_id/Sha3_512 |
~75–92 ns | ~10.8–13.4 Melem/s |
rust_hash/standalone/create_id |
~830 ns | ~1.2 Melem/s |
rust_hash/service/get_id |
~101–150 ns | ~6.6–9.9 Melem/s |
Cache hit = Mutex::lock + VecDeque::pop_front ≈ 75–150 ns — 7–10× faster than standalone generation.
Run the full benchmark suite:
Results are saved as HTML reports in target/criterion/.
Examples
# Cryptographic hasher examples
# Rust 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.