Meson
Distributed ID generator inspired by BSON's ObjectID and Snowflake
In particle physics, mesons (/ˈmiːzɒnz/ or /ˈmɛzɒnz/) are hadronic subatomic particles composed of one quark and one antiquark, bound together by strong interactions. - Wikipedia
Reference implementation in Java. API documentation is available here
ID composition:
48 bits | 6 bytes: Unix-time in millis as 48 bit unsigned integer
32 bits | 4 bytes: Generator identifier (hash)
32 bits | 4 bytes: Generator-instance incremental sequence number (32 bit unsigned integer)
______________________________________________________________________________________________
Total 112 bits, or 14 bytes, or, 28 or 30 characters long, depending on the format
Implementation details
- Timestamps are expressed as 48 bit unsigned integers with max. value of
281474976710655
. This is well beyond what would ordinarily be needed to store timestamps. The topmost timestamp you could store in a 48 bit int is August 2nd year 10889. - Timestamps have millisecond precession and the identifier uses Big-Endian byte order. This makes Meson naturally sortable by time and in sequence within the same instance. Sorting order beyond millis for ID's generated in different instances is not a defined behavior, however, given the millisecond precession of the timestamps, this should almost never present a real problem.
- Generator identifier is a CRC32 of parts defined bellow, and makes a collision between hosts of the same ORG pretty near improbable:
/proc/1/cgroup
contents if the file is present- Hostname of the "127.0.0.1" address
- Process ID (PID)
- Every MAC address in the system
- Every public IPv6/IPv4 address in the system
- Linked to Jackson's DataBind by default (at least for now), so serialization/deserialization to JSON and back should be trivial.
See:
- https://stackoverflow.com/a/42947044/1653859
- https://github.com/moby/moby/issues/22174
- https://tuhrig.de/how-to-know-you-are-inside-a-docker-container/
- https://docs.mongodb.com/manual/reference/method/ObjectId/
Requirements
Java 8+.
Installation
Meson is available in Maven Central. Adding dependency definition to your build config should do the trick.
Maven
<dependency>
<groupId>com.rfksystems</groupId>
<artifactId>meson</artifactId>
<version>VERSION</version>
</dependency>
Gradle
compile group: 'com.rfksystems', name: 'meson', version: 'VERSION'
Usage
Either create new instance of com.rfksystems.meson.Meson
with appropriate or no parameters, or call one of com.rfksystems.meson.Meson#directTo*
methods to create a Meson identifier directly without creating a Meson object. See API documentation for more on what methods are available.
Performance
Meson is on par or quicker than BSON's ObjectID in the limited number of tests I have performed. You should be comfortably able to generate 10s or even 100s of millions of id's on modest server hardware, per second, per core.
Sample ID's
This should give an example on how the hex formatted Meson ID's look like.
Compact HEX string (prefered format)
0162915be2e1900035c91a2a5d33
0162915be2e1900035c91a2a5d34
0162915be2e1900035c91a2a5d35
0162915be2e1900035c91a2a5d36
0162915be2e1900035c91a2a5d37
0162915be2e1900035c91a2a5d38
0162915be2e1900035c91a2a5d39
0162915be2e1900035c91a2a5d3a
0162915be2e1900035c91a2a5d3b
0162915be2e1900035c91a2a5d3c
Format HEX string (display ID's)
0162915be2da-900035c9-1a2a5d29
0162915be2e1-900035c9-1a2a5d2a
0162915be2e1-900035c9-1a2a5d2b
0162915be2e1-900035c9-1a2a5d2c
0162915be2e1-900035c9-1a2a5d2d
0162915be2e1-900035c9-1a2a5d2e
0162915be2e1-900035c9-1a2a5d2f
0162915be2e1-900035c9-1a2a5d30
0162915be2e1-900035c9-1a2a5d31
0162915be2e1-900035c9-1a2a5d32
Byte values from Java's byte[]
output
[1, 98, -111, 91, -30, -31, -112, 0, 53, -55, 26, 42, 93, 61]
[1, 98, -111, 91, -30, -31, -112, 0, 53, -55, 26, 42, 93, 62]
[1, 98, -111, 91, -30, -31, -112, 0, 53, -55, 26, 42, 93, 63]
[1, 98, -111, 91, -30, -31, -112, 0, 53, -55, 26, 42, 93, 64]
[1, 98, -111, 91, -30, -31, -112, 0, 53, -55, 26, 42, 93, 65]
[1, 98, -111, 91, -30, -30, -112, 0, 53, -55, 26, 42, 93, 66]
[1, 98, -111, 91, -30, -30, -112, 0, 53, -55, 26, 42, 93, 67]
[1, 98, -111, 91, -30, -30, -112, 0, 53, -55, 26, 42, 93, 68]
[1, 98, -111, 91, -30, -30, -112, 0, 53, -55, 26, 42, 93, 69]
[1, 98, -111, 91, -30, -30, -112, 0, 53, -55, 26, 42, 93, 70]
License
Apache License, Version 2.0