About Me

header ads
Advertisement

What Is a UUID and When Should You Use One?

What Is a UUID and When Should You Use One?

A UUID (Universally Unique Identifier) is a 128-bit value used to label something, such as a database record, a file, or a user session, without needing a central system to hand out numbers.

What a UUID looks like

550e8400-e29b-41d4-a716-446655440000

It is 32 hexadecimal characters, shown in five groups in an 8-4-4-4-12 pattern, separated by hyphens (36 characters in total).

Common versions

  • Version 1: based on timestamp and a machine identifier. It can reveal when and where it was made.
  • Version 4: generated from random numbers. This is the most widely used version.
  • Version 5: generated from a name and a namespace using hashing, so the same input always gives the same UUID.
  • Version 7: time-ordered and random, designed to work well as a database key because new IDs sort roughly in creation order.

How unique is “unique”?

A version 4 UUID has 122 random bits. The chance of a duplicate is so small that you would need to generate roughly 2.7 quintillion of them before reaching even a 50% chance of a single collision. For nearly all applications, collisions are not a practical concern.

Why developers use UUIDs

  • No central counter needed: many servers can create IDs independently.
  • Hard to guess: unlike sequential numbers (1, 2, 3), random UUIDs are not easily enumerated in a URL.
  • Safe to merge data: records from different systems are unlikely to clash.
  • Easy offline creation: a mobile app can create IDs before syncing.

Trade-offs

  • They are longer than integer IDs, so they use more storage.
  • Fully random UUIDs (version 4) can cause slower inserts in some databases, because new rows land at random positions in an index. Time-ordered versions such as version 7 reduce this problem.
  • They are harder for people to read or type.
  • A UUID is an identifier, not a security feature. Do not rely on it alone to protect private data.

Generate one now

Use a UUID generator to create one or many identifiers instantly. If you are working with API data, a JSON formatter can help you read the records that contain them.

Quick guidance

  1. Use version 4 for general-purpose, unpredictable IDs.
  2. Consider version 7 for database primary keys in new projects.
  3. Store UUIDs in a native UUID or binary column where your database supports it.
  4. Keep the format consistent, for example always lowercase.