Part 6 of 7 · 3 min read
Trade-offs
Abuse, analytics and growth.
Trade-offs and wrap-up
Abuse. A malicious user could send huge numbers of shorten requests to fill the database or to hide harmful links. A rate limiter (module 1) filters by IP address or other rules. Many services also check long URLs against lists of unsafe sites, and let users report a bad link.
Scaling the web tier. It is stateless, so more web servers can be added behind the load balancer.
Scaling the database. Replication for reads and safety, and sharding for size, as in Level 4.
Analytics. To count clicks and see when and where they come from, use 302 and write a small event for each redirect, off the main path, to a queue (Level 6). The redirect should not wait for the analytics write.
Availability and consistency. The redirect path matters most, and it can accept a very short delay for new links to appear everywhere. The shorten path must never give the same code twice, which the ID generator guarantees. A short lag between a new link and its availability on every replica is usually fine, and a retry on "not found" in the first few seconds hides it.
Link expiry and deletion. The book assumes neither. If links expire, add an expiry column, check it on redirect, and sweep old rows in the background.
Common mistake
Taking the first 7 characters of a hash and calling it done. Collisions are likely well before the system is full, and the design needs a stated way to detect and repair them, or a counter-based code that cannot collide.
Interview angle
Answer in your head first, then open it.
How long is the code, and why?
7 characters of base 62, since 62^7 is about 3.5 trillion and 365 billion are needed.
Hash or counter?
A counter in base 62 avoids collisions, at the cost of guessable codes. A hash needs a collision check.
301 or 302?
301 lowers load, 302 allows counting every click.
How does it handle reads?
A cache in front of a sharded database, since reads are ten times writes.
What else would you add?
Rate limiting, analytics through a queue, and safe-link checks.
One follow-up to try. "Users can pick their own custom short names." A good reply: add a second path that checks the requested name against the same table, refuses it if taken, and reserves names that would clash with generated codes, for example by forcing custom names to a different length or prefix. The check must be atomic with the insert, or two users could claim the same name.
Check yourself: 1. Why should analytics writes not block the redirect? 2. How can short links be made hard to guess? 3. In the custom-name follow-up, why must check and insert be one step?
Words you will meet
| Word | Meaning |
|---|---|
| Alias | A short name that stands for a longer address |
| Base 62 | Writing numbers with 62 symbols: digits, lower and upper case letters |
| Collision | Two different inputs getting the same code |
| 301 and 302 | Redirect codes: permanent and temporary |
| Bloom filter | A compact check that rules out most absent keys |
| Read-heavy | Many more reads than writes |