How Meituan’s Leaf Service Generates High‑Performance Distributed IDs
This guide explains how the Leaf distributed ID service, built with Spring Boot, supports segment and Snowflake modes, details its configuration, deployment steps, code examples, and operational considerations for high‑throughput backend systems.
Leaf is a distributed ID generation service originally built to meet Meituan’s various business line order ID requirements, replacing DB auto‑increment, Redis cache, and UUID approaches.
It now serves many Meituan‑Dianping services (finance, food delivery, hotel, movie, etc.) with up to 50 k QPS and 1 ms 99.9th‑percentile latency on a 4C8G VM via RPC.
Quick Start
1. Leaf Server
A Spring Boot HTTP service provides ID retrieval.
2. Configuration
Leaf supports two generation modes: segment mode and Snowflake mode, both disabled by default. Settings are in leaf-server/src/main/resources/leaf.properties.
Key configuration items include:
leaf.name : service name
leaf.segment.enable : enable segment mode (default false)
leaf.jdbc.url , leaf.jdbc.username , leaf.jdbc.password : MySQL connection
leaf.snowflake.enable : enable Snowflake mode (default false)
leaf.snowflake.zk.address : Zookeeper address for Snowflake
leaf.snowflake.port : service port for Snowflake
3. Segment Mode
Requires a MySQL table leaf_alloc with columns biz_tag, max_id, step, description, and update_time. Example DDL and initial data:
CREATE DATABASE leaf;
CREATE TABLE `leaf_alloc` (
`biz_tag` varchar(128) NOT NULL DEFAULT '',
`max_id` bigint(20) NOT NULL DEFAULT '1',
`step` int(11) NOT NULL,
`description` varchar(256) DEFAULT NULL,
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`biz_tag`)
) ENGINE=InnoDB;
INSERT INTO leaf_alloc(biz_tag, max_id, step, description)
VALUES('leaf-segment-test', 1, 2000, 'Test leaf Segment Mode Get Id');Set leaf.segment.enable=true and provide the JDBC settings to use this mode.
4. Snowflake Mode
Implements Twitter’s Snowflake algorithm. Enable with leaf.snowflake.enable=true and configure Zookeeper address and port.
5. Run Leaf Server
Build the project:
git clone [email protected]:Meituan-Dianping/Leaf.git
cd leaf
mvn clean install -DskipTests
cd leaf-serverStart the service either via Maven: mvn spring-boot:run or using the provided script: sh deploy/run.sh Test the APIs:
# segment
curl http://localhost:8080/api/segment/get/leaf-segment-test
# snowflake
curl http://localhost:8080/api/snowflake/get/testMonitoring pages: segment mode at http://localhost:8080/cache.
Leaf Core
For higher performance, integrate leaf-core into an RPC framework and expose the ID generation API.
Precautions
When using Snowflake mode, the service obtains the first network interface IP as the worker ID; services with changing IPs should handle this to avoid exhausting worker IDs.
Project repository: https://github.com/Meituan-Dianping/Leaf
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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
