Snowflake 是 Twitter 开源的一个分布式 ID 生成算法,用于在分布式系统中生成唯一、有序、不重复的 ID。它的设计目标是在不依赖数据库或其他存储设备的情况下生成全局唯一 ID。
Snowflake 算法的原理如下:
使用 64 位整数表示 ID,分为以下几部分:
算法流程:
下面是一个简单的 C# 实现:
public class Snowflake
{
private const long Twepoch = 1609459200000L; // 2021-01-01 00:00:00
private const int WorkerIdBits = 5;
private const int DatacenterIdBits = 5;
private const int SequenceBits = 12;
private const long MaxWorkerId = -1L ^ (-1L<< WorkerIdBits);
private const long MaxDatacenterId = -1L ^ (-1L<< DatacenterIdBits);
private const int WorkerIdShift = SequenceBits;
private const int DatacenterIdShift = SequenceBits + WorkerIdBits;
private const int TimestampLeftShift = SequenceBits + WorkerIdBits + DatacenterIdBits;
private const long SequenceMask = -1L ^ (-1L << SequenceBits);
private readonly object _lock = new object();
private long _sequence;
private long _lastTimestamp;
public Snowflake(long workerId, long datacenterId)
{
if (workerId > MaxWorkerId || workerId < 0)
throw new ArgumentException($"Worker Id must be between 0 and {MaxWorkerId}");
if (datacenterId > MaxDatacenterId || datacenterId < 0)
throw new ArgumentException($"Datacenter Id must be between 0 and {MaxDatacenterId}");
WorkerId = workerId;
DatacenterId = datacenterId;
}
public long WorkerId { get; }
public long DatacenterId { get; }
public long NextId()
{
lock (_lock)
{
var timestamp = GetCurrentTimestamp();
if (timestamp < _lastTimestamp)
throw new Exception("Invalid system clock");
if (_lastTimestamp == timestamp)
{
_sequence = (_sequence + 1) & SequenceMask;
if (_sequence == 0)
timestamp = WaitNextMillis(_lastTimestamp);
}
else
{
_sequence = 0;
}
_lastTimestamp = timestamp;
return ((timestamp - Twepoch)<< TimestampLeftShift) |
(DatacenterId<< DatacenterIdShift) |
(WorkerId<< WorkerIdShift) |
_sequence;
}
}
private long GetCurrentTimestamp()
{
return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
private long WaitNextMillis(long lastTimestamp)
{
var timestamp = GetCurrentTimestamp();
while (timestamp <= lastTimestamp)
{
timestamp = GetCurrentTimestamp();
}
return timestamp;
}
}
使用示例:
var snowflake = new Snowflake(1, 1);
var id = snowflake.NextId();
Console.WriteLine(id);
这个实现是线程安全的,但在高并发场景下可能会有性能瓶颈。在实际应用中,可以根据需要对其进行优化。