Backend Development 9 min read

A High-Performance Strictly Incremental Distributed ID Generation Scheme Using MySQL, Redis, and Nacos

The article proposes a high‑performance, strictly incremental distributed ID generation scheme that combines MySQL segment tables, Redis caching, and Nacos‑controlled switching, achieving sub‑millisecond latency and 7‑10× speedup over pure database methods while preserving fault tolerance, availability, and global uniqueness.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
A High-Performance Strictly Incremental Distributed ID Generation Scheme Using MySQL, Redis, and Nacos

The article discusses the importance of distributed ID generation in growing business data within distributed systems, outlining required properties: high fault tolerance and availability, high performance and concurrency, and global uniqueness.

It reviews the initial database table-based sequence approach, noting its average latency of 1.46 ms (same‑machine) and 5.32 ms (cross‑machine), and highlights its drawbacks under high QPS, increased cross‑machine latency, complications after sharding, and rare deadlocks, while acknowledging its benefit of strictly increasing IDs.

The piece then surveys common distributed ID schemes: UUID, Snowflake algorithm, Baidu’s UidGenerator, and Meituan’s Leaf (snowflake and segment modes), summarizing each scheme’s advantages and disadvantages.

Recognizing the need for strictly increasing IDs for the author’s business, the article proposes a custom solution combining a database segment pattern, Redis caching, and Nacos‑driven switch‑over, using MySQL + Redis + Nacos as the concrete stack.

Details include a MySQL sequence table (bizId, maxId, step), Redis keys (currentId, maxId, step), and a Nacos toggle that controls whether IDs are generated from Redis cache or directly from the database, with listeners to automatically switch modes.

The generation flow is described: at startup, if the Nacos switch is open, Redis is primed from the database; on each write request, if Redis is healthy and the switch is on, the service increments Redis currentId, validates the ID, asynchronously pre‑fetches the next segment when 30 % of the current segment is used; otherwise it falls back to the database, locking and incrementing maxId.

Special‑case handling covers Redis cluster unavailability (triggering a switch to database mode via Nacos) and recovery (switching back to Redis after the issue is resolved).

The article concludes that the Redis‑based approach yields sub‑millisecond latency, improves same‑machine ID retrieval performance by roughly 10× and cross‑machine by about 7× versus the pure database method, eliminates prior deadlock incidents, and provides a fallback that maintains business continuity.

Finally, it notes that if strict monotonic increase is not required, existing frameworks such as UUID, Snowflake, UidGenerator, or Leaf can be used directly.

backend designhigh performanceRedis cachingdistributed IDMySQL segmentNacos switchstrictly increasing
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

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.