<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Günther Hintringer</title>
    <description>The latest articles on DEV Community by Günther Hintringer (@chiefgue).</description>
    <link>https://dev.to/chiefgue</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4283077%2F7724ad3f-3489-41f2-b77b-32a929a9f613.jpg</url>
      <title>DEV Community: Günther Hintringer</title>
      <link>https://dev.to/chiefgue</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chiefgue"/>
    <language>en</language>
    <item>
      <title>UUID v4 vs v7: differences, database speed and which to pick</title>
      <dc:creator>Günther Hintringer</dc:creator>
      <pubDate>Fri, 04 Dec 2026 15:20:10 +0000</pubDate>
      <link>https://dev.to/chiefgue/uuid-v4-vs-v7-differences-database-speed-and-which-to-pick-54j3</link>
      <guid>https://dev.to/chiefgue/uuid-v4-vs-v7-differences-database-speed-and-which-to-pick-54j3</guid>
      <description>&lt;p&gt;Both are 128-bit UUIDs that look alike and fit the same columns. The difference is the first 48 bits: random in v4, the creation time in v7. That one change decides how they behave in a database and what they reveal.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short answer
&lt;/h2&gt;

&lt;p&gt;Use &lt;strong&gt;v7&lt;/strong&gt; for database keys and for anything you want to sort by creation time. Use &lt;strong&gt;v4&lt;/strong&gt; when an ID must not reveal when it was made, for example in a public link. Both are unique enough that you never have to worry about duplicates.&lt;/p&gt;

&lt;p&gt;If you can't decide, take v7. RFC 9562, the current UUID standard, recommends it for new systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Side by side
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;v4&lt;/th&gt;
&lt;th&gt;v7&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Content&lt;/td&gt;
&lt;td&gt;122 random bits&lt;/td&gt;
&lt;td&gt;48-bit Unix time in milliseconds, then random bits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Random bits&lt;/td&gt;
&lt;td&gt;122&lt;/td&gt;
&lt;td&gt;62 to 74, depending on the generator&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sorts by creation time&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shows when it was made&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes, to the millisecond&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;New rows in a B-tree index&lt;/td&gt;
&lt;td&gt;Anywhere&lt;/td&gt;
&lt;td&gt;At the end&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Defined in&lt;/td&gt;
&lt;td&gt;RFC 4122 (2005), now RFC 9562&lt;/td&gt;
&lt;td&gt;RFC 9562 (2024)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Built in&lt;/td&gt;
&lt;td&gt;Almost everywhere&lt;/td&gt;
&lt;td&gt;Newer versions, for example PostgreSQL 18, .NET 9, Python 3.14&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  What the two look like
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;v4  3f2b8c1e-6d4a-4f0b-9a7e-2c5d8e1f4a6b
                   ^    ^ version 4, variant
    everything else is random

v7  01a183c6-500e-71fb-8e3a-1f489e0ea1c4
    ^^^^^^^^^^^^^ time: 28 October 2026, 18:29:34 UTC
                   ^    ^ version 7, variant
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can tell them apart by the first digit of the third group: 4 or 7. Paste any UUID into the &lt;a href="https://uuidgen.net/en/uuid-decoder/" rel="noopener noreferrer"&gt;decoder&lt;/a&gt; to see its version and, for v7, the exact time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Uniqueness: both are safe
&lt;/h2&gt;

&lt;p&gt;With v4, a duplicate needs two of the same 122 random bits. You would have to create about 2.7 quintillion v4 UUIDs for a 50 % chance of a single collision.&lt;/p&gt;

&lt;p&gt;With v7, two UUIDs can only collide if they were made in the same millisecond and their random parts match too. A busy system creates many UUIDs per millisecond, but each one still has at least 62 random bits. For real workloads that is as safe as v4. The &lt;a href="https://uuidgen.net/en/uuid-collision-calculator/" rel="noopener noreferrer"&gt;collision calculator&lt;/a&gt; shows the odds for both versions with your own numbers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Database speed
&lt;/h2&gt;

&lt;p&gt;Most databases store primary keys in a B-tree index, sorted by value. A new v4 key lands at a random place in that tree. With a large table, the right index page is often not in memory, has to be read from disk and may have to be split. Over time the index gets bigger and writes get slower.&lt;/p&gt;

&lt;p&gt;A new v7 key is larger than all keys before it, so it lands at the end of the index. The last pages stay in memory, pages fill up completely and inserts stay fast as the table grows. Range queries by creation time also get cheaper, because recent rows sit next to each other.&lt;/p&gt;

&lt;p&gt;For small tables you won't notice a difference. For tables with millions of rows it is often the main reason to switch. The guide &lt;a href="https://uuidgen.net/en/guides/uuid-primary-keys/" rel="noopener noreferrer"&gt;UUIDs as primary keys&lt;/a&gt; explains the details for PostgreSQL, MySQL and SQL Server. One catch: SQL Server sorts &lt;code&gt;uniqueidentifier&lt;/code&gt; by its last bytes first, so v7 does not sort by time there. The &lt;a href="https://uuidgen.net/en/code/uuid-sql-server/" rel="noopener noreferrer"&gt;SQL Server page&lt;/a&gt; shows what to do instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Privacy: what v7 reveals
&lt;/h2&gt;

&lt;p&gt;Anyone who sees a v7 UUID can read when it was created. For an order ID that is usually harmless. For a user ID in a public profile URL it tells everyone when that person signed up. For a document link it tells when the document was made.&lt;/p&gt;

&lt;p&gt;v4 reveals nothing at all. A common pattern is to use both: v7 as the internal primary key, and a separate v4 or other random ID for anything that leaves your system.&lt;/p&gt;

&lt;p&gt;Neither version is a secret. Don't use a UUID of any version as a password, session token or access key.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sorting and paging
&lt;/h2&gt;

&lt;p&gt;With v7 keys, &lt;code&gt;ORDER BY id&lt;/code&gt; gives you the rows in creation order without a separate timestamp column. That also makes keyset paging easy: ask for rows with an &lt;code&gt;id&lt;/code&gt; greater than the last one you saw.&lt;/p&gt;

&lt;p&gt;Two limits apply. The order is only as good as the clocks of the machines that create the IDs, so UUIDs from different servers within the same few milliseconds may not be in perfect order. And within one millisecond, only a generator with a counter keeps the order. The &lt;a href="https://uuidgen.net/en/uuid-v7-generator/" rel="noopener noreferrer"&gt;uuidgen.net v7 generator&lt;/a&gt; uses one, as described in RFC 9562.&lt;/p&gt;

&lt;h2&gt;
  
  
  Switching from v4 to v7
&lt;/h2&gt;

&lt;p&gt;You don't have to change existing data. v4 and v7 have the same size and format, so they fit the same column type. Keep the old v4 keys as they are, because other tables, URLs and logs refer to them, and create new keys as v7. The index slowly turns into one where new rows are added at the end.&lt;/p&gt;

&lt;p&gt;What you need is a v7 generator in your code or database. Many platforms have one built in by now, older ones need a small library. The &lt;a href="https://uuidgen.net/en/uuid-v7-generator/" rel="noopener noreferrer"&gt;v7 page&lt;/a&gt; lists one-liners for JavaScript, Python, Java, C#, Go and PostgreSQL, and the &lt;a href="https://uuidgen.net/en/code/" rel="noopener noreferrer"&gt;code examples&lt;/a&gt; go into detail for each language.&lt;/p&gt;

&lt;h2&gt;
  
  
  And v5?
&lt;/h2&gt;

&lt;p&gt;v5 is in a different category. It is not random and has no time: the same namespace and name always give the same UUID. Use it when two systems must compute the same ID independently, and v4 or v7 for everything else. The guide &lt;a href="https://uuidgen.net/en/guides/which-uuid-version/" rel="noopener noreferrer"&gt;Which UUID version should you use?&lt;/a&gt; covers v5 and the other versions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;You need&lt;/th&gt;
&lt;th&gt;Take&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Primary keys in a large table&lt;/td&gt;
&lt;td&gt;v7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IDs sorted by creation time&lt;/td&gt;
&lt;td&gt;v7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Public IDs that reveal nothing&lt;/td&gt;
&lt;td&gt;v4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wide support on old platforms&lt;/td&gt;
&lt;td&gt;v4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The same ID from the same input&lt;/td&gt;
&lt;td&gt;v5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can create both in the browser: the &lt;a href="https://uuidgen.net/en/uuid-v4-generator/" rel="noopener noreferrer"&gt;v4 generator&lt;/a&gt; and the &lt;a href="https://uuidgen.net/en/uuid-v7-generator/" rel="noopener noreferrer"&gt;v7 generator&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This article first appeared on &lt;a href="https://uuidgen.net/en/guides/uuid-v4-vs-v7/" rel="noopener noreferrer"&gt;uuidgen.net&lt;/a&gt;, where the guide is also available in German, Spanish, French, Japanese and Portuguese.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>uuid</category>
      <category>postgres</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
