Blog › Developer Security

UUID vs Random Strings: Which Should You Use?

By Marcus Webb, Security AnalystJuly 13, 20268 min read

Both UUIDs and random strings are used as unique identifiers in software — but they're not interchangeable. Choosing the wrong one can cause subtle issues with security, readability, URL safety, and database performance. This guide explains the differences and tells you exactly when to use each.

What is a UUID?

A UUID (Universally Unique Identifier) is a standardized 128-bit identifier defined by RFC 4122. UUID v4 — the most common variant — uses cryptographic randomness for 122 of its 128 bits (the other 6 encode the version and variant). It always follows the same format:

550e8400-e29b-41d4-a716-446655440000

Format: 8-4-4-4-12 hexadecimal characters separated by hyphens

A UUID is always 36 characters (32 hex + 4 hyphens). Without hyphens, it's 32 characters. The standardized format means any UUID-aware library can parse, validate, and handle it correctly.

What is a random string?

A random string is a sequence of characters generated from a random source with no fixed format. The character set, length, and encoding are all variable:

Hex (32 bytes): a3f8c2d19e4b7f0a1d6e8c4b2f9d5e3a7b0c1d2e3f4a5b6c7d8e9f0a1b2c3d4e

Base64URL (32 bytes): o_jC0Z5Lfw oB1uDCyfrntbA

Alphanumeric (32 chars): K7mXpQ2rNsYvWcJh8tFbDgA3nUeLZ1oP

Entropy comparison

IdentifierRandom bitsCharacters
UUID v4122 bits36 (with hyphens)
16-byte hex string128 bits32
32-byte hex string256 bits64
32-byte Base64URL256 bits43
UUID v7 (ordered)74 bits random + timestamp36

A UUID v4 has 122 bits of randomness — enough for collision resistance as a database identifier. For security tokens (API keys, session tokens), 256 bits from a dedicated random source is preferable.

When to use a UUID

  • Database primary keys — UUIDs allow records to be created in multiple services without a central sequence coordinator. Widely supported by ORMs and databases.
  • Public-facing resource IDs — The standardized format is self-documenting. Any developer seeing GET /users/550e8400-e29b-41d4-a716-446655440000 immediately understands it's a UUID.
  • Distributed system identifiers — When multiple services need to create IDs independently without coordination.
  • When libraries expect them — Many frameworks, databases (PostgreSQL's UUID type), and protocols have native UUID support.

When to use a random string

  • API keys — You control the format, length, and character set. A 32-byte hex string gives 256 bits of entropy (stronger than a UUID's 122). You can add meaningful prefixes like sk_live_.
  • Session tokens — Random strings are more compact in Base64URL format, and you can tune the entropy to your security requirements.
  • Password reset tokens — One-time tokens should have at least 128 bits of randomness. A random hex string gives you precise control.
  • CSRF tokens — Short-lived tokens where you want a specific format or maximum length.
  • When URL safety matters — A hex or Base64URL string doesn't require percent-encoding in URLs. UUIDs are also URL-safe, but longer.

UUID drawbacks for databases at scale

UUID v4 is completely random, which means database B-tree indexes fragment heavily as records are inserted in random ID order. This causes "index bloat" and slower queries on large tables.

Solutions to this:

  • UUID v7 (RFC 9562, 2024) — Includes a millisecond timestamp prefix for sort order, dramatically reducing index fragmentation while keeping UUID format compatibility.
  • ULID — A 128-bit sortable identifier that's lexicographically sortable and more compact in Base32 encoding.
  • Snowflake IDs — Used by Twitter/X and Discord: timestamp + machine ID + sequence number in a 64-bit integer.

Quick decision guide

Database primary key (small/medium scale)
UUID v4 ✓
Database primary key (high-insert-rate, large tables)
UUID v7 or ULID ✓
API authentication key
32-byte random hex or Base64URL string ✓
Session token
32-byte random Base64URL string ✓
Public resource identifier in URL
UUID v4 or UUID v7 ✓
One-time password reset token
16–32 byte random hex string ✓

Try the generators

Our UUID Generator creates RFC 4122 v4 UUIDs in your browser. For API keys and random strings, the API Key Generator lets you choose format (hex, Base64URL, alphanumeric) and length.

Frequently asked questions

Is UUID v4 secure enough to use as an API key?

It's acceptable but not ideal. UUID v4 has 122 bits of entropy, which is sufficient for collision resistance. However, 6 bits are fixed (version and variant indicators), and the format is well-known, making it slightly less opaque than a random hex string. For API keys, a 32-byte random hex string (256 bits of entropy) is a better choice.

Can two UUIDs ever collide?

In theory yes, but in practice it's negligible. With 2^122 possible UUID v4 values, generating one trillion UUIDs per second for one billion years would still result in only a 50% chance of a single collision. For all practical purposes, UUIDs are unique.

What is UUID v7 and should I use it?

UUID v7 (standardized in RFC 9562, April 2024) combines a millisecond timestamp with random bits, making UUIDs monotonically increasing within the same millisecond. This eliminates B-tree index fragmentation, making it significantly better for database primary keys than UUID v4 while maintaining UUID format compatibility.

Why are UUIDs represented in 5 groups separated by hyphens?

The 8-4-4-4-12 grouping has historical roots in OSF DCE (Distributed Computing Environment) from the early 1990s. The groups originally corresponded to different data fields (timestamp, clock sequence, node). In UUID v4, these fields are filled with random bits, but the format was retained for backward compatibility.