UUID
GUID

How do I create a GUID / UUID?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A UUID is a 128-bit identifier designed to be unique without requiring a central counter. In many ecosystems the term GUID is used for the same practical idea, so most developers can treat GUID and UUID as interchangeable unless a platform has a very specific format requirement.

Choose the Right Kind of UUID

The most common choice in application code is a random version 4 UUID. It is simple, widely supported, and good enough for identifiers such as order IDs, session tokens, or database keys when you do not need sortable values.

The important rule is to use a cryptographically strong generator, not a homemade Math.random pattern or string concatenation trick. A weak generator can produce collisions or predictable values.

JavaScript: Prefer Built-in crypto.randomUUID

Modern browsers and current Node.js versions expose crypto.randomUUID().

javascript
const id = crypto.randomUUID();
console.log(id);

In Node.js, the same idea works through the crypto module:

javascript
1const { randomUUID } = require('crypto');
2
3const id = randomUUID();
4console.log(id);

This is usually the best answer for JavaScript because it is concise and uses a strong randomness source.

Python: Use the Standard Library

Python has a built-in uuid module.

python
1import uuid
2
3value = uuid.uuid4()
4print(value)
5print(str(value))

If you need the raw bytes instead of the string form, that module also exposes them directly.

python
1import uuid
2
3value = uuid.uuid4()
4print(value.bytes)

For most applications, the string form such as 550e8400-e29b-41d4-a716-446655440000 is the one you store or log.

Java: Use UUID.randomUUID

Java also has a standard-library solution.

java
1import java.util.UUID;
2
3public class Main {
4    public static void main(String[] args) {
5        UUID id = UUID.randomUUID();
6        System.out.println(id.toString());
7    }
8}

This avoids extra dependencies and is the normal choice in Java services.

When Not to Use a UUID

UUIDs solve uniqueness, but they are not perfect for every requirement.

They are longer than simple integer IDs, harder to read manually, and random version 4 values do not preserve insertion order. In some databases that can affect index locality compared with monotonically increasing keys.

That does not make UUIDs bad. It means you should use them when decentralized uniqueness matters more than human readability or index locality.

For example, UUIDs are a good fit when:

  • records are created on many machines independently
  • you do not want to expose sequential IDs publicly
  • you need an ID before a database insert round trip

They are less attractive when:

  • a simple integer key is sufficient and local to one database
  • humans must type or compare the IDs regularly
  • ordered IDs are important for storage patterns

Avoid Homemade Generators

You will still see snippets based on template replacement and Math.random. Those examples are common because they are short, not because they are the best choice.

If a platform already provides a UUID generator, use it. Built-ins are clearer, safer, and more likely to match the expected variant and version bits correctly.

A custom implementation only makes sense when the environment truly lacks a proper generator and you fully understand the consequences.

Common Pitfalls

Using Math.random for UUID generation in JavaScript is the most common mistake. It is not the right source for robust identifier generation.

Mixing GUID and UUID terminology as if they were completely different mechanisms also creates unnecessary confusion. In most application code, the practical difference does not matter.

Assuming UUIDs are always the best database key is another mistake. They solve one problem well, but they do not optimize every storage pattern.

Finally, do not trim or reformat UUID strings arbitrarily if another system expects the standard canonical form.

Summary

  • GUID and UUID usually refer to the same practical identifier concept in everyday development
  • version 4 UUIDs are the common default when you need decentralized unique IDs
  • prefer built-in generators such as crypto.randomUUID, Python's uuid.uuid4, or Java's UUID.randomUUID
  • avoid homemade random-string generators when secure built-ins already exist
  • UUIDs are great for uniqueness, but not automatically the best choice for every key-design problem

Course illustration
Course illustration

All Rights Reserved.