GlobUid
A globally unique ID generator with pluggable algorithms and transport layer, written in Rust.
Features
- 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
Algorithms Comparison
| 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 |
Snowflake
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
ULID
Universally Unique Lexicographically Sortable Identifier:
| 48 bits timestamp | 80 bits randomness |
- 26 characters, Base32 encoded
- Case-insensitive
- URL-safe
- Monotonic increment support
NanoID
URL-friendly unique string identifier:
- Default: 21 characters
- Customizable length and alphabet
- URL-safe characters:
A-Za-z0-9_-
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
# With HTTP support
= { = "0.1.0", = ["http"] }
# With gRPC support
= { = "0.1.0", = ["grpc"] }
# With all features
= { = "0.1.0", = ["full"] }
Usage
As a Library
Snowflake (Distributed Systems)
use ;
use Arc;
async
ULID (Lexicographically Sortable)
use ;
async
NanoID (Short URLs)
use ;
async
As a Server
HTTP REST API
# Start HTTP server with Snowflake
# With ULID
# With NanoID (custom length)
API 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
# {"status":"ok"}
# Single ID
# {"id":"287628074806149120"}
# Batch IDs
# {"ids":["...","..."],"count":5}
gRPC
# Start gRPC server
Proto Definition:
service GlobUid {
rpc Generate(GenerateRequest) returns (GenerateResponse);
rpc GenerateBatch(GenerateBatchRequest) returns (GenerateBatchResponse);
rpc Health(HealthRequest) returns (HealthResponse);
}
Storage Backends
Memory Storage (Default)
Non-persistent in-memory storage. Suitable for single-instance deployments.
use MemoryStorage;
use Arc;
let storage = new;
File Storage
Persistent file-based storage. Survives application restarts.
use FileStorage;
use Arc;
let storage = new;
Custom Storage
Implement the Storage trait for custom backends (Redis, PostgreSQL, etc.):
use ;
use Pin;
use Future;
CLI Options
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 Flags
| Feature | Description |
|---|---|
http |
Enable HTTP REST API server |
grpc |
Enable gRPC server |
full |
Enable all transport layers |
Performance
Benchmarks on Apple M1 (single thread):
| Algorithm | Ops/sec |
|---|---|
| Snowflake | ~2M/sec |
| ULID | ~1.5M/sec |
| NanoID (21 chars) | ~800K/sec |
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.