ULID Generator

लेक्सिकोग्राफिकली सॉर्टेबल यूनिक IDs जनरेट करें

संख्या

ULIDs बनाने के लिए जनरेट पर क्लिक करें

ULID क्या है?

ULID (Universally Unique Lexicographically Sortable Identifier) एक 128-bit identifier format है जो unique और naturally sortable दोनों होने के लिए design किया गया है। UUID v4 के विपरीत, जो entirely random है, ULID अपने high bits में millisecond-precision Unix timestamp embed करता है — ensuring कि बाद में generate किए गए IDs पहले generate किए गए IDs के बाद sort हों।

ULIDs Crockford's Base32 alphabet use करके encode किए जाते हैं, एक compact 26-character string produce करते हैं जो URL-safe, case-insensitive, और visually ambiguous characters से free है।

ULID specification Alizain Feerasta द्वारा बनाई गई थी और distributed systems, event sourcing, और database primary keys में widely use की जाती है। ULIDs IETF standard नहीं हैं — वे ulid.github.io पर available community specification हैं।

ULID Structure

ULID 128 bits wide है, दो components में divided:

TimestampRandom
01ARZ3NDEKTSVE4RRFFQ69G5FAV
48 bits — Unix timestamp in milliseconds। 1ms precision के साथ generation time encode करता है। Year 10889 तक dates cover करता है।80 bits — cryptographically secure random data। हर ULID के लिए fresh regenerated (जब तक monotonicity mode use न हो)।

26 Crockford Base32 characters के रूप में Encoded: पहले 10 characters timestamp represent करते हैं, अंतिम 16 characters random component represent करते हैं।

Crockford Base32 Alphabet

ULIDs Crockford's Base32 encoding use करते हैं, 36 alphanumeric characters में से 32 use करते हैं। Alphabet है: 0123456789ABCDEFGHJKMNPQRSTVWXYZ

ये 32 characters क्यों?
0123456789ABCDEFGHJKMNPQRSTVWXYZ

चार characters intentionally excluded हैं:

  • I और L excluded हैं — digit 1 के साथ easily confused
  • O excluded है — digit 0 के साथ easily confused
  • U excluded है — generated IDs में accidental obscene words से बचाता है
  • Encoding case-insensitive है — 01ARZ3NDEKTSV4RRFFQ69G5FAV और 01arz3ndektsv4rrffq69g5fav same ULID हैं

Crockford Base32 hexadecimal से अधिक efficient है (32 symbols vs 16) और Base64 से अधिक human-readable है (कोई + / = characters नहीं, case-insensitive)।

ULID vs UUID

ULIDs और UUIDs दोनों 128-bit identifiers represent करते हैं, लेकिन encoding और design goals में significantly differ करते हैं:

PropertyULIDUUID
FormatCrockford Base32Hyphenated hex
Length26 characters36 characters
Timestamp48-bit Unix msकोई नहीं (v4) या 48-bit ms (v7)
Sortableहाँ — lexicographicनहीं (v4) / हाँ (v7)
URL-safeहाँ (alphanumeric only)हाँ (with hyphens)
DependenciesLibrary requiredNative (crypto.randomUUID)
DB supportCHAR(26) या BINARY(16) के रूप में Store करेंNative UUID column type
SpecCommunity spec (ulid.github.io)RFC 4122 / RFC 9562

यदि आप already UUID ecosystem में हैं (PostgreSQL uuid columns, REST APIs, ORMs with UUID support), UUID v7 ULID से usually better fit है। यदि आप fresh start कर रहे हैं और human-friendly encoding prefer करते हैं, ULID एक excellent choice है।

ULID vs nanoid

ULID और nanoid दोनों short, URL-safe identifiers produce करते हैं, लेकिन different design goals हैं:

PropertyULIDNanoID
Timestampहाँ — 48-bit Unix msनहीं
Sortableहाँनहीं
Default length26 characters21 characters
Entropy80 bits (random component)~126 bits
AlphabetCrockford Base32 (32 chars)URL-safe Base64 (64 chars)
Customizable lengthनहींहाँ (any length)
Use caseTime-ordered IDs, DB primary keysRandom tokens, short URLs, API keys

ULID चुनें जब time-ordering चाहिए। nanoid चुनें जब short, random string में maximum entropy चाहिए।

Databases में ULIDs का उपयोग

ULIDs को databases में कई तरीकों से store किया जा सकता है:

CHAR(26) के रूप में Store करें
Simplest approach। Lexicographic sort order preserved है। Binary storage से slightly larger लेकिन human-readable और debug करना easy।
BINARY(16) के रूप में Store करें
Compact storage के लिए ULID को 16-byte binary representation में decode करें। Application code में encoding/decoding require करता है। Sort order preserved है।
PostgreSQL
CHAR(26) के रूप में Store करें या binary storage के लिए bytea type use करें। कोई native ULID type नहीं है, लेकिन CHAR(26) पर lexicographic ordering correctly काम करता है।
MySQL / MariaDB
Efficient storage के लिए CHAR(26) CHARACTER SET ascii use करें। BINARY(16) में Binary storage भी काम करती है और space बचाती है।
SQLite
TEXT के रूप में Store करें। SQLite का default text comparison ULIDs को correctly sort करता है उनके lexicographic design के कारण।
MongoDB
String field के रूप में Store करें। ULID की lexicographic sortability MongoDB के string comparison के साथ naturally काम करती है।

Code Examples

ULID generation के लिए ulid library required है (JavaScript, Python, Go, Rust, और अधिक के लिए available):

JavaScript (ulid npm)
// Using the 'ulid' npm package
import { ulid } from 'ulid'

const id = ulid()          // "01ARZ3NDEKTSV4RRFFQ69G5FAV"
const id2 = ulid()         // "01ARZ3NDEKXXXXXXXXXXXX..." (same ms, incremented random)

// With a custom timestamp
const id3 = ulid(1469918176385) // deterministic time portion

// Extract the timestamp back out
import { decodeTime } from 'ulid'
decodeTime(id)  // → 1469918176385 (Unix ms)
Python (python-ulid)
# Using python-ulid
from ulid import ULID

uid = ULID()
str(uid)                    # "01ARZ3NDEKTSV4RRFFQ69G5FAV"
uid.timestamp               # 1469918176.385
uid.datetime                # datetime(2016, 7, 30, 23, 36, 16, 385000, tzinfo=timezone.utc)

# Parse an existing ULID string
parsed = ULID.from_str("01ARZ3NDEKTSV4RRFFQ69G5FAV")
parsed.timestamp            # 1469918176.385
PostgreSQL
-- Store ULIDs as TEXT or CHAR(26)
CREATE TABLE events (
  id   CHAR(26) PRIMARY KEY DEFAULT gen_ulid(),  -- if using a PG extension
  name TEXT NOT NULL,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Or pass the ULID from your application layer
INSERT INTO events (id, name, created_at)
VALUES ('01ARZ3NDEKTSV4RRFFQ69G5FAV', 'user.signup', NOW());

-- Range queries are efficient because ULIDs sort chronologically
SELECT * FROM events
WHERE id > '01ARZ3NDEKTSV4RRFFQ69G5FAV'
ORDER BY id
LIMIT 20;
JavaScript (pure — no dependencies)
// Pure browser / Deno / Node.js implementation (no dependencies)
const CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'

function encodeTime(ms) {
  let result = '', n = BigInt(ms)
  for (let i = 9; i >= 0; i--) {
    result = CROCKFORD[Number(n & 31n)] + result
    n >>= 5n
  }
  return result
}

function encodeRandom(bytes) {
  let n = 0n
  for (const b of bytes) n = (n << 8n) | BigInt(b)
  let result = ''
  for (let i = 15; i >= 0; i--) {
    result = CROCKFORD[Number(n & 31n)] + result
    n >>= 5n
  }
  return result
}

function generateULID() {
  const randomBytes = new Uint8Array(10)
  crypto.getRandomValues(randomBytes)
  return encodeTime(Date.now()) + encodeRandom(randomBytes)
}

अक्सर पूछे जाने वाले प्रश्न

क्या ULIDs globally unique हैं?
हाँ — बहुत high probability के साथ। 80-bit random component प्रति millisecond प्रति generator 2^80 ≈ 1.2 × 10^24 possible values provide करता है। Strict uniqueness guarantees के लिए, monotonic mode use करें।
क्या ULID UUID के समान है?
नहीं — ULIDs और UUIDs 128-bit value के different encodings हैं। UUID format (hyphenated hex) validate करने वाले systems में ULID directly substitute नहीं किया जा सकता।
ULID monotonic mode कैसे काम करता है?
Standard mode में, 80-bit random component हर ULID के लिए freshly generated होता है। Monotonic mode में, जब same millisecond में multiple ULIDs generate होते हैं, random component previous value plus one होता है।
क्या मैं generation timestamp पाने के लिए ULID decode कर सकता हूँ?
हाँ। पहले 10 Crockford Base32 characters 48-bit Unix millisecond timestamp encode करते हैं। यह tool exactly वही करता है।
क्या ULID official standard है?
नहीं। ULID ulid.github.io पर maintained community specification है। UUID v7 (RFC 9562, 2024) similar time-ordering properties के साथ IETF-standardised alternative है।