A lightweight, pluggable Snowflake ID generator for .NET
$ dotnet add package Quantum.SnowflakeQuantum.Snowflake is a lightweight and customizable Snowflake ID generator for .NET, producing unique, time-based, sortable identifiers suitable for distributed systems.
Inspired by Twitter's original Snowflake algorithm, it splits the ID into:
Each Snowflake ID is a binary string composed of:
| Component | Bits | Purpose |
|---|---|---|
| Timestamp | 41 | Milliseconds since a custom epoch |
| InstanceId | 10 | Identifies the machine or worker |
| Sequence | 12 | Sequence number within the same tick |
// Generate a new ID
var snowflake = SnowflakeIdGenerator.New();
// Get full binary string
Console.WriteLine(snowflake.Get()); // 64-bit binary string
// Convert to long
Console.WriteLine(snowflake.ToLong()); // Unique, sortable long
```
## 🧩 Integration
You can plug in your own timestamp source or machine ID strategy:
```csharp
var customTimestamp = new TimeStampIdGenerator(
startingPoint: new DateTime(2020, 1, 1),
dateTimeProvider: new MyCustomDateTimeProvider()
);
var machineId = new MyMachineIdGenerator(); // Must implement IMachineIdGenerator
var sequencer = new Sequencer(12); // Custom bit-width
SnowflakeIdGenerator.Set(customTimestamp, machineId, sequencer);
Quantum.Snowflake is ideal for:
dotnet add package Quantum.Snowflake