CosId Universal, flexible, high-performance distributed ID generator
Every replica of a service needs IDs (primary keys, order numbers, event ids) that never collide with those of the other replicas. CosId generates them inside your process, without a network call per ID, and does the coordination that keeps them unique: it leases a distinct machine id to every Snowflake instance from a store you already run, takes ranges of segment IDs ahead of time, and handles clocks that move backwards.
| Generator | ID | Throughput on one instance (historical JMH) | Pick it when |
|---|---|---|---|
SegmentChainId |
long, near-sequential |
127,439,148 ops/s | You want short numeric keys or business numbers. The default segment mode |
SnowflakeId |
long, time-ordered |
4,096,000 ops/s (theoretical maximum) | IDs should carry their creation time, with no store call after startup |
CosIdGenerator |
String, 15 characters, time-ordered |
— | You run very many instances (up to a million machine ids) |
Stores: Redis, JDBC (MySQL, MariaDB, PostgreSQL), MongoDB, ZooKeeper, the CosId proxy, or your own. Integrations: Spring Boot, MyBatis, Spring Data JDBC, Jackson, ShardingSphere, Activiti, Flowable and Axon.
| CosId | Spring Boot | Java | Branch |
|---|---|---|---|
| 4.x | 4.x | 17+ | main |
| 3.x | 4.x | 17+ | 3.x (maintenance) |
| 2.x | 3.x | 17+ | 2.x |
| 1.x | 2.x | 8+ | 1.x |
Upgrading from 3.x: see MIGRATION-4.0.md.
dependencies {
implementation(platform("me.ahoo.cosid:cosid-bom:$cosidVersion"))
implementation("me.ahoo.cosid:cosid-spring-boot-starter")
implementation("me.ahoo.cosid:cosid-spring-redis") // or cosid-jdbc, cosid-mongo, cosid-zookeeper, cosid-proxy
implementation("org.springframework.boot:spring-boot-starter-data-redis")
}cosid:
namespace: ${spring.application.name}
segment:
enabled: true
distributor:
type: redis
provider:
order:
step: 100long orderId = idGeneratorProvider.getRequired("order", IdGenerator.class).generate();The Getting Started guide walks through it, including Snowflake IDs and Maven.
- Why CosId: the problem and the trade-offs between the generators.
- Core concepts: generators, machine ids, segments and converters.
- Choosing a backend: Redis, JDBC, MongoDB, ZooKeeper and the proxy.
- Configuration reference: every property and its default.
- Operations: production checklist, rolling upgrades, monitoring and troubleshooting.
- Migrating to 4.0, and the full migration guide.
The examples are runnable applications for Redis, ZooKeeper, the proxy and CosIdGenerator.
Historical JMH benchmarks on one development notebook (MacBook Pro M1, Redis on the same machine):
SegmentChainId is about five times as fast as Meituan Leaf's segment mode, see CosId VS Leaf.
Bug reports, ideas and pull requests are welcome. Read CONTRIBUTING.md for the development setup, commit conventions, review and release process, and SECURITY.md to report a vulnerability privately. The planned work is in the roadmap.


