Software Engineering :: Identifier :: NanoID :: Advocacy :: Why we chose NanoIDs for PlanetScale’s API
⪼ Made with 💜 by Polyglot.
When we were first building PlanetScale’s API, we needed to figure out what type of identifier we’d be using. We knew that we wanted to avoid using integer IDs so that we wouldn’t reveal the count of records in all our tables.
The common solution to this problem is using a UUID (Universally Unique Identifier) instead. UUIDs are great because it’s nearly impossible to generate a duplicate and they obscure your internal IDs. They have one problem though. They take up a lot of space in a URL: api.planetscale.com/v1/deploy-requests/7cb776c5-8c12-4b1a-84aa-9941b815d873.
Try double clicking on that ID to select and copy it. You can’t. The browser interprets it as 5 different words.
It may seem minor, but to build a product that developers love to use, we need to care about details like these.
We decided that we wanted our IDs to be:
Shorter than a UUID Easy to select with double clicking Low chance of collisions Easy to generate in multiple programming languages (we use Ruby and Go on our backend) This led us to NanoID, which accomplishes exactly that.
Here are some examples:
izkpm55j334u z2n60bhrj7e8 qoucu12dag1x
These are much more user-friendly in a URL: api.planetscale.com/v1/deploy-requests/izkpm55j334u
An ID collision is when the same ID is generated twice. If this happens seldomly, it’s not a big deal. The application can detect a collision, auto-generate a new ID, and move on. If this is happening often though, it can be a huge problem.
The longer and more complex the ID, the less likely it is to happen. Determining the complexity needed for the ID depends on the application. In our case, we used the NanoID collision tool and decided to use 12 character long IDs with the alphabet of 0123456789abcdefghijklmnopqrstuvwxyz.
This gives us a 1% probability of a collision in the next ~35 years if we are generating 1,000 IDs per hour.
If we ever need to increase this, the change would be as simple as increasing the length in our ID generator and updating our database schema to accept the new size.
...
