A globally unique ID generator with pluggable algorithms and transport layer, written in Rust.
- Multiple ID Algorithms: Snowflake, ULID, NanoID
- Pluggable Storage: Memory, File, or implement your own
- Optional Transport Layer: HTTP REST API or gRPC
- High Performance: Async/await with Tokio runtime
- Distributed Support: Worker ID support for Snowflake algorithm
- Zero Dependencies Core: Use as a library without HTTP/gRPC overhead
| Algorithm | Output | Length | Sortable | Distributed | Use Case |
|---|---|---|---|---|---|
| Snowflake | u64 | 64-bit | Time-sortable | ✓ (1024 nodes) | Distributed systems, databases |
| ULID | String | 26 chars | Lexicographically | ✗ | URLs, distributed databases |
| NanoID | String | Configurable | ✗ | ✗ | Short URLs, session IDs |
64-bit unique ID with the following structure:
| 1 bit sign | 41 bits timestamp | 10 bits worker_id | 12 bits sequence |
- 41 bits timestamp: ~69 years from custom epoch
- 10 bits worker_id: 1024 nodes maximum
- 12 bits sequence: 4096 IDs per millisecond per node
Universally Unique Lexicographically Sortable Identifier:
| 48 bits timestamp | 80 bits randomness |
- 26 characters, Base32 encoded
- Case-insensitive
- URL-safe
- Monotonic increment support
URL-friendly unique string identifier:
- Default: 21 characters
- Customizable length and alphabet
- URL-safe characters:
A-Za-z0-9_-
Add to your Cargo.toml:
[dependencies]
globuid = "0.1.0"# With HTTP supportglobuid = { version = "0.1.0", features = ["http"] }
# With gRPC supportglobuid = { version = "0.1.0", features = ["grpc"] }
# With all featuresglobuid = { version = "0.1.0", features = ["full"] }use globuid::{Snowflake,SnowflakeConfig,MemoryStorage,IdGenerator};use std::sync::Arc;#[tokio::main]asyncfnmain(){let config = SnowflakeConfig{worker_id:1,// Unique worker ID (0-1023)
..Default::default()};let storage = Arc::new(MemoryStorage::new());let generator = Snowflake::new(config, storage).await.unwrap();// Generate single IDlet id = generator.generate().await.unwrap();println!("ID: {}", id);// Generate batchlet ids = generator.generate_batch(10).await.unwrap();for id in ids {println!("ID: {}", id);}}use globuid::{Ulid,IdGenerator};#[tokio::main]asyncfnmain(){let generator = Ulid::with_default();let id = generator.generate().await.unwrap();println!("ULID: {}", id);// e.g., "01ARZ3NDEKTSV4RRFFQ69G5FAV"}use globuid::{NanoId,NanoIdConfig,IdGenerator};#[tokio::main]asyncfnmain(){// Default: 21 characterslet generator = NanoId::with_default();let id = generator.generate().await.unwrap();println!("NanoID: {}", id);// e.g., "V1StGXR8_Z5jdHi6B-myT"// Custom lengthlet config = NanoIdConfig::new().length(10);let short_generator = NanoId::new(config);let id = short_generator.generate().await.unwrap();println!("Short ID: {}", id);// e.g., "IRFa-VaH2b"}# Start HTTP server with Snowflake
cargo run --features http -- --algorithm snowflake --port 8080
# With ULID
cargo run --features http -- --algorithm ulid --port 8080
# With NanoID (custom length)
cargo run --features http -- --algorithm nanoid --nanoid-length 10 --port 8080API Endpoints:
| Method | Endpoint | Description |
|---|---|---|
GET | /health | Health check |
GET | /id | Generate single ID |
GET | /id/batch?count=N | Generate N IDs (max 10000) |
Example:
# Health check
curl http://localhost:8080/health
# {"status":"ok"}# Single ID
curl http://localhost:8080/id
# {"id":"287628074806149120"}# Batch IDs
curl "http://localhost:8080/id/batch?count=5"# {"ids":["...","..."],"count":5}# Start gRPC server
cargo run --features grpc -- --algorithm ulid --protocol grpc --port 9090Proto Definition:
serviceGlobUid {
rpcGenerate(GenerateRequest) returns (GenerateResponse);
rpcGenerateBatch(GenerateBatchRequest) returns (GenerateBatchResponse);
rpcHealth(HealthRequest) returns (HealthResponse);
}Non-persistent in-memory storage. Suitable for single-instance deployments.
use globuid::MemoryStorage;use std::sync::Arc;let storage = Arc::new(MemoryStorage::new());Persistent file-based storage. Survives application restarts.
use globuid::FileStorage;use std::sync::Arc;let storage = Arc::new(FileStorage::new("/path/to/state.json"));Implement the Storage trait for custom backends (Redis, PostgreSQL, etc.):
use globuid::{Storage,GeneratorState};use std::pin::Pin;use std::future::Future;structRedisStorage{/* ... */}implStorageforRedisStorage{fnload(&self) -> Pin<Box<dynFuture<Output = Result<GeneratorState,Box<dyn std::error::Error + Send + Sync>>> + Send + '_>>{// Load state from Redis}fnsave(&self,state:GeneratorState) -> Pin<Box<dynFuture<Output = Result<(),Box<dyn std::error::Error + Send + Sync>>> + Send + '_>>{// Save state to Redis}}GlobUid - Global Unique ID Service
Usage: globuid [OPTIONS]
Options:
-a, --algorithm <ALGORITHM> ID algorithm: "snowflake", "ulid", "nanoid" [default: snowflake]
-w, --worker-id <WORKER_ID> Worker ID (0-1023) for Snowflake [default: 0]
-p, --port <PORT> Port to listen on [default: 8080]
--host <HOST> Host to bind to [default: 0.0.0.0]
-s, --storage <STORAGE> Storage backend: "memory" or "file" [default: memory]
--storage-path <PATH> File path for file storage
-P, --protocol <PROTOCOL> Protocol: "http" or "grpc" [default: http]
--nanoid-length <LEN> ID length for NanoID [default: 21]
-h, --help Print help
-V, --version Print version
| Feature | Description |
|---|---|
http | Enable HTTP REST API server |
grpc | Enable gRPC server |
full | Enable all transport layers |
Benchmarks on Apple M1 (single thread):
| Algorithm | Ops/sec |
|---|---|
| Snowflake | ~2M/sec |
| ULID | ~1.5M/sec |
| NanoID (21 chars) | ~800K/sec |
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Contributions are welcome! Please feel free to submit a Pull Request.