Distributed ID Generation Service: Principles, Features, and Common Implementations
The article explains the need for globally unique IDs in various business scenarios, outlines essential service characteristics such as uniqueness, ordering, high availability, and security, and details common technical implementations including Snowflake, Redis auto‑increment, short‑URL encoding, and custom coupon code schemes.
In modern business development, many scenarios require a unique identifier to mark entities such as users, products, messages, and orders; these identifiers must be unique within a specific scope, which is the fundamental requirement of an ID generation service.
Various ID generation methods exist, including Redis key auto‑increment, UUID, and Snowflake‑based services. While simple auto‑increment IDs work for small‑scale databases, they become inadequate as data volume grows, necessitating distributed ID services that can provide globally unique identifiers.
Distributed ID services should satisfy several key characteristics:
Uniqueness: IDs must not collide within the defined range.
Orderliness: IDs should be roughly increasing to aid storage and query performance.
High availability and performance: The service must handle high concurrency and remain resilient.
Autonomy: Instances should generate IDs without relying on a central authority.
Security: IDs must not expose sensitive business information.
The article then examines concrete business scenarios:
1. One‑code payment (一码付)
Dynamic QR codes embed user‑selected product information and price; the backend generates the order only after the user scans the code, then forwards payment details to third‑party platforms (WeChat, Alipay, etc.).
2. Order numbers
Order IDs serve as unique references for customer service, payment, and lifecycle management. Design considerations include information security (no leakage of sales volume or personal data), partial readability (embedding timestamps or type codes for easier troubleshooting), and query efficiency (numeric IDs are faster to index than strings).
3. Coupons and redemption codes
Coupons are widely used for promotions; they require pre‑generation, large volume, tamper‑resistance, and post‑use verification. A typical code structure combines a promotion ID, a sequential index, and a checksum, using a 60‑character alphabet (a‑z, A‑Z without O/I, 0‑9) to keep codes short yet unique.
Example encoding scheme (PHP):
/**
* 10进制转为62进制
* @param integer $n 10进制数值
* @return string 62进制
*/
function dec62($n) {
$base = 62;
$index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$ret = '';
for($t = floor(log10($n) / log10($base)); $t >= 0; $t--) {
$a = floor($n / pow($base, $t));
$ret .= substr($index, $a, 1);
$n -= $a * pow($base, $t);
}
return $ret;
}
/**
* 62进制转为10进制
* @param string $s 62进制
* @return integer 10进制
*/
function dec10($s) {
$base = 62;
$index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$ret = 0;
$len = strlen($s) - 1;
for($t = 0; $t <= $len; $t++) {
$ret += strpos($index, substr($s, $t, 1)) * pow($base, $len - $t);
}
return $ret;
}Short‑URL services map long URLs to compact identifiers; by converting numeric IDs to a higher base (e.g., base‑62), a 6‑character string can represent billions of URLs. The article provides the conversion algorithm above.
4. Tracing IDs (TraceId and SpanId)
In distributed systems, a request traverses multiple services; each request carries a TraceId (global identifier) and each service generates a SpanId (local segment). A typical TraceId format combines server IP, timestamp, a local sequence, and process ID (e.g., 0ad1348f1403169275002100356696 ), enabling quick identification of the originating host.
SpanId reflects the call hierarchy (e.g., root=0, first child=0.1, second child=0.2, etc.), allowing reconstruction of the entire call tree for debugging.
Overall, the article emphasizes that ID generation services must not only provide basic uniqueness and ordering but also satisfy domain‑specific requirements such as security, readability, and compactness, and that selecting the appropriate technical solution based on the scenario is crucial for supporting business growth.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.