Aphora
Aphora is a system do distributedly generate unique id without any coordination. It is similiar to Snowflake, but instead of an integer it generates ASCII sortable a String.
It uses a 72 Bit long Binary which get encoded to 12 characters long ID.
An ID is composed of:
45 BitTimestamp with millisecond precision. It gives up to 1115 years with a custom epoch.5 BitDatacenter Identifier, which allows up to32different datacenters.10 BitWorker Identifier, which allows up to1024different processes/workers/machines within a datacenter.12 BitCounter, which allows for4096unique IDs to be generated each millisecond. Aphora also protects against rollovers to happen within the same millisecond.
Just like Snowflake, Aphora protects from non-monotonic clocks, by refusing to generate timestamp until it gets provided with a time equals or after the one used in the last generated ID.
Installation
The package can be installed by adding :aphora to your list of dependencies in mix.exs:
def deps do
[
{:aphora, "~> 1.0.0"}
]
end
The config file is located at /config/config.exs. If you run Aphora in more than one process, you'll need to adjust the :worker and :datacenter.
config :aphora,
# Value between 0..31, unique between all `t:datacenter/0`.
datacenter: 0,
# Value between 0..1_024, unique between all `t:worker/0` within a `t:datacenter/0`.
worker: 0,
# Shouldn't be changed once the first `t:id/0` is generated. Default: 2019-01-01 00:00:00.000000Z in milliseconds.
epoch: 1_546_300_800_000The docs can be found at Hex.
Usage
The usage of Aphora is quite straightforward after setting up the config.exs correctly, as all important methods are within the Aphora module.
To generate a new ID, you use Aphora.new_id/0.
Aphora.new_id()
# {:ok, "0eHHz1--abHr"}
And if you want to find out when a ID was generated, you use Aphora.to_timestamp/2.
Aphora.to_timestamp("0eHHz1--abHr", :relative)
# 912988800000License
Aphora is licensed under the Apache License 2.0.