How Does a URL Shortener Work?

ByteByteGoAbout 4 min readAug 1, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

URL Shorteners, Hashing, Base 62 Encoding, Collisions, Database Lookups, Caching, 301 Redirects, Database Sharding, Distributed Systems, Rate Limiting, Analytics, Security.

How URL Shorteners Work: A Deep Dive

1. The Core Functionality

URL shorteners like Bitly and TinyURL transform long URLs into shorter, more manageable links. Clicking these short links redirects users to the original, longer URL. The challenge lies in handling the massive scale and complexity behind this seemingly simple process.

2. Scale and Storage Requirements

  • URL Creation Rate: Approximately 100 million new URLs are shortened daily, translating to over 1,000 new short links per second.
  • Click Rate: The system handles over 10,000 clicks per second.
  • Storage: Over a decade, this amounts to 365 billion URLs, requiring approximately 36 terabytes of storage just for the URLs themselves.

3. Generating Short URLs: Two Approaches

3.1. Hashing

  • Process: This approach involves using a hash function (e.g., MD5) to generate a long string of random characters from the long URL.
  • Problem: Hash functions produce strings that are too long (e.g., 32 characters), requiring truncation to the desired short URL length (e.g., 7 characters).
  • Collisions: Truncation can lead to collisions, where different URLs produce the same short URL. This necessitates repeated attempts with variations of the original URL and extensive database lookups to ensure uniqueness.
  • Drawbacks: High database lookup overhead for collision detection.

3.2. Base 62 Encoding (Sequential Numbering)

  • Process: Assign a unique sequential number to each new URL. Convert this number to base 62 using the characters 0-9, a-z, and A-Z.
  • Example: URL number 11,157 is converted to base 62 as follows:
    • 11157 / 62 = 179, remainder 59
    • 179 / 62 = 2, remainder 55
    • 2 / 62 = 0, remainder 2
    • Reading the remainders backwards: 2, 55, 59.
    • In base 62: 2 -> 2, 55 -> t, 59 -> x.
    • Short URL: tinyurl.com/2tx
  • Advantages: No collisions, no database lookups for uniqueness.
  • Trade-offs: Requires a mechanism for generating unique numbers across multiple servers. Potential security vulnerability if the numbering pattern is predictable.

4. Handling Clicks and Redirects

  • Process: When a short URL is clicked, the system must retrieve the original URL and redirect the user.
  • Caching: First, check a cache for the mapping between the short URL and the original URL. If found, redirect immediately.
  • Database Lookup: If the mapping is not in the cache, query the database. Cache the result for future use.
  • 301 Redirect: Use a 301 status code to indicate a permanent redirect. This allows the browser to cache the redirect, potentially bypassing the URL shortener on subsequent visits.

5. Scaling the System

  • Database Replicas: A single database can handle approximately 10,000 lookups per second. Multiple database replicas are needed to distribute the load.
  • Database Sharding: Eventually, the data must be split across multiple databases (sharding). This requires strategies for even data distribution, request routing, handling database failures, and rebalancing when adding servers.

6. Additional Considerations

  • Rate Limiting: Prevent abuse by limiting the number of requests from a single user or IP address.
  • Analytics: Track click counts and other metrics for each short URL.
  • Security: Block malicious URLs and prevent the shortening of URLs that lead to harmful content.

7. Real-World Applications

  • Twitter: Shortens URLs in tweets.
  • Slack: Shortens URLs in messages.
  • Internal Tools: Many companies use URL shorteners in their internal tools.
  • Instagram: Uses similar ID generation techniques for photos.
  • Netflix: Uses similar caching strategies for video metadata.
  • Uber: Uses similar database splitting techniques for trip data.

8. Conclusion

URL shortening, while seemingly simple, involves complex engineering challenges related to distributed systems, caching, database scaling, and performance optimization. The techniques used in URL shorteners, such as unique ID generation, caching strategies, and database sharding, are widely applicable in various other systems and applications. The next time you click a shortened link, remember the intricate system working behind the scenes to efficiently redirect you.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.