4

For a database. Should I use an UUID or an integer for the primary key?

I will never exceed 2^32 in amount of rows, so an integer should be more than plenty.

I would prefer to use an integer, as I find it easier to work with, especially when using different languages.

Question: What is the advantage of UUIDs over integers?

KaareZ
  • 151

3 Answers3

7

The intent of UUIDs is to enable distributed systems to uniquely identify information without significant central coordination. (Wikipedia)

UUIDs are merely integers that are picked at random by nodes that cannot necessarily communicate with each other, so they are only very, very likely to be unique rather than guaranteed. If you're programming a NON-distributed system, there is no advantage.

Kilian Foth
  • 110,899
4

In addition to what Kilian Foth said, an advantage to UUIDs can be that they increase the address space and may be harder to guess.

Example: If you expose a REST API that contains the id of an entity in the path, a hacker might guess that if

GET http://mysupersensitive.data/supersecret/42 works,

GET http://mysupersensitive.data/supersecret/41 might work as well.

When it is UUIDs, this becomes much harder.

EDIT: The comment by Jules is right of course, this in itself must not be regarded as a security mechanism.

ftr
  • 1,621
  • 1
  • 14
  • 19
-1

Use a GUID/UUID

Honestly the number of times I heard "we will never run out of integers" only for whatever system to quickly run out of integers.

UUIDs offer significant advantages when you come to archiving data, replication and having code able to generate new Ids without writing to the database

Ewan
  • 83,178