Home

Awesome

ulid

Universally Unique Lexicographically Sortable Identifier implementation for Elixir

<h1 align="center"> <br> <br> <img width="360" src="logo.png" alt="ulid"> <br> <br> <br> </h1>

GitHub License

Universally Unique Lexicographically Sortable Identifier

UUID can be suboptimal for many uses-cases because:

Instead, herein is proposed ULID:

Installation

If available in Hex, the package can be installed as:

  1. Add ulid to your list of dependencies in mix.exs:
def deps do
  [{:ulid, "~> 0.1.0"}]
end
  1. Ensure ulid is started before your application:
def application do
  [applications: [:ulid]]
end

Usage

Ulids can be generated with a simple call.

iex> Ulid.generate()
"01ARZ3NDEKTSV4RRFFQ69G5FAV"

Seed Time

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"

Binary

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>>

UUID

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"

Extracting timestamp

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

Specification

Below is the current specification of ULID as implemented in this repository.

 01AN4Z07BY      79KA1307SR9X4MV3

|----------|    |----------------|
 Timestamp          Randomness
  10 chars           16 chars
   48bits             80bits
   base32             base32

Components

Timestamp

Randomness

Sorting

The left-most character must be sorted first, and the right-most character sorted last. The default ASCII order is used for sorting.

Binary Layout and Byte Order

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                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

String Representation

ttttttttttrrrrrrrrrrrrrrrr

where
t is Timestamp
r is Randomness

Test Suite

mix test

Performance

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

Credits and references: