Backend Development 18 min read

Distributed ID Generation Service: Features, Scenarios, and Implementation

This article explains the need for globally unique identifiers in modern applications, outlines the essential characteristics of a distributed ID generation service, examines common implementation techniques, and discusses concrete business scenarios such as order numbers, coupons, short URLs, and tracing with practical code examples.

Top Architect
Top Architect
Top Architect
Distributed ID Generation Service: Features, Scenarios, and Implementation

In many business applications, unique identifiers are required for user authentication, product tracking, messaging, and more; ensuring uniqueness within a specific scope is the fundamental requirement of an ID generation service.

When multiple services need globally unique IDs, a distributed ID generation service becomes essential, avoiding duplicated efforts and simplifying service governance.

Key service characteristics include:

Uniqueness – IDs must not conflict within the defined range.

Orderliness – IDs follow a loosely increasing order to aid storage and query.

High availability and performance – the service must handle high concurrency and remain resilient.

Autonomy – instances can generate IDs without a central coordinator.

Security – IDs should be opaque and not expose business or personal data.

Common technical approaches are Redis auto‑increment keys, UUIDs, and Snowflake‑style algorithms; simple database auto‑increment works for low volume, but scaling requires sharding and a dedicated ID service.

Scenario 1 – Order System : Order numbers, QR‑code payment links, and coupon codes all need IDs; the article analyses why some IDs are long, some short, and how to design them for readability and security.

Scenario 2 – Order Number : Order IDs must support customer service lookup, internal workflow, and security constraints such as non‑sequential, non‑predictable numbers while still allowing partial readability (e.g., date information) and efficient integer‑based queries.

Scenario 3 – Coupons and Redemption Codes : Large‑scale coupon generation requires a 60‑character alphabet (excluding ambiguous characters), a maximum length of 12, and a structure of schemeID + sequence + checksum to guarantee uniqueness, prevent cracking, and enable verification.

Scenario 4 – Tracing : Distributed tracing relies on a traceId (global request identifier) and spanId (per‑service segment). The article describes a generation rule combining server IP, timestamp, a local counter, and process ID, e.g., 0ad1348f1403169275002100356696 , and explains how the components map to IP, time, sequence, and process.

Short URL Generation : Converting long URLs to short ones can be done by mapping a numeric ID to a higher‑base (62) representation. Example PHP functions are provided:

/**
 * 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 int 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;
}

These functions compress numeric IDs into short strings (e.g., six characters can represent billions of URLs) while allowing reversible decoding.

Conclusion : Different business scenarios impose specific ID requirements—security for order numbers, partial readability for coupons, compactness for short URLs, and traceability for distributed tracing. Selecting an appropriate generation algorithm and encoding scheme ensures the service meets both functional and performance goals.

backendPHPtraceabilitydistributed IDunique identifierShort URLcoupon code
Top Architect
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.