Snowspeck 1.0.2
dotnet add package Snowspeck --version 1.0.2
NuGet\Install-Package Snowspeck -Version 1.0.2
<PackageReference Include="Snowspeck" Version="1.0.2" />
<PackageVersion Include="Snowspeck" Version="1.0.2" />
<PackageReference Include="Snowspeck" />
paket add Snowspeck --version 1.0.2
#r "nuget: Snowspeck, 1.0.2"
#:package Snowspeck@1.0.2
#addin nuget:?package=Snowspeck&version=1.0.2
#tool nuget:?package=Snowspeck&version=1.0.2
Snowspeck
A lightweight, thread-safe, extensible, and decodable Snowflake ID generator for .NET.
Features
- 64-bit unique, time-ordered ID generation
- Fully thread-safe
- Decodable (timestamp, datacenter ID, worker ID, sequence)
- Minimal dependencies (
Microsoft.Extensions.Options
) - Great for distributed systems and high-throughput workloads
What is a Snowflake ID?
A Snowflake ID is a 64-bit integer originally developed by Twitter to generate unique, time-ordered identifiers in distributed systems.
It typically encodes:
- A timestamp (usually in milliseconds)
- A machine or worker ID (identifying the node that generated the ID)
- A sequence number (to prevent collisions when generating multiple IDs in the same millisecond)
This structure ensures:
- Globally unique identifiers without coordination between nodes
- IDs that increase over time (sortable)
- High-performance generation, suitable for distributed environments
Disclaimer
Snowflake IDs are intentionally decodable and expose metadata like generation time and worker identity.
They are not cryptographically secure and should not be used to encode sensitive or private information.
Use them only for unique, ordered identifiers, not for hiding or securing data.
Installation
Using the CLI:
dotnet add package Snowspeck
Or via the NuGet Package Manager:
Install-Package Snowspeck
Implementations
Snowspeck offers two implementations of the generator, depending on whether you prefer signed or unsigned IDs:
Class | Return Type | ID Range | Notes |
---|---|---|---|
SignedSnowflakeService |
long |
-2⁶³ to 2⁶³−1 | Default in DI setup, fits in signed columns |
UnsignedSnowflakeService |
ulong |
0 to 2⁶⁴−1 | Full 64-bit range, use when unsigned is OK |
Both use the same structure:
- 41 bits for timestamp
- 5 bits for datacenter ID
- 5 bits for worker ID
- 12 bits for sequence number
Choose the implementation that best fits your database, serialization, or sorting needs.
Quick Start
Manual Instantiation
using Snowspeck;
using Snowspeck.Interfaces;
using Snowspeck.Services;
var options = new SnowflakeOptions
{
WorkerId = 1,
Epoch = 1735689600000L // (optional) Default is 1735689600000L -> Jan 1, 2025
};
ISnowflakeGenerator generator = new SignedSnowflakeService(options);
long id = generator.NextId();
Dependency Injection (ASP.NET Core)
builder.Services.AddSnowspeck(options =>
{
options.WorkerId = 1;
options.Epoch = 1735689600000L; // (optional) Default is 1735689600000L -> Jan 1, 2025
});
builder.Services.AddSnowspeckUnsigned(options =>
{
options.WorkerId = 1;
options.Epoch = 1735689600000L; // (optional) Default is 1735689600000L -> Jan 1, 2025
});
Once registered, resolve ISnowflakeGenerator
via constructor injection:
public class MyService
{
private readonly ISnowflakeGenerator _generator;
public MyService(ISnowflakeGenerator generator)
{
_generator = generator;
}
public void DoSomething()
{
long id = _generator.NextId();
}
}
Decoding Snowflake Metadata
The generator supports decoding a Snowflake ID back into:
Timestamp
(UTC time the ID was generated)DatacenterId
WorkerId
Sequence
long id = generator.NextId();
var metadata = generator.Decode(id);
Console.WriteLine($"Timestamp: {metadata.Timestamp}");
Console.WriteLine($"DatacenterId: {metadata.DatacenterId}");
Console.WriteLine($"WorkerId: {metadata.WorkerId}");
Console.WriteLine($"Sequence: {metadata.Sequence}");
Planned Features
- Future extensions via subclassing (override clock skew handling, timestamp source, etc.)
Have an idea or feature request? Feel free to open an issue.
License
Licensed under the MIT License.
© 2025 Tiago Ferreira Alves
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.Options (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.