You are asked to build a URL shortener for a company that sends payment reminders by SMS. Every character in an SMS costs space, so the short code should be as short as possible. Let us work out how short, and then how to produce the codes, because the second question is where the interesting trade-offs hide.
Step one: how many codes can a given length hold?
Use an alphabet of digits plus lowercase plus uppercase letters: 10 + 26 + 26 = 62 symbols. This is called base62. A code of length L can take 62 to the power L values. I computed these:
| Length | Possible codes |
|---|---|
| 5 | 916,132,832 (about 916 million) |
| 6 | 56,800,235,584 (about 56.8 billion) |
| 7 | 3,521,614,606,208 (about 3.5 trillion) |
If you expect, say, 100 crore (1 billion) links over the product’s life, six characters suffice with a sequential scheme, since 56.8 billion is far more than 1 billion. Seven gives enormous headroom and, as we will see, is needed for random schemes. Compare with hexadecimal: seven hex characters hold only 268 million values. Base62 packs far more into the same length.
Step two: three ways to generate a code
Hash the long URL and truncate
Run the URL through a hash such as MD5 or SHA-256, encode, take the first seven characters. Appealing: the same URL always yields the same code, so duplicates are free. The problem is that truncation throws away most of the hash, so two different URLs can share a prefix. You must check the database on every insert, and on collision rehash with a salt appended. It works, but a hit costs extra round trips, and you give up the property that made it attractive once salts get involved.
Random strings
Draw seven random characters. Unpredictable, so people cannot guess neighbouring links. Collisions are possible, so insert with a uniqueness constraint and retry on conflict. How often? When the store already holds 1 billion codes out of 3.52 trillion possible, a fresh random code collides with probability about 1 billion divided by 3.52 trillion, which is 0.028 percent. About one insert in 3,500 retries once. Cheap. But at 1 billion codes in six characters (56.8 billion possible) it would be around 1.8 percent, and the rate rises as you fill the space. That is why random schemes want more length than sequential ones.
Counter, encoded
Keep a global counter. Each new link takes the next integer and writes it in base62. Number 125 is “21”, because 125 = 2 x 62 + 1. Number 1,000,000 is “4c92”. No collisions ever, no retries, no lookup before write. Codes grow in length only as the counter grows, so early links are short. This is the most efficient scheme, with two catches.
- Guessable. Anyone who sees code “4c92” can try “4c93”. If short links point to private documents, that is an enumeration leak. A common fix is to scramble the counter with a reversible permutation or a keyed bijection before encoding, so output looks random but remains collision-free.
- One counter is a bottleneck. A single database sequence becomes the hot spot when many app servers write at once. The usual answer is to hand each server a block (server A takes 1 to 100,000, server B takes 100,001 to 200,000) and let it issue numbers locally. Crashes waste unused numbers in a block, which does not matter given the size of the space. Snowflake-style IDs, with timestamp plus machine ID plus sequence, are another form, but they are 64-bit numbers whose base62 form is longer than seven characters, so they suit systems where short length is less critical.
Custom aliases
Marketing will ask for vanity links like /diwali-offer. Keep them in the same table with a flag, enforce uniqueness with the same constraint, and reserve a namespace so that a vanity alias can never clash with a generated code. Forgetting this makes someone’s campaign link silently overwrite another.
Reads dominate
Creating a link happens once; following it happens many times, often in bursts after an SMS blast. So the read path is a key-value lookup from code to long URL, a perfect fit for a cache in front of the database. A small hot set of links takes most traffic, so even a modest cache gives a high hit rate. Think about the redirect status too. A 301 says “permanent” and browsers may cache it, which reduces load but means you never see repeat clicks, so analytics undercount. A 302 forces every click through your server, which costs more but lets you count and later change the target. For anything with click tracking or expiry, 302 is the safer default.
Things teams forget
- Remove look-alike characters (0 and O, 1 and l) if people will type codes from a printed page. This reduces the alphabet, so recompute the length.
- Filter generated codes against an offensive-words list. Random seven-character strings do eventually spell something unfortunate.
- Decide expiry and deletion up front. Reusing a retired code means an old SMS now points to a stranger’s destination.
- Short links are a favourite hiding place for phishing. Scan destinations, and rate-limit creation per account.
My pick for the reminder SMS product: block-allocated counters, encoded in base62, passed through a keyed permutation. Six characters will serve for years, nothing collides, and nobody can walk through other customers’ links.
Figures and names in the examples are illustrative.
Leave a Reply