XID Generator

Generate Multiple XIDs

Number of IDs:

What is XID?

XID (eXtended ID) is a globally unique identifier that’s shorter than UUID while still maintaining uniqueness. XIDs are 12 bytes (compared to UUID’s 16 bytes) and are typically represented as a 20-character base32 string. They’re optimized for use in distributed systems and databases where you need a compact, URL-safe identifier.

Benefits of XIDs:

  • Shorter: 20 characters vs 36 for UUID
  • URL-safe: Uses base32 encoding (no special characters)
  • Sortable: Contains a timestamp component which makes them roughly time-sortable
  • Efficient: Optimized for databases and distributed systems

XID 12-Byte Binary Layout

Inspired by MongoDB’s ObjectID, an XID packs 96 bits (12 bytes) into a 20-character base32hex string (0-9, a-v) that preserves chronological sorting even as plain text:

Bytes Size Purpose
0..3 4 bytes (32 bits) Unix timestamp in seconds (gives roughly chronological sort order)
4..6 3 bytes (24 bits) Machine identifier (derived from host/container ID)
7..8 2 bytes (16 bits) Process ID (PID), which keeps processes on one host apart (only its low 16 bits fit, so it isn’t a hard guarantee)
9..11 3 bytes (24 bits) Counter. In the reference Go library (rs/xid) it is process-wide, starts at a random value and wraps after 16,777,216; IDs from this page’s service don’t necessarily count up one by one

XID vs. UUIDv4 vs. UUIDv7 vs. ULID vs. NanoID

Format Length (String) Size (Binary) Time-Sortable? Best Use Case
XID 20 chars 12 bytes Yes (1s precision) Compact, URL-safe database primary keys & distributed logs
UUIDv4 36 chars 16 bytes No (random) General standard IDs where B-Tree locality doesn’t matter
UUIDv7 36 chars 16 bytes Yes (1ms precision) Modern SQL databases with native uuid columns
ULID 26 chars 16 bytes Yes (1ms precision) Lexicographically sortable 128-bit IDs
NanoID 21 chars (default) Variable No (random) Short, opaque URL slugs & frontend tokens

Because XIDs start with a timestamp, inserting them into a B-Tree primary key appends sequentially instead of fragmenting random disk pages (see Database Indexing).

FAQs

Are XIDs cryptographically secure for session tokens or passwords? No. XIDs embed a predictable Unix timestamp, machine hash, PID, and counter — someone can guess nearby IDs. Use XIDs for primary keys, request IDs, and event IDs; use cryptographically random IDs like NanoID or 256-bit random tokens for secrets and session keys.
Can two servers generate the same XID at the exact same second? Collision risk is low in practice because, besides the timestamp, each ID carries a 3-byte machine ID, a 2-byte PID and a 3-byte counter (in rs/xid, process-wide, starting at a random value and wrapping after 16,777,216 IDs), without needing any central coordinator. Two IDs only collide if all four parts match.
Why is XID 20 characters instead of MongoDB ObjectID’s 24 characters? Both use 12 bytes internally, but MongoDB ObjectID encodes in hex (4 bits/char → 24 chars) while XID uses base32hex (5 bits/char → 20 chars), making XID shorter while preserving strict lexicographical sort order.

More Free Developer Tools