explaingit

meituan-dianping/leaf

6,749JavaAudience · developerComplexity · 4/5Setup · hard

TLDR

A high-performance Java service from Meituan that generates unique ID numbers for distributed systems, handling nearly 50,000 requests per second with sub-millisecond response times via a simple HTTP API.

Mindmap

mindmap
  root((leaf))
    What it does
      Unique ID generation
      Distributed systems
      HTTP API
    Two modes
      Segment MySQL batches
      Snowflake timestamp IDs
    Performance
      50k requests per second
      Sub-millisecond P99
      No single-point failure
    Tech stack
      Java
      Spring Boot
      MySQL
      ZooKeeper
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Deploy as a central ID service so multiple backend servers can generate unique record IDs without database conflicts.

USE CASE 2

Use segment-based mode to reduce database write load in a high-traffic application.

USE CASE 3

Use Snowflake mode to generate timestamp-ordered unique IDs without any database dependency.

USE CASE 4

Embed the core library directly into an RPC service for maximum throughput beyond the HTTP API.

Tech stack

JavaSpring BootMySQLZooKeeperMaven

Getting it running

Difficulty · hard Time to first run · 1h+

Segment mode requires a MySQL database with a specific schema, Snowflake mode requires ZooKeeper for server coordination.

In plain English

Leaf is a Java service from Meituan-Dianping, a large Chinese food delivery and services company, that generates unique ID numbers for use across distributed systems. The name comes from a Leibnitz quote about snowflakes and leaves, and it addresses a specific problem: when many servers in a large application all need to create unique record identifiers at the same time, standard approaches like auto-incrementing database IDs break down because there is no single place to coordinate. Leaf solves this by acting as a central ID-issuing service that all other services can call. The service runs as an HTTP server built on Spring Boot. Any application that needs a new unique ID makes an HTTP request to Leaf and gets one back. The README mentions the system handles nearly 50,000 requests per second on a mid-range server, with response times under 1 millisecond for the 99.9th percentile, and that it covers multiple business lines within Meituan including payments, food delivery, and hotels. Leaf supports two different methods for generating IDs. The first is a segment-based approach that reserves a range of numbers in a MySQL database and hands them out in batches, reducing database load. The second is based on Twitter's Snowflake algorithm, which generates IDs by combining a timestamp, a server identifier, and a sequence number into a single 64-bit number, requiring no database but needing ZooKeeper to coordinate which server ID each instance gets. Setup involves cloning the repository, building with Maven, configuring a properties file with database or ZooKeeper connection details, and starting the server. The README also describes a lower-level integration path where you include the core library directly in an RPC server if you need higher performance than the HTTP interface provides. The project is open-source. The README is available in both Chinese and English.

Copy-paste prompts

Prompt 1
Help me set up Meituan Leaf in segment mode with a MySQL database using Maven and Spring Boot, including the required database schema.
Prompt 2
Using Leaf's Snowflake mode, show me how to configure ZooKeeper so two Leaf server instances never generate conflicting IDs.
Prompt 3
Show me how to call the Leaf HTTP API from a Java Spring Boot service to get a unique ID for each new database record I create.
Prompt 4
Help me tune Leaf's segment size so the database is queried less often in a system that generates around 10,000 IDs per minute.
Open on GitHub → Explain another repo

← meituan-dianping on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.