Creating Short Unique IDs Similar to YouTube

Need help with generating compact unique identifiers

I’m working on a web app and I want to create short, unique IDs for my content. Something like what YouTube uses for their videos (example: N7Et6c9nL9w).

Does anyone know how to make these? They seem really handy and I’d love to use something similar in my project. Are there any libraries or techniques you’d recommend for this?

I’m not entirely sure if these are true GUIDs or a different type of identifier. Any guidance on how to implement something like this would be greatly appreciated. Thanks!

I’ve actually tackled this problem in a project. I ended up using a combination of the current timestamp and a random string. The idea is to take the current time in milliseconds and convert it to base36 to get a compact alphanumeric string, then append a short random sequence. This yields unique IDs that are also sortable, and it avoids the need for any extra libraries. Even though collisions are very unlikely, it’s wise to include a collision-check mechanism in your database. Let me know if you need further details.

For generating compact unique IDs similar to YouTube’s, you might want to consider using base64 encoding of UUIDs. This approach provides a good balance between uniqueness and brevity. Here’s a basic implementation in JavaScript:

function generateShortId() {
    const uuid = crypto.randomUUID();
    return btoa(uuid).replace(/[/+=]/g, '').substr(0, 11);
}

This function creates a UUID, encodes it to base64, removes non-alphanumeric characters, and truncates it to 11 characters. It’s simple, efficient, and produces YouTube-like IDs. Remember to handle potential collisions in your database, though it’s highly unlikely with this method.

hey there! i’ve used nanoid for similar stuff. it’s super easy to use and generates short, unique IDs. just npm install nanoid and you’re good to go. it’s way more compact than regular UUIDs. give it a shot and lemme know if you need any help implementing it!