Skip to content
Tech Interview Prep
Technical interview guide

URL Shortener Design

Designing a URL shortener: unique keys, redirect semantics, cache TTLs, click accounting off the GET path, and open-redirect abuse.

Minutes to read
4
Practice MCQs
25
Interview Q&A
25
Edition
v5
Editorial status
Reviewed

Scope: HTTP per RFC 9110 and RFC 6585; UUIDs per RFC 9562; uniqueness illustrated with PostgreSQL unique indexes and Redis GET; abuse controls per OWASP and Unicode TR36.

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

626≈5.68×1010,627≈3.5×101262^{6} \approx 5.68 \times 10^{10}, \qquad 62^{7} \approx 3.5 \times 10^{12}

P≈1−e−n(n−1)/(2N)P \approx 1 - e^{-n(n-1)/(2N)} 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.

10686400≈12 creates/s average,101086400≈116k redirects/s average\frac{10^{6}}{86400} \approx 12\ \text{creates/s average}, \qquad \frac{10^{10}}{86400} \approx 116\text{k redirects/s average}

A unique index on public_key gives O(log n) insert and lookup in Postgres. A Redis GET is O(1).

How it works

  1. POST /v1/links allocates a key and inserts it under a unique constraint. The database rejects duplicates; the application does not check first.
  2. GET /{key} reads the cache. On a miss it loads the row from Postgres, populates the cache, and returns 302 with Location.
  3. The redirect emits a click event to a queue and returns without waiting for it.
  4. 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.
Client Edge CDN Redirect service Redis cache Postgres links table Click queue Aggregator

When to use and when not to

FactorChooseAvoid
Destinations editable or expiring302 or 307, short shared cache, purge on write301, which browsers keep for up to a day
Creates per secondUnique index, bounded retry on conflictSharding before measuring the primary's write peak
Redirects per secondRedis plus edge cache, clicks asyncPer-click UPDATE on the mapping row
Untrusted destinationsScheme allowlist, Safe Browsing checkAny http(s) string
Internal tool at about 200 creates/weekOne table and one 302 handlerKafka and a key generation service

Traps interviewers probe

  1. 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.
  2. 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).
  3. 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.
  4. 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.
  5. 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.