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:
| Timestamp | Random |
|---|---|
| 01ARZ3NDEK | TSVE4RRFFQ69G5FAV |
| 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
चार characters intentionally excluded हैं:
IऔरLexcluded हैं — digit1के साथ easily confusedOexcluded है — digit0के साथ easily confusedUexcluded है — generated IDs में accidental obscene words से बचाता है- Encoding
case-insensitiveहै —01ARZ3NDEKTSV4RRFFQ69G5FAVऔर01arz3ndektsv4rrffq69g5favsame 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 करते हैं:
| Property | ULID | UUID |
|---|---|---|
| Format | Crockford Base32 | Hyphenated hex |
| Length | 26 characters | 36 characters |
| Timestamp | 48-bit Unix ms | कोई नहीं (v4) या 48-bit ms (v7) |
| Sortable | हाँ — lexicographic | नहीं (v4) / हाँ (v7) |
| URL-safe | हाँ (alphanumeric only) | हाँ (with hyphens) |
| Dependencies | Library required | Native (crypto.randomUUID) |
| DB support | CHAR(26) या BINARY(16) के रूप में Store करें | Native UUID column type |
| Spec | Community 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 हैं:
| Property | ULID | NanoID |
|---|---|---|
| Timestamp | हाँ — 48-bit Unix ms | नहीं |
| Sortable | हाँ | नहीं |
| Default length | 26 characters | 21 characters |
| Entropy | 80 bits (random component) | ~126 bits |
| Alphabet | Crockford Base32 (32 chars) | URL-safe Base64 (64 chars) |
| Customizable length | नहीं | हाँ (any length) |
| Use case | Time-ordered IDs, DB primary keys | Random tokens, short URLs, API keys |
ULID चुनें जब time-ordering चाहिए। nanoid चुनें जब short, random string में maximum entropy चाहिए।
Databases में ULIDs का उपयोग
ULIDs को databases में कई तरीकों से store किया जा सकता है:
Code Examples
ULID generation के लिए ulid library required है (JavaScript, Python, Go, Rust, और अधिक के लिए available):
// 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)# 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-- 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;// 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)
}