HACKER Q&A
📣 tosh

IDs that are URL-friendly and fairly short?


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.


  👤 davidajackson Accepted Answer ✓
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.


👤 asplake
If 32 bit keys are enough, skip32 [1] and then alphanumeric encoding [2] on an incrementing integer key. Both of those are reversible so you won't suffer from collisions and you can get back your original integer key if you want.

In python:

[1] https://github.com/pyrige/skippy/blob/master/skippy/skippy.p...

[2] https://gist.githubusercontent.com/adyliu/4494223/raw/0e21a0...


👤 tdeck
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.


👤 gurgeous
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.

👤 Foober223
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.




👤 alexmingoia
Auto-incremented integers encoded in base32.

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


👤 zzo38computer
I think it will depend on the application. Some things will require longer IDs than others will use.