create urls like Instagram posts or youtube video

Viewed 66

I'm building a social network and I're going to create my own urls at random the question that has been on my mind for a long time is how to create urls like Instagram posts like Django like the following:

https://www.instagram.com/p/CeqcZdeoNaP/

or

https://www.youtube.com/watch?v=MhRaaU9-Jg4

My problem is that on the one hand these urls have to be unique and on the other hand it does not make sense that on a large scale when the number of uploaded posts by the user is more than 100,000 I set unique = True Because the performance of the database decreases

Another point is the use of uuids, which solves this problem of uniqueness to a large extent, but the strings produced by uuid are very long, and if I shorten these strings and reduce the number of letters in the string, there is a possibility of a collision. And that several identical strings are produced

I wanted to know if there is a solution to this issue that generated urls are both short and unique while maintaining database performance

Thank you for your time

1 Answers

You might choose to design around ULIDs. https://github.com/ulid/spec

It's still 128 bits. The engineering tradeoff they made was 48 bits of predictable low-entropy clock catenated with an 80-bit nonce.

Starting with a timestamp makes it play very nicely with postgres B-trees.

They serialize 5 bits per character instead of the 4 bits offered by hex. You could choose to go for 6 if you want, for the sake of brevity.

Similarly you could also choose to adjust the clock tick granularity, and reduce its range.

Keeping the Birthday Paradox in mind, you might choose to use a smaller nonce, as well. The current design offers good collision resistance up to around 2^40 identifiers per clock tick, which might be overkill for your needs.

Related