❄️ SnowID Rust
A Rust implementation of a Snowflake-like ID generator with 42-bit timestamp.
Generate 64-bit unique identifiers that are:
- ⚡️ Fast (~244ns per ID)
- 📈 Time-sorted
- 🔄 Monotonic
- 🔒 Thread-safe
- 🌐 Distributed-ready
- 🎯 Zero dependencies
🧮 ID Structure
Example ID: 151819733950271234
Default configuration:
|------------------------------------------|------------|------------|
| TIMESTAMP (42 bits) | NODE (10) | SEQ (12) |
|------------------------------------------|------------|------------|
- Timestamp: 42 bits = 139 years from 2024-01-01 (1704067200000)
- Node ID: 10 bits = 1,024 nodes (valid range: 6-16 bits)
- Sequence: 12 bits = 4,096 IDs/ms/node
🎯 Quick Start
[]
= "0.1.5"
use SnowID;
🔠 Base62 Encoded IDs
Generate base62 encoded IDs (using characters 0-9, a-z, A-Z) for more compact and URL-friendly identifiers:
use SnowIDBase62;
Benefits of Base62 IDs
- 🔤 More compact representation (11 chars max vs 20 digits for u64)
- 🔗 URL-friendly (no special characters)
- 👁️ Human-readable and easier to share
- 🔄 Fully compatible with original SnowID structure
🔧 Configuration
use ;
ℹ️ Available Methods
use SnowID;
📊 Performance & Comparisons
Social Media Platform Configurations
| Platform | Timestamp | Node Bits | Sequence Bits | Max Nodes | IDs/ms/node | Time/ID |
|---|---|---|---|---|---|---|
| 41 | 10 | 12 | 1,024 | 4,096 | ~242ns | |
| 41 | 13 | 10 | 8,192 | 1,024 | ~1.94µs | |
| Discord | 42 | 10 | 12 | 1,024 | 4,096 | ~245ns |
Node vs Sequence Bits Trade-off
| Node Bits | Max Nodes | IDs/ms/node | Time/ID |
|---|---|---|---|
| 6 | 64 | 65,536 | ~20ns |
| 8 | 256 | 16,384 | ~60ns |
| 10 | 1,024 | 4,096 | ~243ns |
| 12 | 4,096 | 1,024 | ~968ns |
| 14 | 16,384 | 256 | ~3.86µs |
| 16 | 65,536 | 64 | ~15.4µs |
Choose configuration based on your needs:
- More nodes → Increase node bits (max 16 bits = 65,536 nodes)
- More IDs per node → Increase sequence bits (min 6 node bits = 64 nodes)
- Total bits (node + sequence) is fixed at 22 bits
Int64 vs Base62 Performance
| Variant | Time/ID | Size | Notes |
|---|---|---|---|
| Int64 | ~325 ns | 18-20 digits | Fastest option |
| Base62 | ~350 ns | 10-11 chars | ~8% slower, more compact |
Base62 encoding provides more compact, URL-friendly IDs with a small performance trade-off.
🚀 Examples
Check out examples for:
- Basic usage
- Custom configuration
- Base62 encoding and decoding
- Performance comparisons between Int64 and Base62
- Distributed generation
- Performance benchmarks
📜 License
MIT - See LICENSE for details