Menu

Why I reach for a UUID v4 instead of an auto-increment ID
πŸ“°

Why I reach for a UUID v4 instead of an auto-increment ID

DEV Community: databaseΒ·Pablo InfanteΒ·15 days ago
#jYXXwXbT
#devtodatabase#devto#dev#uuid#index
Reading 0:00
15s threshold

Auto-increment primary keys are fine until they aren't. The moment you have two services writing to the same logical dataset, a sharded table, an offline client that creates rows before it syncs, or a public URL you'd rather not have /orders/1 , /orders/2 leaking your volume β€” the sequential integer starts to hurt.

That's the case for UUID v4 .

What a v4 actually is

A UUID v4 is 128 bits, of which 122 are random (the rest are fixed version/variant bits). You get a value like:

f47ac10b-58cc-4372-a567-0e02b2c3d479

The useful property: you can generate one anywhere β€” on the client, in a worker, in three services at once β€” without coordinating with a central sequence, and the chance of a collision is negligible for any realistic volume.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.