SREERAJ.DEV // PERSONAL SITE EDITION 2026-08 · BRUTALIST --:--:-- UTC SCR 000%
SREERAJ.DEV/

Backend engineering & Himalayan treks — plain HTML, served statically

Benchmarking UUID v4 vs ULID

Benchmarking UUID against ULID.

date
2023-02-22
updated
2023-03-28
author
Sreeraj Rajan
read time
1 min
tags
[postgres][uuid][ulid]
Benchmarking UUID v4 vs ULID
FIG. 01 — Benchmarking UUID v4 vs ULID

tl;dr

ULID offers better performance compared to UUIDs (v4) since ULIDs are monotonic. Another advantage is ULID values can be stored in the UUID column type that postgres offers.

ULID Generation

CREATE EXTENSION IF NOT EXISTS pgcrypto;

CREATE OR REPLACE FUNCTION generate_ulid() RETURNS uuid
    AS $$
        SELECT (lpad(to_hex(floor(extract(epoch FROM clock_timestamp()) * 1000)::bigint), 12, '0') || encode(gen_random_bytes(10), 'hex'))::uuid;
    $$ LANGUAGE SQL;

SELECT generate_ulid();

Benchmarks

UUID (mean = 0.4531, median = 0.48)

ULID (mean = 0.0708, median = 0.037)