{"id":19486496,"url":"https://github.com/phxql/snowflake-id","last_synced_at":"2025-05-09T01:23:41.511Z","repository":{"id":44359214,"uuid":"303357831","full_name":"phxql/snowflake-id","owner":"phxql","description":"Generates Twitter-like Snowflake ids in Java","archived":false,"fork":false,"pushed_at":"2025-04-29T10:44:14.000Z","size":335,"stargazers_count":46,"open_issues_count":0,"forks_count":7,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-29T11:34:53.171Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phxql.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-10-12T10:34:01.000Z","updated_at":"2025-04-29T10:43:27.000Z","dependencies_parsed_at":"2023-02-14T00:45:15.631Z","dependency_job_id":"c1ae2870-bf80-46ef-b30b-71591a6aa584","html_url":"https://github.com/phxql/snowflake-id","commit_stats":{"total_commits":116,"total_committers":4,"mean_commits":29.0,"dds":"0.39655172413793105","last_synced_commit":"d342f01e4f40a956003006378fe48a0be5b406ab"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phxql%2Fsnowflake-id","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phxql%2Fsnowflake-id/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phxql%2Fsnowflake-id/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phxql%2Fsnowflake-id/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phxql","download_url":"https://codeload.github.com/phxql/snowflake-id/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253172251,"owners_count":21865484,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T20:38:00.395Z","updated_at":"2025-05-09T01:23:41.488Z","avatar_url":"https://github.com/phxql.png","language":"Java","funding_links":[],"categories":["分布式开发"],"sub_categories":[],"readme":"# SnowflakeId\n\n[![Java CI](https://github.com/phxql/snowflake-id/actions/workflows/build.yaml/badge.svg)](https://github.com/phxql/snowflake-id/actions/workflows/build.yaml)\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/de.mkammerer.snowflake-id/snowflake-id/badge.svg)](https://maven-badges.herokuapp.com/maven-central/de.mkammerer.snowflake-id/snowflake-id)\n\nGenerates Twitter-like Snowflake ids.\nIn short, this is an id scheme to generate unique 64 bit ids which are roughly sortable across multiple systems without\na central\ninstance. [See this blog post for more details](https://blog.twitter.com/engineering/en_us/a/2010/announcing-snowflake.html).\n\nThis project was heavily inspired by [IdGen](https://github.com/RobThree/IdGen) for C#, there's a\ngreat [How it works](https://github.com/RobThree/IdGen#how-it-works) in the readme, too.\n\nThe algorithm is implemented in plain Java without any dependencies. All you need is at least Java 11.\n\nSuch generated id in binary looks like this (this is 4425020822061056 in decimal):\n\n```\n0000000000001111101110001000100001110010001110010000000000000000\n||                                            | |\n|| Timestamp (16880114830)                    | | Sequence (0)\n|                                             |\n| Sign bit (always 0)                         | Generator id (1)\n```\n\nThe structure used for this is 45 bits for the timestamp, 2 for the generator and the remaining 16 for the sequence. This structure can be changed easily, see below for the code. \n\nThe ids really use only 63 bits of the 64 available to circumvent problems with unsigned longs. The generated values are always positive.\nIds from the same generator are monotonically increasing.\n\n## How to use\n\n### Maven\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ede.mkammerer.snowflake-id\u003c/groupId\u003e\n    \u003cartifactId\u003esnowflake-id\u003c/artifactId\u003e\n    \u003cversion\u003e0.0.2\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Gradle\n\n```gradle\nimplementation 'de.mkammerer.snowflake-id:snowflake-id:0.0.2'\n```\n\n### Simple example\n\n```java\n// generatorId must be unique over all your instances!\nint generatorId = 0; \n\n// use default generator settings\nSnowflakeIdGenerator generator = SnowflakeIdGenerator.createDefault(generatorId);\n\n// generate 10 ids\nfor (int i = 0; i \u003c 10; i++) {\n    long id = generator.next();\n    System.out.println(id);\n}\n```\n\n### More configuration options\n\n```java\n// Use a custom epoch\nTimeSource timeSource = new MonotonicTimeSource(Instant.parse(\"2020-04-01T00:00:00Z\"));\n// Use 45 bits for the timestamp, 2 bits for the generator and 16 bits for the sequence\nStructure structure = new Structure(45, 2, 16);\n// If the sequence number overflows, throw an exception\nOptions options = new Options(Options.SequenceOverflowStrategy.THROW_EXCEPTION);\n\n// generatorId must be unique over all your instances!\nlong generatorId = 1;\nSnowflakeIdGenerator generator = SnowflakeIdGenerator.createCustom(generatorId, timeSource, structure, options);\n\n// generate 10 ids\nfor (int i = 0; i \u003c 10; i++) {\n    long id = generator.next();\n    System.out.println(id);\n}\n```\n\n### Calculate maximum timestamps, generators, sequence ids and wraparound dates\n\nYou can query the `Structure` class to find out the maximum numbers of timestamps, generators, sequence ids and wraparound dates:\n\n```java\nTimeSource timeSource = new MonotonicTimeSource(Instant.parse(\"2020-04-01T00:00:00Z\"));\n\nStructure structure = new Structure(45, 2, 16);\nSystem.out.println(\"Max generators: \" + structure.maxGenerators());\nSystem.out.println(\"Max sequences per ms per generator: \" + structure.maxSequenceIds());\nSystem.out.println(\"Max sequences per ms total: \" + structure.maxSequenceIds() * structure.maxGenerators());\nSystem.out.println(\"Wraparound duration: \" + structure.calculateWraparoundDuration(timeSource));\nSystem.out.println(\"Wraparound date: \" + structure.calculateWraparoundDate(timeSource));\n```\n\nThis prints:\n\n```\nMax generators: 4\nMax sequences per ms per generator: 65536\nMax sequences per ms total: 262144\nWraparound duration: PT9773436H41M28.832S\nWraparound date: 3135-03-14T12:41:28.832Z\n```\n\n### Default settings\n\nThe default settings for the `Structure` are 41 bits for the timestamp, 10 for the generator id and 12 for the sequence.\n\nThis will lead to the following properties:\n\n```\nMax generators: 1024\nMax sequences per ms per generator: 4096\nMax sequences per ms total: 4194304\nWraparound duration: PT610839H47M35.552S\nWraparound date: 2089-09-06T15:47:35.552Z\n```\n\n## Building from source\n\n[See here](docs/building.md).\n\n## Changelog?\n\n[See here](CHANGELOG.md).\n\n## License\n\n[LGPLv3](https://www.gnu.org/licenses/lgpl-3.0.html)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphxql%2Fsnowflake-id","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphxql%2Fsnowflake-id","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphxql%2Fsnowflake-id/lists"}