Tạo và phân tích các ID Snowflake Twitter, các định danh 64-bit duy nhất được sử dụng trong các hệ thống phân tán. Công cụ này cho phép bạn tạo các ID Snowflake mới và phân tích các ID hiện có, cung cấp thông tin về thành phần thời gian, ID máy và số thứ tự của chúng.
Một trình tạo ID Snowflake tạo ra các định danh độc nhất cho các hệ thống phân tán, ban đầu được phát triển bởi Twitter để xử lý việc xử lý dữ liệu quy mô lớn. Trình tạo ID độc nhất mạnh mẽ này sản xuất các số nguyên 64-bit bao gồm một dấu thời gian, ID máy, và số thứ tự, đảm bảo tính độc nhất trên các hệ thống phân tán mà không cần phối hợp giữa các máy chủ.
Công cụ trình tạo ID Snowflake trực tuyến miễn phí của chúng tôi cho phép bạn tạo và phân tích các ID Snowflake ngay lập tức, làm cho nó trở nên hoàn hảo cho các nhà phát triển làm việc với microservices, cơ sở dữ liệu phân tán, và các ứng dụng có lưu lượng cao.
ID Snowflake là các số nguyên 64-bit với một cấu trúc được thiết kế cẩn thận đảm bảo tính độc nhất:
Cấu trúc ID phân tán này cho phép tạo ra khoảng 4,096 ID độc nhất mỗi miligiây mỗi máy, làm cho nó lý tưởng cho các hệ thống phân tán có lưu lượng cao.
Thực hiện theo các bước đơn giản này để tạo các ID Snowflake độc nhất:
Để giải mã một ID Snowflake, nhập nó vào trường "Phân tích ID" và nhấp vào "Phân tích" để xem dấu thời gian, ID máy, và các thành phần số thứ tự của nó.
Thuật toán ID Snowflake xây dựng các định danh độc nhất bằng cách sử dụng các phép toán bitwise:
1ID = (timestamp << 22) | (datacenterId << 17) | (workerId << 12) | sequence
2
Các thành phần công thức:
timestamp
: Số miligiây kể từ thời điểmdatacenterId
: Số nguyên 5 bit (0-31) xác định trung tâm dữ liệuworkerId
: Số nguyên 5 bit (0-31) xác định máy công nhânsequence
: Số nguyên 12 bit (0-4095) cho nhiều ID mỗi miligiâyThuật toán tạo ID Snowflake tuân theo các bước chính xác sau:
Quy trình này đảm bảo các ID tăng dần theo thứ tự trong mỗi máy trong khi duy trì tính độc nhất toàn cầu trên các hệ thống phân tán.
ID Snowflake xuất sắc trong nhiều kịch bản tính toán phân tán:
Mặc dù ID Snowflake rất mạnh mẽ, các hệ thống tạo ID độc nhất khác bao gồm:
Hiểu rõ các giới hạn của ID Snowflake giúp trong việc triển khai đúng cách:
ID Snowflake được giới thiệu bởi Twitter vào năm 2010 để giải quyết thách thức tạo ra các định danh độc nhất, có thể sắp xếp theo thời gian trong quy mô lớn. Khi số lượng người dùng và khối lượng tweet của Twitter bùng nổ, các ID tự tăng truyền thống trở nên không đủ cho kiến trúc phân tán của họ.
Hệ thống này đã được các công ty công nghệ lớn áp dụng bao gồm Instagram, Discord, và vô số nền tảng khác yêu cầu tạo ID có thể mở rộng cho các hệ thống phân tán.
Triển khai tạo ID Snowflake trong ngôn ngữ lập trình bạn ưa thích:
1class SnowflakeGenerator {
2 constructor(epoch = 1288834974657, datacenterIdBits = 5, workerIdBits = 5, sequenceBits = 12) {
3 this.epoch = BigInt(epoch);
4 this.datacenterIdBits = datacenterIdBits;
5 this.workerIdBits = workerIdBits;
6 this.sequenceBits = sequenceBits;
7 this.maxDatacenterId = -1n ^ (-1n << BigInt(datacenterIdBits));
8 this.maxWorkerId = -1n ^ (-1n << BigInt(workerIdBits));
9 this.sequenceMask = -1n ^ (-1n << BigInt(sequenceBits));
10 this.workerIdShift = BigInt(sequenceBits);
11 this.datacenterIdShift = BigInt(sequenceBits + workerIdBits);
12 this.timestampLeftShift = BigInt(sequenceBits + workerIdBits + datacenterIdBits);
13 this.sequence = 0n;
14 this.lastTimestamp = -1n;
15 }
16
17 nextId(datacenterId, workerId) {
18 let timestamp = this.currentTimestamp();
19
20 if (timestamp < this.lastTimestamp) {
21 throw new Error('Đồng hồ đã di chuyển ngược. Từ chối tạo ID');
22 }
23
24 if (timestamp === this.lastTimestamp) {
25 this.sequence = (this.sequence + 1n) & this.sequenceMask;
26 if (this.sequence === 0n) {
27 timestamp = this.tilNextMillis(this.lastTimestamp);
28 }
29 } else {
30 this.sequence = 0n;
31 }
32
33 this.lastTimestamp = timestamp;
34
35 return ((timestamp - this.epoch) << this.timestampLeftShift) |
36 (BigInt(datacenterId) << this.datacenterIdShift) |
37 (BigInt(workerId) << this.workerIdShift) |
38 this.sequence;
39 }
40
41 tilNextMillis(lastTimestamp) {
42 let timestamp = this.currentTimestamp();
43 while (timestamp <= lastTimestamp) {
44 timestamp = this.currentTimestamp();
45 }
46 return timestamp;
47 }
48
49 currentTimestamp() {
50 return BigInt(Date.now());
51 }
52}
53
54// Sử dụng
55const generator = new SnowflakeGenerator();
56const id = generator.nextId(1, 1);
57console.log(`ID Snowflake đã tạo: ${id}`);
58
1import time
2import threading
3
4class SnowflakeGenerator:
5 def __init__(self, datacenter_id, worker_id, sequence=0):
6 self.datacenter_id = datacenter_id
7 self.worker_id = worker_id
8 self.sequence = sequence
9
10 self.last_timestamp = -1
11 self.epoch = 1288834974657
12
13 self.datacenter_id_bits = 5
14 self.worker_id_bits = 5
15 self.sequence_bits = 12
16
17 self.max_datacenter_id = -1 ^ (-1 << self.datacenter_id_bits)
18 self.max_worker_id = -1 ^ (-1 << self.worker_id_bits)
19
20 self.worker_id_shift = self.sequence_bits
21 self.datacenter_id_shift = self.sequence_bits + self.worker_id_bits
22 self.timestamp_left_shift = self.sequence_bits + self.worker_id_bits + self.datacenter_id_bits
23 self.sequence_mask = -1 ^ (-1 << self.sequence_bits)
24
25 self._lock = threading.Lock()
26
27 def _til_next_millis(self, last_timestamp):
28 timestamp = self._get_timestamp()
29 while timestamp <= last_timestamp:
30 timestamp = self._get_timestamp()
31 return timestamp
32
33 def _get_timestamp(self):
34 return int(time.time() * 1000)
35
36 def next_id(self):
37 with self._lock:
38 timestamp = self._get_timestamp()
39
40 if timestamp < self.last_timestamp:
41 raise ValueError("Đồng hồ đã di chuyển ngược. Từ chối tạo ID")
42
43 if timestamp == self.last_timestamp:
44 self.sequence = (self.sequence + 1) & self.sequence_mask
45 if self.sequence == 0:
46 timestamp = self._til_next_millis(self.last_timestamp)
47 else:
48 self.sequence = 0
49
50 self.last_timestamp = timestamp
51
52 return ((timestamp - self.epoch) << self.timestamp_left_shift) | \
53 (self.datacenter_id << self.datacenter_id_shift) | \
54 (self.worker_id << self.worker_id_shift) | \
55 self.sequence
56
57## Sử dụng
58generator = SnowflakeGenerator(datacenter_id=1, worker_id=1)
59snowflake_id = generator.next_id()
60print(f"ID Snowflake đã tạo: {snowflake_id}")
61
1import java.util.concurrent.locks.Lock;
2import java.util.concurrent.locks.ReentrantLock;
3
4public class SnowflakeGenerator {
5 private final long epoch;
6 private final long datacenterIdBits;
7 private final long workerIdBits;
8 private final long sequenceBits;
9 private final long maxDatacenterId;
10 private final long maxWorkerId;
11 private final long workerIdShift;
12 private final long datacenterIdShift;
13 private final long timestampLeftShift;
14 private final long sequenceMask;
15
16 private long datacenterId;
17 private long workerId;
18 private long sequence = 0L;
19 private long lastTimestamp = -1L;
20
21 private final Lock lock = new ReentrantLock();
22
23 public SnowflakeGenerator(long datacenterId, long workerId) {
24 this.epoch = 1288834974657L;
25 this.datacenterIdBits = 5L;
26 this.workerIdBits = 5L;
27 this.sequenceBits = 12L;
28
29 this.maxDatacenterId = ~(-1L << datacenterIdBits);
30 this.maxWorkerId = ~(-1L << workerIdBits);
31
32 this.workerIdShift = sequenceBits;
33 this.datacenterIdShift = sequenceBits + workerIdBits;
34 this.timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
35 this.sequenceMask = ~(-1L << sequenceBits);
36
37 if (datacenterId > maxDatacenterId || datacenterId < 0) {
38 throw new IllegalArgumentException("datacenterId không thể lớn hơn maxDatacenterId hoặc nhỏ hơn 0");
39 }
40 if (workerId > maxWorkerId || workerId < 0) {
41 throw new IllegalArgumentException("workerId không thể lớn hơn maxWorkerId hoặc nhỏ hơn 0");
42 }
43 this.datacenterId = datacenterId;
44 this.workerId = workerId;
45 }
46
47 public long nextId() {
48 lock.lock();
49 try {
50 long timestamp = timeGen();
51 if (timestamp < lastTimestamp) {
52 throw new RuntimeException("Đồng hồ đã di chuyển ngược. Từ chối tạo ID");
53 }
54
55 if (lastTimestamp == timestamp) {
56 sequence = (sequence + 1) & sequenceMask;
57 if (sequence == 0) {
58 timestamp = tilNextMillis(lastTimestamp);
59 }
60 } else {
61 sequence = 0L;
62 }
63
64 lastTimestamp = timestamp;
65
66 return ((timestamp - epoch) << timestampLeftShift) |
67 (datacenterId << datacenterIdShift) |
68 (workerId << workerIdShift) |
69 sequence;
70 } finally {
71 lock.unlock();
72 }
73 }
74
75 private long tilNextMillis(long lastTimestamp) {
76 long timestamp = timeGen();
77 while (timestamp <= lastTimestamp) {
78 timestamp = timeGen();
79 }
80 return timestamp;
81 }
82
83 private long timeGen() {
84 return System.currentTimeMillis();
85 }
86
87 public static void main(String[] args) {
88 SnowflakeGenerator generator = new SnowflakeGenerator(1, 1);
89 long id = generator.nextId();
90 System.out.println("ID Snowflake đã tạo: " + id);
91 }
92}
93
1require 'time'
2
3class SnowflakeGenerator
4 def initialize(datacenter_id, worker_id, sequence = 0)
5 @datacenter_id = datacenter_id
6 @worker_id = worker_id
7 @sequence = sequence
8 @last_timestamp = -1
9 @epoch = 1288834974657
10
11 @datacenter_id_bits = 5
12 @worker_id_bits = 5
13 @sequence_bits = 12
14
15 @max_datacenter_id = -1 ^ (-1 << @datacenter_id_bits)
16 @max_worker_id = -1 ^ (-1 << @worker_id_bits)
17
18 @worker_id_shift = @sequence_bits
19 @datacenter_id_shift = @sequence_bits + @worker_id_bits
20 @timestamp_left_shift = @sequence_bits + @worker_id_bits + @datacenter_id_bits
21 @sequence_mask = -1 ^ (-1 << @sequence_bits)
22 end
23
24 def next_id
25 timestamp = (Time.now.to_f * 1000).to_i
26
27 raise 'Đồng hồ đã di chuyển ngược' if timestamp < @last_timestamp
28
29 if timestamp == @last_timestamp
30 @sequence = (@sequence + 1) & @sequence_mask
31 timestamp = til_next_millis(@last_timestamp) if @sequence == 0
32 else
33 @sequence = 0
34 end
35
36 @last_timestamp = timestamp
37
38 ((timestamp - @epoch) << @timestamp_left_shift) |
39 (@datacenter_id << @datacenter_id_shift) |
40 (@worker_id << @worker_id_shift) |
41 @sequence
42 end
43
44 private
45
46 def til_next_millis(last_timestamp)
47 timestamp = (Time.now.to_f * 1000).to_i
48 timestamp = (Time.now.to_f * 1000).to_i while timestamp <= last_timestamp
49 timestamp
50 end
51end
52
53## Sử dụng
54generator = SnowflakeGenerator.new(1, 1)
55snowflake_id = generator.next_id
56puts "ID Snowflake đã tạo: #{snowflake_id}"
57
<?php class SnowflakeGenerator { private $epoch; private $datacenterIdBits; private $workerIdBits; private $sequenceBits; private $maxDatacenterId; private $maxWorkerId; private $workerIdShift; private $datacenterIdShift; private $timestampLeftShift; private $sequenceMask; private $datacenterId; private $workerId; private $sequence = 0; private $lastTimestamp = -1; public function __construct($datacenterId, $workerId) { $this->epoch = 1288834974657; $this->datacenterIdBits = 5; $this->workerIdBits = 5; $this->sequenceBits = 12; $this->maxDatacenterId =
Khám phá thêm các công cụ có thể hữu ích cho quy trình làm việc của bạn