Ask HN: IDs that are URL-friendly and fairly short?

7 points by tosh ↗ HN
I am looking for a way (or best practices) on fairly short IDs that are also URL friendly.

In a few past projects I've used UUID v4s with a custom encoding to shorten them down to 26 characters. But those IDs were still fairly long.

I found this topic surprisingly difficult to google for. Any pointers or opinions very welcome.

10 comments

[ 6.5 ms ] story [ 31.9 ms ] thread
I think it will depend on the application. Some things will require longer IDs than others will use.
Depends how much randomness you need. I usually just use a rand alpha of length 6-8. You can also use dictionary words (see gfycat).

Sample Ruby code:

  8.times.map { (('a'..'z').to_a + (0..9).to_a).sample }.join
It's less collision resistant than UUID, of course.
Auto-incremented integers encoded in base32.

You can also encode UUIDs in base32 instead of base16, which will make them about 20% smaller.

This answer is assuming you want to sacrifice collision resistance for length (seems dubious for anything that needs to be highly reliable) and that you don't want to use auto-incrementing. Assuming your UUIDs are sufficiently random, isn't this just a generalized birthday problem? Take the birthday problem and generalize the formula from 365 buckets to N buckets, where N is the total number of possible UUIDs given a length and set of possible characters. https://mste.illinois.edu/courses/mat764fa03/folders/edwards...

You should be concerned with what happens if there's a duplicate -- you can calculate the probability of that occurring as long as you know the potential size of your dataset and set your UUID length accordingly.

(comment deleted)
Do the values need to be secret? Not guessable/predictable?

If that's not a concern, just increment a number 1, 2, 3...N. Encode it in the URL in base-36 (alpha numeric).

It all depends on the requirements of your ID.

I have been using NanoID in JS, and it works well. It also has a variant that will check for rude words, in case the identifier is particularly user-visible.

https://github.com/ai/nanoid

It has a nice calculator for collision probability as well.