Overview
Curated: · Written: · Reviewed:
URL Shortener Design
In one line
A URL shortener is a durable map from a unique public key to a long URL, served as GET /{key} with a 302 and a Location header. Reach for this design whenever a question forces you to settle key uniqueness, redirect caching, click accounting and abuse controls together.
Key definitions
- Public key: the slug GET /{key} looks up. It carries a unique index, and nothing else depends on how it was minted.
- Idempotent create: a retried POST returns the same key for the same owner and destination, keyed by an Idempotency-Key header or a unique (owner_id, destination_hash).
- Negative cache: a 404 entry with a TTL of 5 to 30 s, short enough that a just-created key is not hidden.
- Click event: an append-only record emitted off the GET path and aggregated later. It is never an UPDATE on the mapping row.
- 302 vs 301: 301 is permanent and browsers cache it for long periods. 302 and 307 are temporary, so edits and takedowns take effect.
- Open redirect: a shortener that 302s any http(s) URL, letting attackers borrow your domain's trust.
Formulas and complexity
where N is the key space and n is the number of keys issued. For N = 62^6, collisions become likely near n = sqrt(N), about 2.4 × 10^5 keys.
A unique index on public_key gives O(log n) insert and lookup in Postgres. A Redis GET is O(1).
How it works
- POST /v1/links allocates a key and inserts it under a unique constraint. The database rejects duplicates; the application does not check first.
- GET /{key} reads the cache. On a miss it loads the row from Postgres, populates the cache, and returns 302 with Location.
- The redirect emits a click event to a queue and returns without waiting for it.
- A miss on an unknown key caches a 404 for 5 to 30 s. A successful insert deletes that negative entry in the same region.
When to use and when not to
| Factor | Choose | Avoid |
|---|---|---|
| Destinations editable or expiring | 302 or 307, short shared cache, purge on write | 301, which browsers keep for up to a day |
| Creates per second | Unique index, bounded retry on conflict | Sharding before measuring the primary's write peak |
| Redirects per second | Redis plus edge cache, clicks async | Per-click UPDATE on the mapping row |
| Untrusted destinations | Scheme allowlist, Safe Browsing check | Any http(s) string |
| Internal tool at about 200 creates/week | One table and one 302 handler | Kafka and a key generation service |
Traps interviewers probe
- SELECT then INSERT. Two writers both see a miss and one fails with a 500. Fix: unique index on public_key, INSERT ... ON CONFLICT, bounded retry for random keys.
- Truncated hash as the key. 62^6 is the key space, not the collision rate. Collisions appear near 2.4 × 10^5 keys, and the same URL gets two codes. Fix: allocate a unique counter or random key, and dedupe on (owner_id, destination_hash).
- Counting clicks on the mapping row. UPDATE clicks = clicks + 1 serializes the hottest keys, which are the ones that must be fastest. Fix: append click events and roll them up in windows.
- startsWith('https://') plus includes('yourbrand.example'). https://yourbrand.example.evil.example passes both tests. Fix: parse the URL with a URI library, require https, reject userinfo, and match the exact host or a dot-bounded suffix.
- Always 301 for SEO. Edits and takedowns then fail for anyone who already resolved the key. Fix: use 302 or 307 unless the mapping is truly immutable.
Interview tips
Opening. The product is a unique key-to-URL map plus a redirect policy. I will fix the uniqueness rule and the status code before choosing a hash.
Expected follow-ups. How do retries avoid duplicate keys? When is 301 acceptable? How do clicks leave the GET path? How do you stop open redirects? What changes at 10x the create rate? Which regions can create links?
30-second outline. Store key to URL in Postgres with a unique public_key. Allocate keys from a counter or a random draw in the public alphabet, and retry on conflict. Serve GET /{key} from Redis with a 302 and a short cache, with a 10 s negative TTL. Send clicks to a queue and roll them up later. Validate destinations: https only, parsed host, Safe Browsing before the first redirect. Quote about 12 creates/s and 116k redirects/s average, and say which number drives the design.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.