
How URL Shorteners Actually Work Under the Hood
A URL shortener maps a long URL to a short code, stores that mapping, and redirects anyone who visits the short URL to the original. The concept is trivial. The implementation has interesting engineering decisions at every step. The core data model short_code -> original_url That is the entire data model. A key-value store. A hash map. A database table with two columns. The engineering challenges are in generating the short code, handling collisions, and scaling the redirect. Generating short codes The short code needs to be short (obviously), unique, and ideally URL-safe. The standard approach uses base62 encoding (a-z, A-Z, 0-9), which gives 62 possible characters per position. A 6-character base62 code has 62^6 = 56.8 billion possible values. A 7-character code has 3.5 trillion. For most use cases, 6 characters is more than sufficient. Method 1: Auto-incrementing ID. Assign each URL an incrementing integer ID (1, 2, 3...) and base62-encode it. ID 1 becomes "1", ID 62 becomes "10", I
Continue reading on Dev.to
Opens in a new tab



