Snowflake ID Collision in Production: Why Custom ID Generators Fail

A production incident caused by a flawed custom Snowflake ID generator with a 31-bit timestamp (24-day cycle), IP-based BusinessId collisions, and unconfigured WorkerId/DataCenterId led to duplicate order IDs; the article analyzes the root causes and recommends mature libraries like Hutool and Baomidou with proper WorkerId assignment strategies.

Top Architect
Top Architect
Top Architect
Snowflake ID Collision in Production: Why Custom ID Generators Fail

Standard Snowflake Algorithm

The standard Snowflake ID is a 64-bit long integer composed of:

+----------------------------------------------------------------------------------------------------+
| 1 Bit | 41 Bits Timestamp | 5 Bits DataCenterId | 5 Bits WorkerId | 12 Bits Sequence |
+----------------------------------------------------------------------------------------------------+

1 sign bit : always 0 to ensure positive numbers.

41-bit timestamp : milliseconds since a custom epoch, supporting ~69 years.

10-bit machine ID (5 DataCenterId + 5 WorkerId): identifies nodes in a distributed cluster.

12-bit sequence : allows up to 4096 IDs per millisecond per node.

Advantages: high-performance unique ID generation, time-ordered, suitable for distributed environments.

The Flawed Custom Implementation

The team's second-party package used a non-standard 64-bit layout (inferred from investigation):

+----------------------------------------------------------------------------------------------------+
| 31 Bits TimestampDelta | 13 Bits DataCenterId | 4 Bits WorkerId | 8 Bits BusinessId | 8 Bits Sequence |
+----------------------------------------------------------------------------------------------------+

Three critical defects were found:

1. 31-bit Timestamp — 24.85-Day Cycle

After left-shifting 33 bits, only 31 bits remain for the timestamp delta.

2^31 milliseconds ≈ 24.85 days before the timestamp wraps around.

The custom epoch was set to 2018; by 2025 the timestamp had cycled countless times, causing ID reuse.

2. BusinessId Derived from IP Last Octet

The 8-bit BusinessId used the last octet of the IP address (e.g., 1 from 192.168.0.1). In any sizable deployment, multiple nodes share the same last octet, guaranteeing collisions.

3. WorkerId and DataCenterId Left Unconfigured (All Zeros)

Both fields defaulted to 0, so every instance produced IDs with identical node identifiers. Uniqueness relied solely on timestamp and sequence, which failed once the timestamp wrapped.

Result: timestamp wrap-around + IP octet collision + sequence reuse = total ID collision.

Lessons Learned

Don't reinvent common components : Snowflake involves clock drift, bit manipulation, and distributed coordination; battle-tested libraries are safer.

Don't blindly trust second-party packages : Inspect the implementation logic and understand its uniqueness guarantees regardless of the author.

Configure machine IDs properly : IP last octet is fragile; centrally plan and assign WorkerId and DataCenterId.

Test boundary conditions early : Simulate long-running operation, sequence overflow, and clock rollback to ensure robustness.

Recommended Practices

Use mature open-source implementations:

// Hutool example
Snowflake snowflake = IdUtil.getSnowflake(1, 1);
long id = snowflake.nextId();

// Baomidou example (supports IP/MAC auto-derivation or manual)
DefaultIdentifierGenerator generator = new DefaultIdentifierGenerator(1, 1); // workerId=1, dataCenterId=1
long id = generator.nextId("user");

For medium-to-large systems, DataCenterId typically identifies data centers or availability zones. WorkerId assignment can evolve with scale:

Simple : manual config file — easy, for dev or single-node.

Standard : hash(IP + port/process) mod WorkerId pool — automated, no external dependency, for small-to-medium.

Intermediate : registry (Eureka, Nacos) assigns ID at registration — leverages service ID for uniqueness.

Advanced : centralized coordination via Redis/ZooKeeper — dynamic scaling, conflict-free allocation.

Adopt progressively; avoid over-engineering initially.

Additional Advice: Keep Business Metadata Out of the ID

Embedding business prefixes or module codes into the ID causes:

Loss of pure numeric, time-ordered property → degrades database index performance.

Irregular or longer IDs → higher storage cost, log readability issues.

Compatibility risks when business meanings change.

Store business fields separately; let the ID serve only as a unique, sortable key.

Closing

Don't build wheels for the sake of building wheels, especially for foundational components. Hope is not a strategy.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

backend developmentsystem designhutoolsnowflake-algorithmproduction-incidentdistributed-id-generationbaomidouworker-id-assignment
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

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.