{
  "id": 20495137,
  "title": "UUID v4 or auto-increment? A practical guide to picking primary keys",
  "url": "https://urgent.news/2026/11/17/uuid-v4-or-auto-increment-a-practical-guide-to-picking-primary-keys",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-17T21:11:57.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/pablo_infante/uuid-v4-or-auto-increment-a-practical-guide-to-picking-primary-keys-5d4g"
  },
  "original_language": "en",
  "account": "When designing tables in a project, you have to decide whether primary keys should increment sequentially (1, 2, 3...) or appear as random UUIDs (550e8400-e29b-41d4-a716-446655440000). It's not that UUIDs are inherently modern, but rather a trade-off between index performance and migration difficulty. The main question to ask is: who is responsible for generating the IDs? If only the database can create them, one option is an auto-increment integer, which is small and naturally ordered. However, if any client or service can generate IDs, a UUID v4 becomes the better option. This is because UUIDs provide 122 random bits, allowing any process to generate unique IDs independently with almost no risk of collisions. There are two benefits to using UUIDs that are often overlooked. Firstly, they eliminate the need for a round-trip to retrieve an ID, as the client can already know the ID before the row exists. This is particularly useful for UIs that require optimistic updates or for quickly linking related records in a single batch. Secondly, using UUIDs prevents the leakage of business data. While an auto-increment ID like /orders/438 might unintentionally reveal how many orders you have, a UUID like /orders/9b2c... offers no such insight. A common pitfall to avoid is that random v4 UUIDs are not ordered, which can lead to page fragmentation and slower insert throughput in clustered indexes (such as InnoDB's primary key or SQL Server). If you need UUIDs and maintain insert locality, consider using UUID v7 instead. This version is time-ordered, ensuring new values sort to the end while still maintaining global uniqueness. It's worth noting that a UUID is unguessable, not secret. So while it's perfectly fine to use UUIDs in URLs, they should not be considered access tokens. Authorization should be handled separately. When making your decision, consider the specific situation. If a single database owns the sequence and generates IDs internally, auto-increment IDs are the best choice. If IDs are generated by multiple clients or services, or if they need to be exposed publicly without revealing volume, go with UUID v4 (or v7 if insert order is important). In some cases, a hybrid approach can be beneficial. You can use a compact auto-increment integer as the internal clustered key for join performance, while adding a UUID as the public-facing identifier in APIs and URLs. This way, you get the benefits of both index locality and opacity. If you're unsure about the best approach, it's a good idea to try it out before committing. You can generate some v4 values using a client-side generator that runs entirely in the browser with crypto.getRandomValues. This ensures the keys are safe for both throwaway or real use cases. For a more in-depth explanation of the format, why collisions are unlikely, and the full comparison between UUIDs and auto-increment, you can refer to the extended article \"What is a UUID v4 and when to use it\". Ultimately, the decision should be based on your specific write pattern, not just following a trend.",
  "summary": "Every project hits the same fork the moment you design your first table: do primary keys count up — 1, 2, 3 — or do they look like 550e8400-e29b-41d4-a716-446655440000 ? The answer isn't \"UUIDs are modern, use UUIDs.\" It's a trade-off, and picking the wrong side costs you either index performance or a painful migration later. Here's the decision the way I actually make it. The one question that…",
  "key_points": [
    "Use UUID v4 when clients generate IDs, ensuring uniqueness with minimal risk of collisions.",
    "UUIDs eliminate the need for round-trips to retrieve IDs and prevent business data leakage.",
    "Consider UUID v7 for ordered UUIDs to maintain insert locality in clustered indexes."
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}