🔢Developer

How to Generate a UUID in Any Programming Language

UUIDs prevent collisions in distributed systems. Here's how to generate them correctly across languages and what the different versions mean.

4 min readFebruary 4, 2026By FreeToolKit TeamFree to read

Every database row needs an ID. The question is whether that ID should come from the database (auto-increment) or be generated by your application (UUID). When you need IDs generated without database interaction, or across multiple systems, UUIDs are the answer.

Generate a UUID Now

In JavaScript

crypto.randomUUID() is built into modern browsers and Node.js 15+. No library needed. Returns a UUID v4 string. For older environments: import { v4 as uuidv4 } from 'uuid' and call uuidv4(). The uuid package is well-maintained and has 5M+ weekly downloads.

In Python

import uuid; str(uuid.uuid4()) — built into Python's standard library. For UUID v7 (time-ordered), the python-uuid7 package provides uuid7.uuid7(). No installation needed for v4.

In Other Languages

  • Java: UUID.randomUUID().toString() — built-in
  • Go: google/uuid package (uuid.New())
  • Rust: uuid crate with rand feature
  • PHP: Str::uuid() in Laravel, or ramsey/uuid package
  • SQL (PostgreSQL): gen_random_uuid() — built-in

UUID v7 for Database Primary Keys

UUID v4 is random, which means adjacent insertions go to different parts of a B-tree index, causing fragmentation and slower queries over time. UUID v7 includes a millisecond timestamp prefix, so recently-created records have adjacent UUIDs. They sort chronologically. Database index performance is much closer to auto-increment integers. If you're using UUIDs as primary keys in PostgreSQL or MySQL, v7 is worth using.

Formatting note

UUIDs are case-insensitive: 550E8400-E29B-41D4-A716-446655440000 and 550e8400-e29b-41d4-a716-446655440000 are the same UUID. Store consistently (lowercase is conventional) and normalize on input if your system accepts user-supplied UUIDs.

Frequently Asked Questions

What is a UUID and what is it used for?+
A UUID (Universally Unique Identifier) is a 128-bit identifier formatted as 8-4-4-4-12 hexadecimal characters (like 550e8400-e29b-41d4-a716-446655440000). It's used wherever you need unique IDs without a central authority to coordinate assignments — database record IDs, session tokens, file names, message queue IDs, and API keys. The 'universally unique' part means the chance of two independently generated UUIDs colliding is statistically negligible.
What's the difference between UUID versions?+
UUID v1: time-based + MAC address. Unique and sortable but reveals hardware info. UUID v3/v5: namespace-based deterministic UUIDs (same input always produces same UUID). UUID v4: randomly generated, the most common choice for general use. UUID v7: time-ordered random UUID (new in 2024) — sortable by creation time, useful as database primary keys because it reduces index fragmentation. Most general use cases: v4 for random IDs, v7 for database primary keys where sort order matters.
Are UUIDs really unique?+
Practically yes. UUID v4 has 122 random bits, meaning there are 2^122 possible values (about 5.3 × 10^36). To have a 50% chance of a collision, you'd need to generate 2.71 × 10^18 UUIDs. If you generate a billion UUIDs per second, reaching that collision probability would take 85 years. In practice, UUID collisions are treated as theoretically possible but practically impossible — like your RAM having a bit flip.
Should I use UUIDs or auto-increment integers for database IDs?+
Depends on your requirements. Auto-increment integers are compact (4–8 bytes vs 16 bytes), fast to index, and clearly sequential. UUIDs don't reveal record counts, can be generated without a database round-trip (useful for distributed systems), and don't expose insertion sequence. For most monolithic applications, auto-increment is simpler. For distributed systems, APIs where you don't want to expose scale, or offline-capable apps, UUIDs are better. UUID v7 mitigates the index fragmentation problem of random v4 UUIDs.

🔧 Free Tools Used in This Guide

FT

FreeToolKit Team

FreeToolKit Team

We build free browser-based tools and write practical guides that skip the fluff.

Tags:

developeruuidprogrammingtools