XCUID

eliXir CUID.

Collision-resistant ids optimized for horizontal scaling and binary search lookup performance.

This project is a more modern and optimized implementation compare to duailibe/cuid which is no longer maintained AFAIK.

Usage

Add xcuid to your list of dependencies in mix.exs:

def deps do
  [
    {:xcuid, "~> 0.1.0"}
  ]
end

Run mix deps.get, then generate a CUID is simple:

XCUID.generate() # => cka3hpvst29dw1m12hzwblxd5

If you want to start multiple generator you can do

{:ok, pid} = XCUID.start(name: :generator_1)
XCUID.generate(pid) #=> cka3hpvst29dw1m12hzwblxd5

Why not UUID v4?

Refer to the original project for the original motivation of CUID. Personally I use this instead of UUID v4 because:

One disadvantage of CUID is that it take more space, UUID is 16 bytes if stored as hexdecimal, where as CUID is at least 25 bytes. However you can could save some space by not storing an extra inserted_at column because that is already included in CUID. If we subtract that 8 byte for timestamp then technically it is just 1 byte more than UUID which is a reasonable price to pay for getting the above advantages.

Performance

I have optimized the generation code to the best of my knowledge. Currently it can generate ~250k CUIDs per second on my MBP (Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz). If you want even better performance you can pre-generate them in batches.

Breakdown

c - h72gsb32 - 0000 - udoc - l363eofy

The groups, in order, are:

Others

This issue in the original javascript version is fixed in XCUID by changing all CUID to start with d instead of c after the roll over time which is 2_821_109_907_456 in unix timestamp.

Credits