Meson

Distributed ID generator inspired by BSON's ObjectID and Snowflake

License

License

GroupId

GroupId

com.rfksystems
ArtifactId

ArtifactId

meson
Last Version

Last Version

1.0.3
Release Date

Release Date

Type

Type

jar
Description

Description

Meson
Distributed ID generator inspired by BSON's ObjectID and Snowflake
Project URL

Project URL

https://github.com/rfksystems/meson
Project Organization

Project Organization

RFK Systems
Source Code Management

Source Code Management

http://github.com/rfksystems/meson/tree/master

Download meson

How to add to project

<!-- https://jarcasting.com/artifacts/com.rfksystems/meson/ -->
<dependency>
    <groupId>com.rfksystems</groupId>
    <artifactId>meson</artifactId>
    <version>1.0.3</version>
</dependency>
// https://jarcasting.com/artifacts/com.rfksystems/meson/
implementation 'com.rfksystems:meson:1.0.3'
// https://jarcasting.com/artifacts/com.rfksystems/meson/
implementation ("com.rfksystems:meson:1.0.3")
'com.rfksystems:meson:jar:1.0.3'
<dependency org="com.rfksystems" name="meson" rev="1.0.3">
  <artifact name="meson" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.rfksystems', module='meson', version='1.0.3')
)
libraryDependencies += "com.rfksystems" % "meson" % "1.0.3"
[com.rfksystems/meson "1.0.3"]

Dependencies

compile (1)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind jar 2.9.5

test (3)

Group / Artifact Type Version
org.mongodb : bson jar 3.6.3
com.google.truth : truth jar 0.40
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

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

Maven Central

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

  1. 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.
  2. 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.
  3. 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
  4. Linked to Jackson's DataBind by default (at least for now), so serialization/deserialization to JSON and back should be trivial.

See:

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

com.rfksystems

RFK Systems

Versions

Version
1.0.3
1.0.2
1.0.1
1.0.0