Are there any downsides to using nanoid for primary key?

Viewed 1098

I know that UUIDs and incrementing integers are often used for primary keys. I'm thinking of nanoids instead because those are URL friendly without being guessable / brute-force scrapeable (like incrementing integers).

Would there be any reason not to use nanoids as primary keys in a database like Postgres? (For example: Maybe they drastically increase query time since they aren't ... aligned or something?)

https://github.com/ai/nanoid

3 Answers

Most databases use incrementing id's because it's more efficient to insert a new value onto the end of a B-tree based index.

If you insert a new value into a random place in the middle of a B-tree, it may have to split the B-tree nonterminal node, and that could cause the node at the next higher level to split, and so on up to the top of the B-tree.

This also has a greater risk of causing fragmentation, which means the index takes more space for the same number of values.

Read https://www.percona.com/blog/2015/04/03/illustrating-primary-key-models-in-innodb-and-their-impact-on-disk-usage/ for a great visualization about the tradeoff between using an auto-increment versus UUID in a primary key.

That blog is about MySQL, but the same issue applies to any B-tree based data structure.

I'm not sure if there is a disadvantage to using nanoids, but they are often unnecessary. While UUIDs are long, they can be translated to a shorter format without losing entropy.

See the NPM package (https://www.npmjs.com/package/short-uuid).

UUIDs are standardized by the Open Software Foundation (OSF) and described by the RFC 4122. That means that there will be far more chances for other tools to give you some perks around it.

Some examples:

  • MongoDB has a special type to optimize the storage of UUIDs. Not only a NanoID string will take more space, but even the binary takes more bits (126 in Nano ID and 122 in UUID)

  • Once saw a logging tool extracting the timestamp from the uids, can't remember which, but is is available

Also the long, non reduced version of UUIDs are very easy to identify visually. When the end user is a developer, it might help to understand the nature/source of the ID (like clearly not a database auto-increment key)

Related