Skip to Content
Course content

417: Building a URL Shortener Backend

Click on the "Edit" button in the top corner of the screen to edit your slide content.

When people first try to build a URL shortener, they almost always start with the same approach: they generate a random 6-character string using random.choices(), check if it's already in the database, and if it is, they just loop and try again. It feels intuitive. It feels "random."

Random Strings Aren't Unique Enough

Here is why that approach is a trap. Let's say you're using a character set of 62 characters (A-Z, a-z, 0-9). With a 6-character string, you have about 56 billion possibilities. That sounds huge, right? But because of the Birthday Paradox, the probability of a collision happens much faster than your intuition tells you. Once you hit a few million URLs, your "generate and check" loop starts hitting existing keys more often, slowing down your write operations exactly when your app is becoming successful.

# The "naive" way that eventually slows down
import random
import string

def generate_short_url(existing_urls):
    chars = string.ascii_letters + string.digits
    while True:
        code = ''.join(random.choices(chars, k=6))
        if code not in existing_urls:
            return code

I've seen this kill the performance of early-stage startups. You don't want your database to be the bottleneck just because you're guessing random strings and hoping for the best.

The Elegance of Base-62 Encoding

The professional way to handle this is to stop guessing and start calculating. Instead of treating the "short code" as a random string, treat it as a base-62 representation of a database ID.

Think about it: your database already gives you a unique, incrementing integer as a primary key (1, 2, 3...). If you convert that integer from base-10 to base-62, you get a unique, short string every single time. No collisions, no loops, and no unnecessary database queries to check if a code exists.

def encode(num):
    # Our alphabet: 0-9, a-z, A-Z
    alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    if num == 0:
        return alphabet[0]
    
    arr = []
    base = len(alphabet)
    while num:
        num, rem = divmod(num, base)
        arr.append(alphabet[rem])
    
    arr.reverse()
    return ''.join(arr)

# Example: Database ID 100,000 becomes 'g6s'
print(encode(100000)) 

This is significantly more efficient. You're essentially just changing the "language" the number is written in. It's deterministic, meaning ID 100,000 will always be 'g6s', and you'll never have two different IDs mapping to the same short code.

Mapping IDs to Short Codes in the Backend

Now, the actual flow of your backend changes. Instead of generating a code and then saving the URL, you save the URL first, get the auto-incremented ID back from the database, and then encode that ID to create your short link. When a user visits your.sh/g6s, you simply reverse the process: decode 'g6s' back into the integer 100,000, and fetch the record with that ID.

I'll admit, it feels slightly less "magical" than random strings, but in production, predictability is your best friend. You can calculate exactly how many URLs you can support before you need to add another character to your short code. For instance, a 7-character base-62 string can handle over 3.5 trillion URLs. That's plenty for most of us.




📋 Practical Task

Build a Bidirectional Base-62 URL Encoder/Decoder

Your task is to implement the counterpart to the encode function. You need to create a decode function that takes a base-62 string (the short code) and converts it back into its original integer database ID.

Requirements:

  • Use the same alphabet: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".
  • The decode function should take a string and return an integer.
  • Test your implementation to ensure that decode(encode(123456)) returns 123456.
def encode(num):
    alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    if num == 0: return alphabet[0]
    arr = []
    base = len(alphabet)
    while num:
        num, rem = divmod(num, base)
        arr.append(alphabet[rem])
    arr.reverse()
    return ''.join(arr)

def decode(code):
    # TODO: Implement this function
    pass

# Test your code here
test_id = 987654321
encoded = encode(test_id)
decoded = decode(encoded)
print(f"Original: {test_id} -> Encoded: {encoded} -> Decoded: {decoded}")
print(f"Success: {test_id == decoded}")
Rating
0 0

There are no comments for now.

to be the first to leave a comment.