Development 6 min read
Sooner or later every app needs unique identifiers for users, orders, files or events. A counter works until you have more than one database, an offline client or a public API that should not reveal how many records you have. That is where UUIDs come in.
In this guide you will learn what a UUID is, how version 4 (random) differs from version 7 (time-ordered), why v7 is usually the better choice for database primary keys and how to avoid the most common mistakes.
Generate UUIDs now Create one or many UUIDs in your browser. Nothing is sent to a server. Open the UUID generatorWhat is a UUID (or GUID)?
A UUID (Universally Unique Identifier), also called a GUID in the Microsoft world, is a 128-bit value used to identify something. It is normally written as 32 hexadecimal digits split into five groups by hyphens in the pattern 8-4-4-4-12, which gives 36 characters in total.
Example: 018f4e2a-7c3b-7d4e-9a1b-2c3d4e5f6a7b. One digit tells you the version (the first digit of the third group) and a couple of bits mark the variant, so the rest carries the actual data.
Why use UUIDs?
- No central counter: any server, device or browser can create an ID on its own, without asking a database for the next number.
- Safe for distributed systems: several services, regions or offline clients can create records and merge them later without clashes.
- Generate before saving: the client can know the ID of a new record before the server confirms it, which simplifies offline-first apps and retries.
- No row counts leaked: an ID like 1042 tells outsiders roughly how many records exist and makes guessing neighbors easy. A UUID does not.
UUID v4: fully random
Version 4 is the most common one. Apart from the few bits that mark the version and variant, all of its 122 remaining bits are random. There is no order, no timestamp and no information about the machine that made it.
That makes v4 simple, widely supported and a good default for general use, especially when you do not want an ID to reveal when something was created.
UUID v7: time-ordered and random
Version 7 was standardized in RFC 9562 (2024), which updated the older UUID specification. Its first 48 bits hold a Unix timestamp in milliseconds, and most of the remaining bits are random.
Because the timestamp comes first, v7 values sort by creation time: a UUID created later is greater than one created earlier (at millisecond precision). You still generate it without any central coordination.
In short: v4 is all random, v7 is "timestamp first, random after".
Why v7 is better for database primary keys
Most databases keep a primary key in an index ordered by value. With random v4 keys, every new row lands at a random spot in that index, which causes page splits, more fragmentation, a poorer cache hit rate and slower inserts on large tables.
With v7, new keys are always near the end of the index, just like an auto-increment number. You keep the benefits of UUIDs and get much better index locality. Newest-first listings and range queries by time also become easier.
The trade-off: a v7 reveals roughly when the record was created. If that is sensitive, use v4 or keep the UUID out of public URLs.
Other versions, briefly
- v1: built from a timestamp plus a node identifier, historically the MAC address. It can leak information about the machine, so it is rarely recommended today.
- v5: name-based. It hashes a namespace and a name with SHA-1, so the same input always gives the same UUID. Useful for deterministic IDs.
- v3 is the same idea as v5 but with MD5, and v6 is a reordered v1. For new projects you will usually pick v4 or v7.
Can two UUIDs collide?
In theory yes, in practice the chance is negligible. With 122 random bits in a v4, you would need to generate an enormous number of IDs before a duplicate becomes even remotely likely, far more than almost any system will ever create.
That assumes a good source of randomness. Use your platform's secure random generator, not a homemade one. If uniqueness is critical, keep a unique constraint on the column anyway.
UUID vs auto-increment
- Auto-increment is small (4 or 8 bytes), fast, readable and perfect for a single database. But it needs one authority to hand out numbers and exposes volume and order.
- UUIDs take 16 bytes, are harder to read and type, and make indexes larger. In return they work across systems and are not guessable.
- A common compromise is an internal auto-increment key plus a UUID as the public identifier. Another is simply using v7 as the primary key.
How to generate UUIDs with the free tool
- Open the UUID generator. It runs entirely in your browser, so nothing is sent to a server.
- Choose the version you need (v4 or v7) and how many UUIDs to create.
- Generate, then copy one or the whole list into your code, database seed or test data.
Common mistakes to avoid
- Using a UUID as a secret or access token. A UUID is an identifier, not a credential. v7 is partly predictable because of its timestamp, and even v4 was not designed for authentication. Use a proper random token for that.
- Relying on a UUID in a URL as the only access control. Always check permissions.
- Storing UUIDs as 36-character text when the database has a native type (such as UUID in PostgreSQL) or a 16-byte binary column. It wastes space and slows indexes.
- Mixing v4 and v7 in the same column and expecting a time order.
- Writing your own generator from Math.random() or similar weak randomness.
Conclusion
A UUID gives you unique IDs without a central counter. Use v7 when the ID will be a database primary key and time order helps, use v4 when you want pure randomness and no creation time, and never treat either one as a secret. Store them in a native or binary column and generate them with our free tool whenever you need a few.
Frequently asked questions
What is the difference between UUID and GUID?
They are the same thing. GUID is the name used mostly in Microsoft environments, UUID is the standard name. Both are 128-bit identifiers.
Should I use UUID v4 or v7?
Use v7 for database primary keys and anything where sorting by creation time helps. Use v4 for general purposes or when you do not want the ID to reveal when it was created.
Is a UUID safe to use as a password or token?
No. A UUID is meant to identify, not to protect. For session tokens, API keys or password resets use a long token from a cryptographically secure random generator.
Can UUIDs collide?
The chance is negligible in practice when they are generated with good randomness. Keep a unique constraint in the database as a safety net.
Does the UUID generator send my IDs to a server?
No. The UUIDs are created locally in your browser and are not sent or stored anywhere.
Free tools mentioned in this article
Keep reading
- How to Create a Strong PasswordLength, randomness and uniqueness: the simple rules that actually protect your accounts, and how to follow them without memorizing a thing.
- The Pomodoro Technique: Complete GuideWork in focused 25-minute blocks with short breaks. Learn how the method works, how to start today and how to adapt it to your routine.
- What Is a Hash? MD5, SHA-256 and What They Are Used ForA hash is a short fingerprint of any data. Learn how it works, which algorithms to trust and how to use hashes to verify files and protect passwords.