11# Jordium.Snowflake.NET
22
3+ English | [ 简体中文] ( ./README.md )
4+
35[ ![ NuGet] ( https://img.shields.io/nuget/v/Jordium.Snowflake.NET.svg )] ( https://www.nuget.org/packages/Jordium.Snowflake.NET/ )
46[ ![ .NET] ( https://img.shields.io/badge/.NET-8.0-512BD4 )] ( https://dotnet.microsoft.com/download/dotnet/8.0 )
57[ ![ License] ( https://img.shields.io/badge/license-MIT-blue.svg )] ( LICENSE )
@@ -8,12 +10,12 @@ High-performance distributed ID generator based on Twitter's Snowflake algorithm
810
911## Features
1012
11- - ? ** Globally Unique** : Supports multi-datacenter, multi-machine deployment
12- - ? ** Trend Increasing** : IDs increase by timestamp, optimized for database indexing
13- - ? ** High Performance** : 2 million - 20 million IDs/second per machine
14- - ? ** Multiple Implementations** : Drift algorithm, Traditional algorithm, Lock-free algorithm
15- - ? ** Clock Rollback Handling** : Automatic handling of system time rollback
16- - ? ** Flexible Configuration** : Customizable bit allocation
13+ - ✅ ** Globally Unique** : Supports multi-datacenter, multi-machine deployment
14+ - 📈 ** Trend Increasing** : IDs increase by timestamp, optimized for database indexing
15+ - ⚡ ** High Performance** : 2 million - 20 million IDs/second per machine
16+ - 🔧 ** Multiple Implementations** : Drift algorithm, Traditional algorithm, Lock-free algorithm
17+ - ⏰ ** Clock Rollback Handling** : Automatic handling of system time rollback
18+ - 🎯 ** Flexible Configuration** : Customizable bit allocation
1719
1820## Installation
1921
@@ -130,19 +132,19 @@ public class OrderController : ControllerBase
130132
131133| Algorithm | Implementation | Use Case | Performance | Clock Rollback | Recommended |
132134| -----------| ---------------| ----------| -------------| ----------------| -------------|
133- | ** Method 1** | SnowflakeWorkerV1 | Monolith app, high concurrency | ???? | ? Special sequence | ????? |
134- | ** Method 2** | SnowflakeWorkerV2 | General, standard implementation | ???? | ?? Throws exception | ??? |
135- | ** Method 3** | SnowflakeWorkerV3 | Distributed cluster, microservices | ????? | ?? Large rollback throws | ???? |
135+ | ** Method 1** | SnowflakeWorkerV1 | Monolith app, high concurrency | ⭐⭐⭐⭐ | ✅ Special sequence | ⭐⭐⭐⭐⭐ |
136+ | ** Method 2** | SnowflakeWorkerV2 | General, standard implementation | ⭐⭐⭐⭐ | ❌ Throws exception | ⭐⭐⭐ |
137+ | ** Method 3** | SnowflakeWorkerV3 | Distributed cluster, microservices | ⭐⭐⭐⭐⭐ | ⚠️ Large rollback throws | ⭐⭐⭐⭐ |
136138
137139### Detailed Description
138140
139141#### Method 1 - Drift Algorithm (Recommended)
140142
141143** Features** :
142- - ? Auto-handles clock rollback (uses reserved sequence 1-4)
143- - ? Auto "drift" to future time when sequence overflows
144- - ? Complete event callback mechanism
145- - ?? Uses ` lock ` for synchronization
144+ - ✅ Auto-handles clock rollback (uses reserved sequence 1-4)
145+ - ✅ Auto "drift" to future time when sequence overflows
146+ - ✅ Complete event callback mechanism
147+ - 🔒 Uses ` lock ` for synchronization
146148
147149** Use Cases** :
148150- Monolithic applications (multiple threads competing for same WorkerId)
@@ -176,10 +178,10 @@ generator.GenIdActionAsync = arg =>
176178#### Method 2 - Traditional Algorithm
177179
178180** Features** :
179- - ? Standard Snowflake implementation
180- - ? Simple logic, easy to understand
181- - ?? Throws exception on clock rollback
182- - ?? Uses ` lock ` for synchronization
181+ - ✅ Standard Snowflake implementation
182+ - ✅ Simple logic, easy to understand
183+ - ❌ Throws exception on clock rollback
184+ - 🔒 Uses ` lock ` for synchronization
183185
184186** Use Cases** :
185187- Learning and understanding Snowflake algorithm
@@ -213,11 +215,11 @@ catch (Exception ex)
213215#### Method 3 - Lock-Free Algorithm (High Performance)
214216
215217** Features** :
216- - ? CAS lock-free concurrency, highest performance
217- - ? Timestamp cached in state, reduces system calls
218- - ? Significant performance advantage in distributed scenarios
219- - ?? Short rollback auto-sleeps and waits
220- - ?? Large rollback (> 1000ms) throws exception
218+ - ✅ CAS lock-free concurrency, highest performance
219+ - ✅ Timestamp cached in state, reduces system calls
220+ - ✅ Significant performance advantage in distributed scenarios
221+ - ⚠️ Short rollback auto-sleeps and waits
222+ - ⚠️ Large rollback (> 1000ms) throws exception
221223
222224** Use Cases** :
223225- Microservice clusters (each service has independent WorkerId)
@@ -282,10 +284,10 @@ var generator = new DefaultIDGenerator(options);
282284
283285| Threads | Throughput | Avg Latency | Note |
284286| ---------| -----------| -------------| ------|
285- | 1 | 12-15M IDs/sec | 0.067-0.083 ¦Ìs | Single thread, no contention |
286- | 2 | 9-12M IDs/sec | 0.167-0.222 ¦Ìs | Light contention |
287- | 4 | 7-9M IDs/sec | 0.444-0.571 ¦Ìs | Medium contention |
288- | 8 | 6-8M IDs/sec | 1.000-1.333 ¦Ìs | Heavy contention |
287+ | 1 | 12-15M IDs/sec | 0.067-0.083 ��s | Single thread, no contention |
288+ | 2 | 9-12M IDs/sec | 0.167-0.222 ��s | Light contention |
289+ | 4 | 7-9M IDs/sec | 0.444-0.571 ��s | Medium contention |
290+ | 8 | 6-8M IDs/sec | 1.000-1.333 ��s | Heavy contention |
289291
290292### Distributed Performance (Method 3)
291293
@@ -299,17 +301,17 @@ var generator = new DefaultIDGenerator(options);
299301
300302| Test Item | Count | Duplicates | Result |
301303| -----------| -------| -----------| --------|
302- | Single WorkerId Concurrent | 1M | 0 | ? Pass |
303- | Multi WorkerId Concurrent | 1M | 0 | ? Pass |
304- | Extreme Concurrency (32 threads) | 3.2M | 0 | ? Pass |
305- | Sustained Pressure (5 sec) | 10M+ | 0 | ? Pass |
304+ | Single WorkerId Concurrent | 1M | 0 | ✅ Pass |
305+ | Multi WorkerId Concurrent | 1M | 0 | ✅ Pass |
306+ | Extreme Concurrency (32 threads) | 3.2M | 0 | ✅ Pass |
307+ | Sustained Pressure (5 sec) | 10M+ | 0 | ✅ Pass |
306308
307309### Stability Tests
308310
309- - ? ** Million-level Test** : Generate 1M IDs at once, no duplicates
310- - ? ** Sustained Pressure Test** : Continuous generation for 5 seconds, no duplicates
311- - ? ** Multi-datacenter Test** : 3 datacenters ¡Á 3 workers, no duplicates
312- - ? ** Clock Rollback Test** : Method 1 auto-handles, Method 2/3 correctly throw exceptions
311+ - ✅ ** Million-level Test** : Generate 1M IDs at once, no duplicates
312+ - ✅ ** Sustained Pressure Test** : Continuous generation for 5 seconds, no duplicates
313+ - ✅ ** Multi-datacenter Test** : 3 datacenters × 3 workers, no duplicates
314+ - ✅ ** Clock Rollback Test** : Method 1 auto-handles, Method 2/3 correctly throw exceptions
313315
314316### Performance Notes
315317
@@ -322,32 +324,32 @@ var generator = new DefaultIDGenerator(options);
322324> - System load
323325>
324326> The above data are reference values from typical test environments.
325- > Real deployment environments may have ¡À30 % variations.
327+ > Real deployment environments may have ��30 % variations.
326328> We recommend running benchmarks on actual hardware for accurate data.
327329
328330## ID Structure
329331
330332### Standard 64-bit Snowflake ID
331333
332334```
333- ©°©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ð©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©´
334- ©¦ Timestamp(41bit)©¦ DC ID (5bit)©¦ Worker(5bit)©¦ Sequence(12) ©¦
335- ©¸©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©Ø©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¼
335+ ����������������������������������������������������������������������������������������������������������������������
336+ �� Timestamp(41bit)�� DC ID (5bit)�� Worker(5bit)�� Sequence(12) ��
337+ �������������������������������������ة��������������������������ة��������������������������ة�����������������������������
336338 ~69 years 32 DCs 32 Workers 4096/ms
337339```
338340
339341### Bit Allocation Examples
340342
341343| Config | Worker Bits | DC Bits | Seq Bits | Capacity |
342344| --------| ------------| ---------| ----------| ----------|
343- | Standard | 5 | 5 | 12 | 32 DC ¡Á 32 Worker ¡Á 4096/ms |
344- | Multi-DC | 4 | 6 | 12 | 64 DC ¡Á 16 Worker ¡Á 4096/ms |
345- | Multi-Worker | 6 | 4 | 12 | 16 DC ¡Á 64 Worker ¡Á 4096/ms |
346- | Low Concurrency | 5 | 5 | 10 | 32 DC ¡Á 32 Worker ¡Á 1024/ms |
345+ | Standard | 5 | 5 | 12 | 32 DC �� 32 Worker �� 4096/ms |
346+ | Multi-DC | 4 | 6 | 12 | 64 DC �� 16 Worker �� 4096/ms |
347+ | Multi-Worker | 6 | 4 | 12 | 16 DC �� 64 Worker �� 4096/ms |
348+ | Low Concurrency | 5 | 5 | 10 | 32 DC �� 32 Worker �� 1024/ms |
347349
348350** Constraints** :
349- - Worker bits + DC bits + Seq bits ¡Ü 22
350- - Seq bits ¡Ü 12
351+ - Worker bits + DC bits + Seq bits �� 22
352+ - Seq bits �� 12
351353
352354## FAQ
353355
@@ -383,11 +385,11 @@ var options = new IDGeneratorOptions
383385Ensure each machine has a unique ` WorkerId ` and ` DataCenterId ` combination:
384386
385387``` csharp
386- // Wrong Example ?
388+ // Wrong Example ❌
387389// Server 1: WorkerId=1, DataCenterId=1
388- // Server 2: WorkerId=1, DataCenterId=1 ¡û Will produce duplicate IDs
390+ // Server 2: WorkerId=1, DataCenterId=1 ← Will produce duplicate IDs
389391
390- // Correct Example ?
392+ // Correct Example ✅
391393// Server 1: WorkerId=1, DataCenterId=1
392394// Server 2: WorkerId=2, DataCenterId=1
393395// Server 3: WorkerId=1, DataCenterId=2
@@ -396,13 +398,13 @@ Ensure each machine has a unique `WorkerId` and `DataCenterId` combination:
396398### 4. What if Performance is Below Expectations?
397399
398400** Checklist** :
399- - ? Use singleton pattern (` AddSingleton ` )
400- - ? Avoid frequent creation of ` DefaultIDGenerator ` instances
401- - ? Choose Method 1 for single-machine high concurrency
402- - ? Choose Method 3 for distributed scenarios
403- - ? Check for duplicate WorkerIds
404- - ? Use Release mode build
405- - ? Ensure stable system clock
401+ - ✅ Use singleton pattern (` AddSingleton ` )
402+ - ✅ Avoid frequent creation of ` DefaultIDGenerator ` instances
403+ - ✅ Choose Method 1 for single-machine high concurrency
404+ - ✅ Choose Method 3 for distributed scenarios
405+ - ✅ Check for duplicate WorkerIds
406+ - ✅ Use Release mode build
407+ - ✅ Ensure stable system clock
406408
407409## License
408410
0 commit comments