雪花算法(Snowflake)是一种分布式系统中生成唯一ID的算法,最初由Twitter提出。它可以在不依赖数据库的情况下生成全局唯一的ID。雪花算法生成的ID是一个64位的长整型数字,结构如下:
+-----------------------------+
| 1 | 0 | 1 |
+-----------------------------+
| 41 | 0 | 1 |
+-----------------------------+
| 42 | 0 | 1 |
+-----------------------------+
| 43 | 0 | 1 |
+-----------------------------+
| 44 | 0 | 1 |
+-----------------------------+
| 54 | 0 | 1 |
+-----------------------------+
| 55 | 0 | 1 |
+-----------------------------+
| 56 | 0 | 1 |
+-----------------------------+
| 57 | 0 | 1 |
+-----------------------------+
| 58 | 0 | 1 |
+-----------------------------+
| 59 | 0 | 1 |
+-----------------------------+
| 60 | 0 | 1 |
+-----------------------------+
| 61 | 0 | 1 |
+-----------------------------+
| 62 | 0 | 1 |
+-----------------------------+
| 63 | 0 | 1 |
+-----------------------------+
| 64 | 0 | 1 |
+-----------------------------+
| 83 | 0 | 1 |
+-----------------------------+
| 84 | 0 | 1 |
+-----------------------------+
| 85 | 0 | 1 |
+-----------------------------+
| 86 | 0 | 1 |
+-----------------------------+
| 87 | 0 | 1 |
+-----------------------------+
| 88 | 0 | 1 |
+-----------------------------+
| 89 | 0 | 1 |
+-----------------------------+
| 90 | 0 | 1 |
+-----------------------------+
| 91 | 0 | 1 |
+-----------------------------+
| 92 | 0 | 1 |
+-----------------------------+
| 93 | 0 | 1 |
+-----------------------------+
| 94 | 0 | 1 |
+-----------------------------+
| 95 | 0 | 1 |
+-----------------------------+
| 96 | 0 | 1 |
+-----------------------------+
| 97 | 0 | 1 |
+-----------------------------+
| 98 | 0 | 1 |
+-----------------------------+
| 99 | 0 | 1 |
+-----------------------------+
| 100 | 0 | 1 |
+-----------------------------+
| 101 | 0 | 1 |
+-----------------------------+
| 102 | 0 | 1 |
+-----------------------------+
| 103 | 0 | 1 |
+-----------------------------+
| 104 | 0 | 1 |
+-----------------------------+
| 105 | 0 | 1 |
+-----------------------------+
| 106 | 0 | 1 |
+-----------------------------+
| 107 | 0 | 1 |
+-----------------------------+
| 108 | 0 | 1 |
+-----------------------------+
| 109 | 0 | 1 |
+-----------------------------+
| 110 | 0 | 1 |
+-----------------------------+
| 111 | 0 | 1 |
+-----------------------------+
| 112 | 0 | 1 |
+-----------------------------+
| 113 | 0 | 1 |
+-----------------------------+
| 114 | 0 | 1 |
+-----------------------------+
| 115 | 0 | 1 |
+-----------------------------+
| 116 | 0 | 1 |
+-----------------------------+
| 117 | 0 | 1 |
+-----------------------------+
| 118 | 0 | 1 |
+-----------------------------+
| 119 | 0 | 1 |
+-----------------------------+
| 120 | 0 | 1 |
+-----------------------------+
| 121 | 0 | 1 |
+-----------------------------+
| 122 | 0 | 1 |
+-----------------------------+
| 123 | 0 | 1 |
+-----------------------------+
| 124 | 0 | 1 |
+-----------------------------+
| 125 | 0 | 1 |
+-----------------------------+
| 126 | 0 | 1 |
+-----------------------------+
| 127 | 0 | 1 |
+-----------------------------+
| 128 | 0 | 1 |
+-----------------------------+
| 129 | 0 | 1 |
+-----------------------------+
| 130 | 0 | 1 |
+-----------------------------+
| 131 | 0 | 1 |
+-----------------------------+
| 132 | 0 | 1 |
+-----------------------------+
| 133 | 0 | 1 |
+-----------------------------+
| 134 | 0 | 1 |
+-----------------------------+
| 135 | 0 | 1 |
+-----------------------------+
| 136 | 0 | 1 |
+-----------------------------+
| 137 | 0 | 1 |
+-----------------------------+
| 138 | 0 | 1 |
+-----------------------------+
| 139 | 0 | 1 |
+-----------------------------+
| 140 | 0 | 1 |
+-----------------------------+
| 141 | 0 | 1 |
+-----------------------------+
| 142 | 0 | 1 |
+-----------------------------+
| 143 | 0 | 1 |
+-----------------------------+
| 144 | 0 | 1 |
+-----------------------------+
| 145 | 0 | 1 |
+-----------------------------+
| 146 | 0 | 1 |
+-----------------------------+
| 147 | 0 | 1 |
+-----------------------------+
| 148 | 0 | 1 |
+-----------------------------+
| 149 | 0 | 1 |
+-----------------------------+
| 150 | 0 | 1 |
+-----------------------------+
| 151 | 0 | 1 |
+-----------------------------+
| 152 | 0 | 1 |
+-----------------------------+
| 153 | 0 | 1 |
+-----------------------------+
| 154 | 0 | 1 |
+-----------------------------+
| 155 | 0 | 1 |
+-----------------------------+
| 156 | 0 | 1 |
+-----------------------------+
| 157 | 0 | 1 |
+-----------------------------+
| 158 | 0 | 1 |
+-----------------------------+
| 159 | 0 | 1 |
+-----------------------------+
| 160 | 0 | 1 |
+-----------------------------+
| 161 | 0 | 1 |
+-----------------------------+
| 162 | 0 | 1 |
+-----------------------------+
| 163 | 0 | 1 |
+-----------------------------+
| 164 | 0 | 1 |
+-----------------------------+
| 165 | 0 | 1 |
+-----------------------------+
| 166 | 0 | 1 |
+-----------------------------+
| 167 | 0 | 1 |
+-----------------------------+
| 168 | 0 | 1 |
+-----------------------------+
| 169 | 0 | 1 |
+-----------------------------+
| 170 | 0 | 1 |
+-----------------------------+
| 171 | 0 | 1 |
+-----------------------------+
| 172 | 0 | 1 |
+-----------------------------+
| 173 | 0 | 1 |
+-----------------------------+
| 174 | 0 | 1 |
+-----------------------------+
| 175 | 0 | 1 |
+-----------------------------+
| 176 | 0 | 1 |
+-----------------------------+
| 177 | 0 | 1 |
+-----------------------------+
| 178 | 0 | 1 |
+-----------------------------+
| 179 | 0 | 1 |
+-----------------------------+
| 180 | 0 | 1 |
+-----------------------------+
| 181 | 0 | 1 |
+-----------------------------+
| 182 | 0 | 1 |
+-----------------------------+
| 183 | 0 | 1 |
+-----------------------------+
| 184 | 0 | 1 |
+-----------------------------+
| 185 | 0 | 1 |
+-----------------------------+
| 186 | 0 | 1 |
+-----------------------------+
| 187 | 0 | 1 |
+-----------------------------+
| 188 | 0 | 1 |
+-----------------------------+
| 189 | 0 | 1 |
+-----------------------------+
| 190 | 0 | 1 |
+-----------------------------+
| 191 | 0 | 1 |
+-----------------------------+
| 192 | 0 | 1 |
+-----------------------------+
| 193 | 0 | 1 |
+-----------------------------+
| 194 | 0 | 1 |
+-----------------------------+
| 195 | 0 | 1 |
+-----------------------------+
| 196 | 0 | 1 |
+-----------------------------+
| 197 | 0 | 1 |
+-----------------------------+
| 198 | 0 | 1 |
+-----------------------------+
| 199 | 0 | 1 |
+-----------------------------+
| 200 | 0 | 1 |
+-----------------------------+
| 201 | 0 | 1 |
+-----------------------------+
| 202 | 0 | 1 |
+-----------------------------+
| 203 | 0 | 1 |
+-----------------------------+
| 204 | 0 | 1 |
+-----------------------------+
| 205 | 0 | 1 |
+-----------------------------+
| 206 | 0 | 1 |
+-----------------------------+
| 207 | 0 | 1 |
+-----------------------------+
| 208 | 0 | 1 |
+-----------------------------+
| 209 | 0 | 1 |
+-----------------------------+
| 210 | 0 | 1 |
+-----------------------------+
| 211 | 0 | 1 |
+-----------------------------+
| 212 | 0 | 1 |
+-----------------------------+
| 213 | 0 | 1 |
+-----------------------------+
| 214 | 0 | 1 |
+-----------------------------+
| 215 | 0 | 1 |
+-----------------------------+
| 216 | 0 | 1 |
+-----------------------------+
| 217 | 0 | 1 |
+-----------------------------+
| 218 | 0 | 1 |
+-----------------------------+
| 219 | 0 | 1 |
+-----------------------------+
| 220 | 0 | 1 |
+-----------------------------+
| 221 | 0 | 1 |
+-----------------------------+
| 222 | 0 | 1 |
+-----------------------------+
| 223 | 0 | 1 |
+-----------------------------+
| 224 | 0 | 1 |
+-----------------------------+
| 225 | 0 | 1 |
+-----------------------------+
| 226 | 0 | 1 |
+-----------------------------+
| 227 | 0 | 1 |
+-----------------------------+
| 228 | 0 | 1 |
+-----------------------------+
| 229 | 0 | 1 |
+-----------------------------+
| 230 | 0 | 1 |
+-----------------------------+
| 231 | 0 | 1 |
+-----------------------------+
| 232 | 0 | 1 |
+-----------------------------+
| 233 | 0 | 1 |
+-----------------------------+
| 234 | 0 | 1 |
+-----------------------------+
| 235 | 0 | 1 |
+-----------------------------+
| 236 | 0 | 1 |
+-----------------------------+
| 237 | 0 | 1 |
+-----------------------------+
| 238 | 0 | 1 |
+-----------------------------+
| 239 | 0 | 1 |
+-----------------------------+
| 240 | 0 | 1 |
+-----------------------------+
| 241 | 0 | 1 |
+-----------------------------+
| 242 | 0 | 1 |
+-----------------------------+
| 243 | 0 | 1 |
+-----------------------------+
| 244 | 0 | 1 |
+-----------------------------+
| 245 | 0 | 1 |
+-----------------------------+
| 246 | 0 | 1 |
+-----------------------------+
| 247 | 0 | 1 |
+-----------------------------+
| 248 | 0 | 1 |
+-----------------------------+
| 249 | 0 | 1 |
+-----------------------------+
| 250 | 0 | 1 |
+-----------------------------+
| 251 | 0 | 1 |
+-----------------------------+
| 252 | 0 | 1 |
+-----------------------------+
| 253 | 0 | 1 |
+-----------------------------+
| 254 | 0 | 1 |
+-----------------------------+
| 255 | 0 | 1 |
+-----------------------------+
雪花算法生成的ID是一个64位的长整型数字,其中包含以下部分:
- 1位符号位:始终为0,表示正数。
- 41位时间戳:表示当前时间与某个固定时间点的差值,单位为毫秒。这部分可以表示约69年的时间。
- 10位机器标识:用于标识不同的机器或节点,最多可以支持1024个节点。
- 12位序列号:在同一毫秒内,同一个节点可以生成的ID序列号,最多可以生成4096个。
雪花算法的优点是生成的ID全局唯一且趋势递增,适用于分布式系统中的唯一ID生成场景。