Mastering TinyID: A High‑Performance Distributed ID System for Java Backend

This article introduces TinyID, a distributed ID service originally developed by Didi, explains its features, segment‑based architecture, database schema, HTTP and client APIs, configuration steps, and provides practical code examples for integrating the system into Java backend applications.

macrozheng
macrozheng
macrozheng
Mastering TinyID: A High‑Performance Distributed ID System for Java Backend

TinyID Overview

TinyID is a distributed ID system created by Didi, built on the Leaf segment algorithm and enhanced to support multi‑master database mode and a convenient tinyid-client for client integration. Unlike Leaf, TinyID only supports the segment mode, not the Snowflake mode.

TinyID Features

Globally unique long‑type IDs

Monotonically increasing IDs

HTTP and Java client access methods

Batch ID retrieval

Support for odd‑numbered sequences (1,3,5,…)

Multiple database configurations

Applicable Scenarios : Systems that only need numeric, increasing IDs and can tolerate non‑continuous IDs and occasional ID waste.

Unsuitable Scenarios : Order‑ID use cases where IDs are mostly continuous, as they could be guessed or used to infer order volume.

TinyID Principle

TinyID uses a segment‑based approach: a range of auto‑increment IDs is fetched from the database (e.g., (1,1000] representing 1000 IDs). The service loads the segment into memory and generates IDs locally. When the current segment reaches a usage threshold, the next segment is loaded asynchronously to ensure continuous availability.

The principle diagram:

TinyID principle diagram
TinyID principle diagram

Database Schema

tiny_id_info

stores segment information for each business type.

CREATE TABLE `tiny_id_info` ( ... ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='id information table';
tiny_id_token

is a permission table indicating which tokens can operate on which business types.

Token permission diagram
Token permission diagram

Configuration

Modify offline/application.properties to configure multiple master databases:

datasource.tinyid.primary.driver-class-name=com.mysql.jdbc.Driver
datasource.tinyid.primary.url=jdbc:mysql://127.0.0.1:3306/xin-master?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
datasource.tinyid.primary.username=junkang
datasource.tinyid.primary.password=junkang
...

datasource.tinyid.secondary.driver-class-name=com.mysql.jdbc.Driver
datasource.tinyid.secondary.url=jdbc:mysql://localhost:3306/db2?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
datasource.tinyid.secondary.username=root
datasource.tinyid.secondary.password=123456
...

HTTP API

TinyID provides four HTTP endpoints for ID and segment retrieval. Example controller snippet:

@RestController
@RequestMapping("/id/")
public class IdController {
    @Autowired
    private IdGeneratorFactoryServer idGeneratorFactoryServer;
    @RequestMapping("nextId")
    public Response<List<Long>> nextId(String bizType, Integer batchSize, String token) { ... }
    @RequestMapping("nextIdSimple")
    public String nextIdSimple(String bizType, Integer batchSize, String token) { ... }
    @RequestMapping("nextSegmentId")
    public Response<SegmentId> nextSegmentId(String bizType, String token) { ... }
    @RequestMapping("nextSegmentIdSimple")
    public String nextSegmentIdSimple(String bizType, String token) { ... }
}

Sample requests:

GET http://localhost:9999/tinyid/id/nextId?bizType=test&token=0f673adf80504e2eaa552f5d791b644c
Response: {"data":[2],"code":200,"message":""}

GET http://localhost:9999/tinyid/id/nextIdSimple?bizType=test&token=0f673adf80504e2eaa552f5d791b644c
Response: 3

TinyID‑Client Usage

Include the client dependency:

<dependency>
    <groupId>com.xiaoju.uemc.tinyid</groupId>
    <artifactId>tinyid-client</artifactId>
    <version>${tinyid.version}</version>
</dependency>

Configure the server address and token in application.properties:

tinyid.server=127.0.0.1:9999
tinyid.token=0f673adf80504e2eaa552f5d791b644c

Java code example:

// Single ID
Long id = TinyId.nextId("test");
// Batch IDs
List<Long> ids = TinyId.nextId("test", 10);

Conclusion

Both HTTP and client modes are supported, but the tinyid-client is recommended because IDs are generated locally; a larger segment size ( step) yields higher QPS (potentially >10 million). This reduces load on the TinyID server.

Project Repository

GitHub: https://github.com/didi/tinyid

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.

Backendjavadistributed-idsegment IDTinyid
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.