Mubai.Snowflake
1.0.0
dotnet add package Mubai.Snowflake --version 1.0.0
NuGet\Install-Package Mubai.Snowflake -Version 1.0.0
<PackageReference Include="Mubai.Snowflake" Version="1.0.0" />
<PackageVersion Include="Mubai.Snowflake" Version="1.0.0" />
<PackageReference Include="Mubai.Snowflake" />
paket add Mubai.Snowflake --version 1.0.0
#r "nuget: Mubai.Snowflake, 1.0.0"
#:package Mubai.Snowflake@1.0.0
#addin nuget:?package=Mubai.Snowflake&version=1.0.0
#tool nuget:?package=Mubai.Snowflake&version=1.0.0
Mubai.Snowflake
English | 简体中文
Vision
Provide a Snowflake ID component for .NET that is:
- Simple to adopt: one DI registration, business code only cares about
IIdGenerator.NewId(). - Safe in production: clear defensive behavior for misconfiguration, clock skew/rollback, and extreme bit layouts.
- Able to evolve: covers everything from single-instance deployment to multi-node, multi-region, and cluster-level worker allocation.
In short, a Snowflake ID library suitable as ID infrastructure for distributed systems, with a smooth path toward more complex architectures.
Version Overview
| Version | Description |
|---|---|
| v1.0.0 (current) | Flexible bit layout config, clock safety, thread safety, extreme-config fallback, decoding, DI support |
| v1.1.0 | Strongly-typed ID wrapper, EF Core integration, sample projects |
| v1.2.0 | Fix JS Number precision issues and unify ID representation between backend and frontend |
| v1.3.0 | Optional high-performance implementation and observability, keeping the default simple and reliable |
| v2.0.0 | Cluster-aware & multi-region solution |
Current Version: v1.0.0 – Base Implementation (Shipped)
Core Capabilities
Flexible bit layout configuration
- Default layout:
0 | 41-bit timestamp | 10-bit workerId | 12-bit sequence - Custom bit widths for each field, as long as the total is ≤ 63 bits
- Default layout:
Clock safety
- Configurable epoch (default: 2025-01-01)
- Built-in detection for “future timestamps” to prevent misconfigured clocks
- Clear exceptions when runtime clock anomalies are detected (rollback / moving too far ahead)
Thread safety
- Stable generation under multi-threaded workloads
- Proper handling of sequence overflow and clock rollback edge cases
Extreme-configuration fallback
- When the timestamp field overflows, a composite counter kicks in so IDs remain usable in stress / extreme test scenarios
Decoding
- Decode timestamp, workerId, and sequence from a Snowflake ID
- Consistent configuration between encoding and decoding
Dependency injection support
Single-line registration in DI:
services.AddSnowflakeIdGenerator(options => { options.WorkerId = 1; });Business code injects and uses
IIdGeneratordirectly
Performance
- Generation: single thread ~244 ns per ID, throughput ~4.1M IDs/s
- Decoding: 1–4 ns per ID, throughput in the hundreds of millions to billions of IDs/s
- Configuration & initialization: ~240 ns, effectively zero overhead
- High-concurrency stability: 20 / 50 threads under high contention, stable for a 25-second run, no abnormal behavior
- End-to-end pipeline: generate + decode still stays around ~244 ns per ID
For details, see the v1 benchmark results.
Explicit Non-Goals / Not Yet Implemented
- No EF Core integration yet (no
ValueConverter/ strongly-typed ID mapping). - No JSON converters yet (no unified solution for string IDs / JS precision issues).
- No lock-free “high performance” implementation yet; the default focuses on simplicity and safety, using
lock. - No cluster-level worker ID allocation (e.g., Redis / DB lease).
All of these are planned in later versions.
v1.1 – Application Integration: EF Core & Strongly-Typed IDs
Goal: make application code more type-safe and avoid “raw
longIDs everywhere”.
Planned work:
Strongly-typed ID wrapper
- Define a
SnowflakeIdstruct / record struct that holds an internallongvalue. - Provide explicit / implicit conversions to and from
long/string.
- Define a
EF Core integration
- Provide a
ValueConverterforSnowflakeId. - Offer extension methods to simplify configuration.
- Provide a
Sample project
- A simple WebAPI / Minimal API sample that demonstrates:
- Using
SnowflakeIdas a primary key. - Combining
SnowflakeIdGeneratorwith EF Core.
- Using
- A simple WebAPI / Minimal API sample that demonstrates:
v1.2 – JSON & Frontend Compatibility
Goal: solve JS
Numberprecision issues and unify how IDs are represented end to end.
Planned work:
System.Text.Json converter
- By default, serialize Snowflake IDs as
stringvalues in JSON.
- By default, serialize Snowflake IDs as
Optional Newtonsoft.Json support
- For projects still using Newtonsoft.Json, provide a matching
JsonConverter.
- For projects still using Newtonsoft.Json, provide a matching
Frontend docs & examples
- Show how to handle IDs as
stringin TypeScript / frontend code to avoid casting tonumber. - Examples: displaying IDs on the UI, passing IDs in URLs, table pagination, etc.
- Show how to handle IDs as
v1.3 – Performance Optimization & Diagnostics
Goal: provide an optional high-performance implementation and better observability, while keeping the default implementation simple and robust.
Planned work:
Optional high-performance implementation
- Introduce a lock-free version, e.g.
HighPerfSnowflakeIdGenerator:- Use
Interlocked/ CAS /SpinWait. - Eliminate the global
lockoverhead as much as possible.
- Use
- Choose implementation via configuration at registration time.
- Introduce a lock-free version, e.g.
Benchmark project improvements
- Use BenchmarkDotNet to compare:
- v1.0 generator vs. the high-performance implementation.
- Snowflake vs.
Guid.NewGuid().
- Present typical environment results in tables within the README.
- Use BenchmarkDotNet to compare:
Diagnostics & observability
- Provide logging hooks or counters for:
- Clock rollback events.
WaitUntilNextMillisinvocations.- Usage of composite counter logic in extreme configurations.
- Provide logging hooks or counters for:
v2.0 – Cluster Awareness & Multi-Region Strategy
Goal: support large-scale multi-region / multi-datacenter systems with worker ID allocation and high availability strategies.
Planned work:
Worker ID leases & centralized allocation
- Obtain worker IDs from Redis / a database with time-bound leases.
- Register worker IDs with a central service at startup; automatically reclaim leases when a process crashes.
Multi-region bit splitting
- Split
WorkerIdBitsinto:- High bits:
RegionId - Low bits:
MachineId
- High bits:
- Provide configuration support for this split.
- Split
Availability strategies
- Define clear behavior for:
- Worker ID acquisition failures.
- Lease conflicts.
- Central service downtime.
- Potential strategies:
- Fail-fast with clear diagnostics.
- Optional fallback to single-node mode.
- Emit warning logs.
- Define clear behavior for:
Docs & guidance
- Provide deployment guidelines for multi-region setups:
- How to plan epochs, bit allocation, and worker ID distribution.
- Provide deployment guidelines for multi-region setups:
Backlog (Unscheduled Ideas)
Ideas that have come up but are not yet scheduled for any of the versions above.
Monitoring dashboard example
- Provide a sample based on Prometheus / OpenTelemetry showing:
- IDs generated per second.
- Clock rollback counts.
- Composite counter usage under extreme configurations.
- Provide a sample based on Prometheus / OpenTelemetry showing:
Security & obfuscation
- Provide an optional obfuscation scheme for IDs exposed externally.
Language interoperability
- In documentation, recommend compatible Snowflake implementations in other languages
to make it easier to integrate in polyglot stacks.
- In documentation, recommend compatible Snowflake implementations in other languages
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 239 | 11/10/2025 |
Initial release of Mubai.Snowflake, a high-performance Snowflake ID generator for .NET.