YMJake.Snowflake.IdGen 1.0.1

dotnet add package YMJake.Snowflake.IdGen --version 1.0.1
                    
NuGet\Install-Package YMJake.Snowflake.IdGen -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="YMJake.Snowflake.IdGen" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="YMJake.Snowflake.IdGen" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="YMJake.Snowflake.IdGen" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add YMJake.Snowflake.IdGen --version 1.0.1
                    
#r "nuget: YMJake.Snowflake.IdGen, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package YMJake.Snowflake.IdGen@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=YMJake.Snowflake.IdGen&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=YMJake.Snowflake.IdGen&version=1.0.1
                    
Install as a Cake Tool

YMJake.Snowflake.IdGen

A lightweight, zero-dependency Snowflake ID library for .NET.


Installation

dotnet add package YMJake.Snowflake.IdGen
dotnet add package YMJake.Snowflake.IdGen.AspNetCore

Quick Start

SnowflakeId.Configure(3);
var id = SnowflakeId.New();

Console.WriteLine(id);            // 313860533411381248
Console.WriteLine(id.WorkerId);   // 3
Console.WriteLine(id.Sequence);   // 0
Console.WriteLine(id.CreatedAt);  // 2026-05-16T02:09:42+00:00

ASP.NET Core

builder.Services.AddSnowflakeId(3);

Distributed Coordination

The library does not include a built-in distributed coordinator.
WorkerId assignment is a deployment concern, not an ID generation concern.

For dynamic multi-instance deployments, implement WorkerIdProviderBase:

public sealed class RedisWorkerIdProvider(IConnectionMultiplexer redis)
    : WorkerIdProviderBase
{
    private readonly IDatabase _db = redis.GetDatabase();
    private ushort _workerId;
    private readonly string _identity =
        $"{Environment.MachineName}-{Guid.NewGuid()}";

    public override ushort GetWorkerId() => _workerId;

    protected override async Task<ushort> AcquireWorkerIdAsync(
        CancellationToken cancellationToken)
    {
        for (ushort i = 0; i < 1024; i++)
        {
            var ok = await _db.StringSetAsync(
                $"snowflake:workerid:{i}",
                _identity,
                TimeSpan.FromSeconds(30),
                When.NotExists);

            if (ok) { _workerId = i; return i; }
        }
        throw new InvalidOperationException("No available workerId.");
    }

    protected override async Task RefreshAsync(
        CancellationToken cancellationToken)
    {
        await _db.KeyExpireAsync(
            $"snowflake:workerid:{_workerId}",
            TimeSpan.FromSeconds(30));
    }

    protected override async Task ReleaseAsync(
        CancellationToken cancellationToken)
    {
        await _db.KeyDeleteAsync($"snowflake:workerid:{_workerId}");
    }
}

Register:

builder.Services.AddSnowflakeId<RedisWorkerIdProvider>();

Full samples for Redis / Etcd / Consul / ZooKeeper are available in the demo/ directory.


EF Core

SnowflakeId is a readonly struct. Register a ValueConverter to use it as a primary key:

public class SnowflakeIdValueConverter()
    : ValueConverter<SnowflakeId, long>(
        v => v.ToLong(),
        v => SnowflakeId.FromLong(v));

// Apply globally in DbContext
protected override void ConfigureConventions(ModelConfigurationBuilder config)
{
    config.Properties<SnowflakeId>()
          .HaveConversion<SnowflakeIdValueConverter>();
}

Auto-generate IDs on insert:

public class Order
{
    public SnowflakeId Id { get; set; } = SnowflakeId.New();
}

Design

YMJake.Snowflake.IdGen            → ID generation only, zero dependencies
YMJake.Snowflake.IdGen.AspNetCore → DI integration, WorkerIdProviderBase
demo/                             → Redis / Etcd / Consul / ZooKeeper coordination examples

The library boundary stops at ID generation.
Distributed coordination complexity belongs to the caller.


Snowflake Layout

[1 bit sign] [41 bit timestamp] [10 bit workerId] [12 bit sequence]

Epoch: 2024-01-01 UTC
Max workerId: 1023
Max sequence: 4095
Capacity: 4096 IDs/ms per instance


License

MIT

Product 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 is compatible.  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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on YMJake.Snowflake.IdGen:

Package Downloads
YMJake.Snowflake.IdGen.AspNetCore

ASP.NET Core integration for NSnowflakeId — DI extensions and WorkerIdProviderBase.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 33 5/16/2026