CUID Generator

कोलिजन-रेसिस्टेंट यूनिक IDs जनरेट करें (CUID v1)

संख्या

जनरेट किए गए CUIDs

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

CUID v1 एक लेगेसी फॉर्मेट है। नए प्रोजेक्ट के लिए CUID2 उपयोग करें।

CUID क्या है?

CUID (Collision-resistant Unique IDentifier) एक globally unique ID है जो horizontally scalable applications के लिए design किया गया है।

यह हमेशा c letter से शुरू होता है, जिसके बाद timestamp, counter, host fingerprint, और random segment होता है — सभी base36 में encode।

IDs URL-safe, compact, और human-readable होते हैं, जो database IDs, URLs, और session generation के लिए उपयुक्त हैं।

CUID Structure

हर CUID पाँच अलग-अलग sections से बना होता है जो machines, processes, और time के across uniqueness सुनिश्चित करते हैं:

उदाहरण:: clrc4gkwz001ag2hs3k7f9m2q
cPrefixType marker के रूप में constant 'c' character
lrc4gkwzTimestampMilliseconds में Unix time, base36 encode
001aCounterSame-millisecond collisions रोकने के लिए sequential counter
g2hsFingerprintPID और hostname से unique host fingerprint
3k7f9m2qRandomExtra uniqueness के लिए random segment

यह hierarchical design IDs को chronologically ordered रखता है जबकि distributed systems के लिए adequate entropy maintain करता है।

Collision Resistance

CUID uniqueness guarantee करने के लिए कई mechanisms combine करता है:

Temporal Precision
Millisecond precision का मतलब है कि different execution threads में बने IDs ज्यादातर different values से शुरू होते हैं।
Sequential Counter
अगर दो IDs same millisecond में बनाए जाएं, तो counter increment होता है जो single process के अंदर uniqueness guarantee करता है।
Host Fingerprint
PID और hostname से derived fingerprint different servers से issue किए गए IDs को differentiate करता है।
Random Entropy
Random segment remaining collisions के खिलाफ extra defense layer जोड़ता है।

CUID vs UUID

CUID और UUID दोनों uniqueness problem solve करते हैं लेकिन अलग-अलग approaches से:

FeatureCUID v1UUID v4
FormatLowercase alphanumericHex with hyphens
SortablePartially (timestamp first)No (v4 random)
URL-safeहाँहाँ (hyphens के साथ)
Collision ResistanceHigh (structural)High (statistical)
PredictabilityLow (visible timestamp)Very low
Length25 characters36 characters
समन्वय की आवश्यकतानहींनहीं

CUID visible database IDs और URLs के लिए बेहतर है; UUID अधिक widely-adopted और broader support वाला है।

CUID v1 vs CUID v2

CUID v2 CUID v1 की security concerns को address करने वाला complete algorithm rewrite है:

पहलूCUID v1CUID v2
AlgorithmCustomSHA3 + CSPRNG
Cryptographicनहीं (predictable)हाँ (cryptographically secure)
TimestampClearly visibleHidden in hash
FormatAlways starts with 'c'Customizable prefix
npm packagecuid@paralleldrive/cuid2
LengthFixed 25 charactersCustomizable (default 24)

New projects के लिए, CUID v2 recommended है जो ease of use maintain करते हुए stronger security guarantees provide करता है।

Common Use Cases

Database IDs
Primary keys के रूप में ideal — human-readable, compact, encoding के बिना URL-safe।
Session Management
Web applications के लिए unique session IDs generate करना guessing के खिलाफ resistance के साथ।
Request IDs
Distributed systems के across requests को unique ID से track करना जो log और trace हो सके।
File IDs
Uploaded content या temporary files के लिए unique filenames generate करना।
Data Synchronization
Multiple devices या distributed databases के across data synchronize करना uniqueness confidence के साथ।
URL Identifiers
Short links या video IDs create करना guaranteed minimal collision के साथ।

Code Examples

Installation और basic usage:

JavaScript / TypeScript
// npm install @paralleldrive/cuid2  (recommended — CUID v2)
import { createId } from '@paralleldrive/cuid2'

const id = createId()
// → 'tz4a98xxat96iws9zmbrgj3a'

// Custom length
import { init } from '@paralleldrive/cuid2'
const createShortId = init({ length: 10 })
createShortId() // → 'zxp1l6mf4c'

Node.js और common database integration:

Node.js (no dependencies)
// Pure Node.js — CUID v1 style (no dependencies)
let counter = 0

function pad(str, size) {
  return str.padStart(size, '0').slice(-size)
}

function fingerprint() {
  const os = require('os')
  const source = [process.pid, os.hostname().length].join('')
  let hash = 0
  for (const c of source) {
    hash = ((hash << 5) - hash) + c.charCodeAt(0)
    hash |= 0
  }
  return pad(Math.abs(hash).toString(36), 4)
}

function cuid() {
  const timestamp = Date.now().toString(36)
  const cnt       = pad((counter++ & 0xffff).toString(36), 4)
  const fp        = fingerprint()
  const rnd       = pad(Math.floor(Math.random() * 0xffffffff).toString(36), 4)
              + pad(Math.floor(Math.random() * 0xffffffff).toString(36), 4)
  return 'c' + timestamp + cnt + fp + rnd
}

console.log(cuid()) // → 'clrc4gkwz001ag2hs3k7f9m2q'

Database constraint usage:

Prisma / SQL
-- Use CUID as a primary key in PostgreSQL
CREATE TABLE users (
  id   TEXT        PRIMARY KEY DEFAULT gen_cuid(),
  name TEXT        NOT NULL,
  created_at TIMESTAMPTZ DEFAULT now()
);

-- Prisma schema (auto-generates CUID by default)
model User {
  id        String   @id @default(cuid())
  name      String
  createdAt DateTime @default(now())
}

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

क्या CUID different servers में truly unique है?
हाँ — fingerprint mechanism (PID + hostname) ensure करता है कि different servers से issue किए IDs fingerprint portion में vary करें। Time-based counter और random segment further protection provide करते हैं।
क्या CUID browser में use हो सकता है?
हाँ — CUID library browser और Node.js दोनों compatible है। Browser environment में, fingerprint process data की बजाय navigator information और कुछ other data use करता है।
Alternatives की तुलना में CUID कितना बड़ा है?
CUID v1 fixed 25 characters है। UUID v4 hyphens के साथ 36 characters है। NanoID customizable है, typically 21 characters। CUID v2 customizable है, default 24 characters।
क्या CUID cryptographically secure है?
CUID v1 cryptographically secure नहीं है — timestamp visible है और कुछ components predictable हैं। CUID v2 SHA3 और CSPRNG use करता है, जो इसे security-sensitive cases के लिए suitable बनाता है।
CUID और NanoID के बीच main difference क्या है?
NanoID fully customizable sizes के साथ small size और cryptographic security पर focus करता है। CUID partial chronological sortability और visual distinctiveness को prioritize करता है। CUID v2 security को recognizable identity के साथ combine करता है।
CUID collisions को कैसे handle करता है?
Collisions theoretically possible लेकिन statistically very unlikely हैं। Counter single process के अंदर collisions prevent करता है; fingerprint different machines को separate करता है; random segment edge cases handle करता है। Algorithm real-world production systems के लिए designed है।