Universally Unique Lexicographically Sortable Identifier implementation for Elixir
UUID can be suboptimal for many uses-cases because:
- It isn't the most character efficient way of encoding 128 bits of randomness
- The string format itself is apparently based on the original MAC & time version (UUIDv1 from Wikipedia)
- It provides no other information than randomness
Instead, herein is proposed ULID:
- 128-bit compatibility with UUID
- 1.21e+24 unique ULIDs per millisecond
- Lexicographically sortable!
- Canonically encoded as a 26 character string, as opposed to the 36 character UUID
- Uses Crockford's base32 for better efficiency and readability (5 bits per character)
- Case insensitive
- No special characters (URL safe)
If available in Hex, the package can be installed as:
- Add
ulid
to your list of dependencies inmix.exs
:
def deps do
[{:ulid, "~> 0.1.0"}]
end
- Ensure
ulid
is started before your application:
def application do
[applications: [:ulid]]
end
Ulids can be generated with a simple call.
iex> Ulid.generate()
"01ARZ3NDEKTSV4RRFFQ69G5FAV"
One can also input a seed time which will consistently give the same time component. This is useful for migrating to ulid.
iex> {:ok, dt} = DateTime.from_naive(~N[2018-01-01 00:00:00], "Etc/UTC")
iex> timestamp = DateTime.to_unix(dt, :millisecond)
iex> Ulid.generate(timestamp)
"01C2QG9400RY29DZPBYFNM93FJ"
Generating raw binary Ulids is also possible.
iex> Ulid.generate_binary()
<<1, 98, 114, 66, 176, 35, 120, 165, 246, 238, 101, 132, 56, 100, 63, 177>>
iex> {:ok, dt} = DateTime.from_naive(~N[2018-01-01 00:00:00], "Etc/UTC")
iex> timestamp = DateTime.to_unix(dt, :millisecond)
iex> Ulid.generate_binary(timestamp)
<<1, 96, 175, 4, 144, 0, 76, 227, 163, 150, 142, 231, 2, 152, 139, 26>>
Sometimes it's easier to deal with the UUID format. This library provides conversion for raw and encoded Ulids to and from UUID, as well as generation.
iex> Ulid.generate_uuid()
"0162727c-69cb-822b-2d7b-46425d5bb45d"
iex> Ulid.to_uuid(<<1, 99, 150, 178, 200, 245, 145, 16, 174, 235, 231, 88, 83, 10, 188, 83>>)
"016396b2-c8f5-9110-aeeb-e758530abc53"
iex> Ulid.to_uuid("01CEBB5J7NJ48AXTZ7B19GNF2K")
"016396b2-c8f5-9110-aeeb-e758530abc53"
iex> Ulid.from_uuid("016396b2-c8f5-9110-aeeb-e758530abc53")
"01CEBB5J7NJ48AXTZ7B19GNF2K"
It is possible to extract the timestamp from Ulids as well.
iex> Ulid.extract_timestamp(<<1, 86, 61, 243, 100, 129, 149, 125, 206, 44, 55, 150, 198, 186, 71, 79>>)
1469918176385
iex> Ulid.extract_timestamp("01ARYZ6S4124TJP2BQQZX06FKM")
1469918176385
iex> Ulid.extract_timestamp("01563df3-6481-1135-2b09-77bffa033e74")
1469918176385
Below is the current specification of ULID as implemented in this repository.
01AN4Z07BY 79KA1307SR9X4MV3
|----------| |----------------|
Timestamp Randomness
10 chars 16 chars
48bits 80bits
base32 base32
Timestamp
- 48 bit integer
- UNIX-time in milliseconds
- Won't run out of space till the year 10895 AD.
Randomness
- 80 bits
- Cryptographically secure source of randomness, if possible
The left-most character must be sorted first, and the right-most character sorted last. The default ASCII order is used for sorting.
The components are encoded as 16 octets. Each component is encoded with the Most Significant Byte first (network byte order).
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 32_bit_uint_time_high |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 16_bit_uint_time_low | 16_bit_uint_random |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 32_bit_uint_random |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 32_bit_uint_random |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
ttttttttttrrrrrrrrrrrrrrrr
where
t is Timestamp
r is Randomness
mix test
Encoding
## UlidBench
benchmark name iterations average time
generate_binary 1000000 1.03 µs/op
encode 1000000 1.69 µs/op
decode 1000000 1.70 µs/op
generate_uuid 1000000 2.01 µs/op
generate 1000000 2.86 µs/op