Scope
Functional
- Create a short URL for a long URL; optionally a custom alias and expiry.
- Redirect short URL → long URL.
- Basic click analytics per link (counts by day, country, referrer).
Non-functional
- Redirect latency p99 < 50 ms (server side); very high availability for redirects.
- Codes must be unique and not trivially guessable.
- Read-heavy: ~100 redirects per creation.
Out of scope: user accounts beyond API keys, link-in-bio pages, QR codes.
Size
- 100M new links/month → ~40 creates/s (peak ~200).
- Redirects 100× → ~4,000/s average, ~40k/s peak (viral links).
- Record ≈ 500 B → 50 GB/month → ~6 TB in 10 years: one sharded-later relational or KV store handles it.
- 7 base62 characters = 3.5 trillion codes ≫ 12B links over 10 years.
- Click events: 4,000/s × ~200 B ≈ 70 GB/day of raw analytics.
Decision: the redirect path is everything. Optimise reads with caches and edge; writes are trivial.
APIs and data model
POST /v1/links {long_url, custom_alias?, expires_at?} → {code, short_url}
GET /{code} → 302 Location: long_url
GET /v1/links/{code}/stats?from=&to= → aggregated counts
DELETE /v1/links/{code}| Table | Key | Fields |
|---|---|---|
| links | code (PK) |
long_url, owner_id, created_at, expires_at, status |
| click_rollups | (code, day) |
total, by_country (map), by_referrer (map) |
The architecture
The redirect path: CDN → redirect service → Redis → DB. The click is published asynchronously to Kafka; the redirect never waits for analytics.
The 30-second summary
"A read-optimised service: codes from leased ID ranges, scrambled and base62-encoded, so they're unique with no per-request coordination. Redirects are 302s served from CDN and Redis, with clicks published asynchronously to Kafka and rolled up per minute. The main trade-off is 302 over 301: more traffic to us in exchange for accurate analytics."
Follow-up questions the interviewer may ask
- How do you support custom aliases without colliding with generated codes?
- How would you make it multi-region?
- How do you delete a link that's cached in CDNs and browsers?
- What if analytics consumers fall behind for an hour?
Model answers to each of these are in the book.