Choosing the Right Distributed ID Generation Strategy for Different Business Scenarios
This article analyzes the diverse business requirements for unique identifiers—such as order numbers, QR‑code payments, coupons, trace IDs, and short URLs—and compares technical implementations like Redis auto‑increment, Snowflake, and custom base‑62 encoding to help engineers select the most suitable distributed ID generation solution.
What Is a Distributed ID Generation Service?
In modern software development, many features rely on unique identifiers: user authentication, product SKUs, instant messaging, and more. A distributed ID generation service provides globally unique IDs across multiple services and databases, ensuring no collisions even after sharding or scaling.
Key Service Characteristics
Uniqueness: IDs must not conflict within a defined scope.
Orderliness: IDs should be roughly increasing to simplify storage and queries.
High Availability & Performance: The service must handle high concurrency without downtime.
Autonomy: Instances should generate IDs independently without a central coordinator.
Security: IDs should not expose sensitive business metrics.
Common Technical Implementations
Typical approaches include Redis key auto‑increment, UUIDs, and Snowflake‑style algorithms. Simple database auto‑increment works for low‑traffic scenarios, but as data grows and sharding becomes necessary, a dedicated distributed ID service is required.
Scenario 1 – One‑Code Payment (QR‑Code Payment)
QR codes are essentially strings that encode a URL. By detecting the client’s payment environment via the HTTP User‑Agent header, a backend can dynamically bind product information and price to the QR code, allowing a single code to trigger payment across multiple platforms (WeChat, Alipay, QQ Wallet, etc.). This reduces user friction and improves checkout speed.
Scenario 2 – Order Numbers
Order IDs serve as the primary key for customer service, payment reconciliation, and order lifecycle management. Design considerations include:
Security: IDs must not reveal daily sales volume, user phone numbers, or other private data.
Partial Readability: Embedding date or type information helps staff quickly locate orders.
Query Efficiency: Numeric IDs (INT) are faster to index and query than VARCHAR strings.
Scenario 3 – Coupons and Redemption Codes
Coupon codes need to be pre‑generated in large volumes (often >100 000) and must be non‑guessable, verifiable, and optionally bindable to specific campaigns. A typical encoding scheme combines:
Campaign ID (15 bits, up to 32 768 campaigns)
Serial number (30 bits, up to 1 billion codes per campaign)
Checksum (13 bits, split into 6 + 7 bits for validation and data dispersion)
The character set excludes ambiguous characters (O, I) and consists of 60 symbols: abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXZY0123456789. With a maximum length of 12 characters, the space is 60^12 ≈ 1.3×10^23, more than enough for any promotional activity.
Scenario 4 – Distributed Tracing (TraceId & SpanId)
In micro‑service architectures, each request carries a traceId that links all downstream calls. A high‑performance traceId can be generated locally using:
traceId = IP(8hex) + timestamp(13hex) + sequence(4hex) + pid(5hex)Example: 0ad1348f1403169275002100356696 where the first 8 hex digits decode to the server IP, the next 13 hex digits represent the generation time, followed by an auto‑incrementing sequence and the process ID.
SpanId reflects the call hierarchy. The root service uses 0, its direct children use 0.1, 0.2, etc., and deeper levels append additional segments (e.g., 0.2.1, 0.2.2), forming a complete tree of the request flow.
Scenario 5 – Short URL Service
Short URLs map long links to compact identifiers. After generating a numeric ID, converting it to a higher base (e.g., base‑62) dramatically reduces length. The following PHP functions implement the conversion:
/**
* Convert decimal to base‑62 string
* @param int $n Decimal number
* @return string Base‑62 representation
*/
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;
}
/**
* Convert base‑62 string back to decimal
* @param string $s Base‑62 string
* @return int Decimal number
*/
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;
}Using this method, a numeric ID like 2047601319 can be represented as 2ezwDJ0, yielding a short, unique URL such as https://dwz.cn/2ezwDJ0.
Summary
Different business scenarios impose distinct requirements on ID generation services. Order systems need secure, partially readable IDs; coupon systems demand massive, non‑guessable codes with built‑in verification; tracing requires locally generated, high‑throughput IDs; short‑URL services prioritize minimal length. Selecting the appropriate technical solution—whether Redis auto‑increment, Snowflake, custom base‑62 encoding, or a hybrid approach—ensures the identifier infrastructure scales with the product and supports future growth.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
