URL shortening services rely on three core technical components: short code generation, database storage, and HTTP redirection. This article explains how short URLs are generated and how users are forwarded to the original destination, with actual HTTP response examples.
When you submit a long URL, the service generates a unique short code. Several approaches exist for this step. Random alphanumeric string generation, extracting a portion of a hash (such as SHA-256) from the original URL, and encoding sequential IDs in base-62 format are the most common methods. Base-62 encoding uses 62 characters: digits 0-9 (10), lowercase a-z (26), and uppercase A-Z (26). For example, the sequential number 100000 converts to "q0U" in base-62 (just 3 characters). A 6-character code provides 62^6 = approximately 56.8 billion possible combinations, ensuring sufficient uniqueness even for large-scale services. Hash-based methods can generate the same code for the same URL, which is useful for deduplication, but require collision handling when different URLs produce the same hash prefix. Sequential methods avoid collisions entirely but produce predictable codes, requiring security considerations to prevent enumeration attacks.
The mapping between the short code and the original URL is stored in a database. High-performance NoSQL databases like DynamoDB are commonly used because URL shorteners are read-heavy workloads. Many services also implement a caching layer using Redis or Memcached to improve response times. Large-scale services place a CDN like CloudFront in front of the origin server, caching redirect responses at edge locations to deliver redirects with single-digit millisecond latency worldwide. However, when using CDN caching with 301 redirects, careful TTL (cache duration) design is essential. If you change the destination URL while the cache remains, users will continue to be forwarded to the old URL until the cache expires. This is a critical tradeoff between performance and flexibility. For a deeper understanding of these systems, database architecture books on Amazon provide excellent technical coverage.
When a user clicks a shortened URL, the server performs an HTTP redirect. The actual redirect response consists of HTTP headers like: "HTTP/1.1 301 Moved Permanently", "Location: https://example.com/very/long/original-page?utm_source=campaign", "Cache-Control: max-age=86400", "Content-Length: 0". The browser reads the Location header and automatically navigates to the specified URL. There are two primary redirect types: 301 (permanent) and 302 (temporary). A 301 redirect is cached by the browser, making subsequent visits faster but preventing the server from tracking repeat clicks. A 302 redirect passes every click through the server, enabling precise analytics. Most URL shortening services default to 302 redirects to prioritize tracking accuracy.
You can verify redirect behavior directly using the curl command in your terminal. Running "curl -v -L https://short.url/abc" displays the response headers including the HTTP status code and Location header. The "-I" flag retrieves headers only, which is convenient for inspecting redirect chains.
The complete flow from DNS resolution to redirect completion works as follows. The browser resolves the short URL domain via DNS, obtaining the server's IP address (typically 10-50ms). It then establishes an HTTPS connection through a TLS handshake (50-100ms) and sends an HTTP GET request to the server. The server looks up the short code in the database or cache to retrieve the original URL (1-10ms), then responds with a redirect status code and the original URL in the Location header. The browser navigates to the destination through a similar DNS resolution and connection process. When using a CDN, the entire sequence from initial DNS resolution to receiving the redirect response completes in approximately 20-50 milliseconds.
Choosing between redirect types involves a tradeoff based on your use case. For permanent links where SEO matters, 301 redirects are preferred. Google officially confirms that link equity (SEO value) passes through 301 redirects. For marketing use cases where click tracking accuracy is the priority, 302 redirects are the better choice. Some services offer the option to select the redirect type on a per-link basis, giving you maximum flexibility.
Recommended reading: For a deeper dive into web technologies and HTTP, browse related books on Amazon.