Skip to main content

URL Shortener

1. Executive Summary​

A URL shortener maps long URLs to compact, shareable aliases and resolves those aliases with low latency on redirect. At principal level, the exercise tests whether you can separate write path (create short link, rare) from read path (redirect, dominant), choose an ID generation strategy without centralized bottlenecks, and reason about cache hierarchy, analytics, and abuse without over-engineering.

This chapter presents a reference design for a global shortener handling billions of redirects per day, custom domains, optional expiration, and click analytics—using explicit design phases (requirements through tradeoffs) mapped into the standard 30-section curriculum structure.

2. Why This Topic Matters​

URL shorteners appear in staff and principal loops because they compress many distributed-systems themes into a bounded problem:

  • Read-heavy skew — viral links dominate traffic; caching strategy is the design.
  • ID allocation — counters vs. hashes vs. snowflakes; collision and predictability tradeoffs.
  • Latency SLOs — redirects must be sub-10 ms at the edge; database on critical path is unacceptable.
  • Abuse and security — phishing, malware, SSRF via preview endpoints.
  • Multi-tenancy — enterprise custom domains and branded short links.

Production systems in this class include Bitly, TinyURL patterns, and internal link wrappers at large enterprises. Failures manifest as redirect outages (revenue and trust impact), ID collisions, or security incidents from unmoderated URLs.

3. Problems Being Solved​

ProblemCapability
Long URLs unwieldyCompact alias (e.g., 7-char base62)
Redirect latencyEdge cache + CDN; minimal hops
Unique alias generationDistributed ID or hash-with-retry
Custom brandingPer-tenant domain + namespace
AnalyticsAsync click stream; aggregation
Expiration / revocationTTL metadata; cache invalidation
AbuseURL blocklists, rate limits, safe browsing
DurabilityMapping survives restarts; replicated store

4. Assumptions and System Model​

Phase 1: Clarify Requirements​

Functional requirements (in scope):

  • Create short URL from long URL; optional custom alias, expiration, password.
  • HTTP 301/302 redirect on GET; support HEAD for metadata.
  • Optional analytics dashboard: clicks by time, referrer, geography.
  • API for programmatic creation (authenticated tenants).

Non-functional requirements:

  • Redirect p99 latency < 50 ms globally (edge); create p99 < 200 ms.
  • Availability 99.99% for redirect path.
  • Durability: no lost mappings once acknowledged.
  • Scale: 100M DAU, 10:1 read:write ratio, 10B redirects/month.

Non-goals (explicit):

  • Full web page preview rendering at redirect time.
  • Guaranteed exactly-once click counting (at-least-once + dedup acceptable).
  • Editing long URL after creation (immutable mapping v1).
AssumptionImplication
Reads dominateAggressive caching; write-optimized metadata store
Aliases are immutableCache forever with TTL for expiring links
Collision probability manageableBase62 length 7+ or counter-based IDs
Malicious URLs existScan/block pipeline on create
Partial failure normalRedirect must degrade to origin DB, not fail open to wrong URL

Failure model: Crash-stop servers, network partitions, cache misses—not Byzantine clients unless abuse is in scope.

5. Essential Terminology​

TermDefinition
Alias / slugShort path component (e.g., abc12XY)
Long URLOriginal destination
301 vs. 302Permanent vs. temporary redirect; affects SEO and cache
Base62Encoding using [A-Za-z0-9] for URL-safe IDs
Snowflake IDTime-ordered 64-bit ID from machine + sequence
Cache-asideApp loads cache on miss, populates from DB
Surrogate keyInternal ID distinct from public alias
Click streamAsync events for analytics
Custom domainTenant CNAME to shortener edge
Bloom filterProbabilistic "alias exists" check before DB

6. Core Mechanism​

6.1 Phase 5: High-Level Architecture​

Figure 1: URL shortener—redirect path optimized through CDN and Redis; create path writes durable store; analytics async.

6.2 Phase 3: Define APIs​

MethodEndpointPurpose
POST/v1/linksCreate short link; body: long_url, custom_alias?, expires_at?
GET/{alias}Redirect 302 to long URL
GET/v1/links/{alias}Metadata (auth); no redirect
DELETE/v1/links/{alias}Revoke; purge cache
GET/v1/links/{alias}/statsAggregated clicks

Create response: 201 with short_url, alias, created_at. Idempotency: Idempotency-Key header for create retries.

Redirect: 302 Found + Location; 404 if unknown/expired; 410 Gone if revoked.

6.3 Phase 4: Model Data​

links table (sharded by alias_hash):

ColumnTypeNotes
aliasVARCHAR(16) PKPublic slug
long_urlTEXTNormalized URL
tenant_idUUIDMulti-tenant
created_atTIMESTAMP
expires_atTIMESTAMP NULL
statusENUMactive, revoked
long_url_hashBINARY(32)Dedup optional

custom_domains: domain, tenant_id, verified, DNS challenge state.

Analytics (columnar / OLAP): alias, timestamp, referrer, country, user_agent_hash.

Cache key: link:{alias} → JSON {long_url, expires_at, status} TTL = min(expires_at - now, 24h).

6.4 Phase 6: Deep Dives​

ID generation strategies:

StrategyProsCons
Global counter + base62No collisions; sortableCounter SPOF; needs range allocation per shard
Hash (MD5/SHA) truncateStatelessCollisions; retry loop
Snowflake / UUIDDistributedLonger URLs; not human-friendly
Pre-generated ID poolsFast createPool management overhead

Recommended: Range-based counters per shard (e.g., 1000 IDs/sec per DB shard) encoded base62; collision check on custom aliases.

Redirect path (critical):

  1. CDN edge: cache Location header keyed by full short URL path.
  2. On miss: redirect service checks Redis (GET link:{alias}).
  3. On Redis miss: read DB replica; populate Redis; respond.
  4. Fire-and-forget click event to Kafka (never block redirect).

Custom domain: TLS cert via ACME; route by Host header to tenant namespace; alias uniqueness scoped per domain.

Figure 2: Redirect sequence—CDN and Redis absorb read load; DB only on cold miss.

6.5 Expiration and revocation​

Revoke sets status=revoked, deletes Redis key, CDN purge via API. Expired links: lazy check on read + background job; CDN TTL must be < min time-to-expiry for active links or use shorter edge TTL.

7. Step-by-Step Walkthrough​

  1. API client POSTs long URL with API key.
  2. Normalize URL (lowercase host, strip fragments per policy).
  3. Safe-browsing scan queue; reject if malware.
  4. Allocate alias from shard counter; encode base62.
  5. INSERT into links; return https://short.example/abc12XY.
  6. Optional: warm Redis with mapping.

7.3 Multi-region read after create​

  1. User in EU creates link; write goes to primary in eu-west.
  2. Async replication to us-east read replica lag 200 ms.
  3. US user redirects before replication: cache miss, replica miss → brief 404 possible.
  4. Mitigation: read-after-write for creator session routes to primary; redirects use replicas with "eventual" OK; or global primary with higher latency.
  5. Principal decision: document consistency model in API contract.

7.4 Analytics pipeline backpressure​

  1. Viral link generates 2M clicks/sec to analytics topic.
  2. Kafka partition saturates; consumers lag minutes.
  3. Mitigation: edge sampling (1%); aggregate counters in CDN; dedicated high-partition topic per viral detection.
  4. Product accepts approximate counts for trending; exact billing uses sampled extrapolation with confidence intervals.

7A. Design Phase Summary​

PhaseSectionKey decisions
Requirements§4Read-heavy; immutable mappings; async analytics
Scale§1019K+ peak RPS redirect; CDN first
APIs§6.2Idempotent create; 302 redirect
Data model§6.3Sharded links; Redis cache
Architecture§6.1Edge → Redirect → Redis → DB
Deep dives§6.4Counter+base62; hot key CDN
Reliability§8–9HA DB; cache degrade path
Security§13SSRF block; safe browsing
Operations§12Revoke purge runbook
Tradeoffs§16301 vs 302; hash vs counter

8. Invariants and Guarantees​

PropertyGuaranteeMechanism
UniquenessOne alias → one long URL (per domain)PK on alias; custom alias transactional insert
SafetyNo redirect to unmapped URL404 on miss; no default fallback
DurabilityAcknowledged create persistsSync replicate DB before 201
AnalyticsAt-least-once click eventsKafka retries; idempotent consumer
RevocationEventually invisibleCache purge + TTL bound

Not guaranteed: exactly-once click counts; global strong consistency on read-after-write across regions (eventual for cache).

9. Failure Scenarios​

ScenarioImpactMitigation
Redis downHigher DB loadCircuit breaker; read replicas; brief stale OK for 301
DB shard unavailablePartial 404sHA replicas; failover
Counter shard exhaustedCreate failuresPre-allocate ranges; alert
CDN misconfigurationStale redirects after revokePurge API; short TTL
Hash collision (custom)Create conflictRetry with suffix; user message
Kafka backlogDelayed analyticsScale consumers; sample clicks
DDoS on short domainEdge saturationWAF, rate limit, anycast

10. Performance Characteristics​

Phase 2: Estimate Scale​

DAU = 100M
Creates per user/month = 2 → 200M creates/month ≈ 80 creates/sec avg
Redirects per create = 50 → 10B redirects/month
Peak factor = 5×
Peak redirect RPS ≈ (10B × 5) / (30 × 86400) ≈ 19K RPS average peak bursts higher on viral

Storage: 200M new links/month × 500 bytes ≈ 100 GB/month metadata
Read:write = 50:1 → design for redirect path first
DimensionTargetDriver
Redirect p99< 50 msCDN hit ratio > 95%
Create p99< 200 msDB write + scan
Cache hit> 90% originTTL, hot keys
CDN hit> 80% globalGeographic distribution

11. Scalability Limits​

LimitCauseMitigation
Hot key in RedisViral aliasLocal cache; read replicas of cache; split analytics
Counter coordinationGlobal sequenceShard ranges; snowflake
DB write rateMass create APIBatch; async provisioning
Custom domain TLSCert per hostShared wildcard + SAN limits; automated ACME
Analytics ingestClick firehoseSample; edge aggregation

Figure 3: Multi-region reads with single write primary—acceptable eventual consistency for redirects; custom domains may need regional routing.

12. Operational Considerations​

Phase 9: Operations​

  • SLOs: Redirect availability 99.99%; create 99.9%.
  • Dashboards: RPS, cache hit ratio, p99 latency, create error rate, Kafka lag.
  • Alerts: DB replication lag; Redis memory; CDN 5xx; abuse spike.
  • Runbooks: Purge alias from all caches; failover DB; block malicious tenant.
  • Deployments: Redirect service canary—read-only; no schema change on redirect path without dual-write.

13. Security Considerations​

Phase 8: Security​

  • Open redirect abuse: Allowlist schemes (https only); block internal IPs (SSRF).
  • Phishing: Safe browsing API; user report; rapid takedown.
  • Enumeration: Non-sequential IDs; rate limit GET on unknown aliases.
  • Auth: API keys per tenant; OAuth for dashboard.
  • Custom domains: DNS verification before activation.
  • Audit: Log creates/deletes with actor.

14. Cost Considerations​

DriverLever
CDN egressCache 302 aggressively; minimize redirect chain hops
Redis memoryTTL; evict cold aliases; compress values
DB storageArchive expired links to cold tier; partition by date
KafkaSample analytics; tiered retention
Safe browsing APICache scan results by URL hash
Custom domain TLSAutomate ACME; wildcard where policy allows

Unit economics: cost per million redirects and per thousand creates—compare to enterprise contract value. At 10B redirects/month, 1% origin miss at 19K RPS peak can still overwhelm DB—CDN misconfiguration is a cost and availability incident.

14A. Evolution and Migration Path​

Phase 1 (MVP): Single region, counter IDs, Redis + PostgreSQL, no custom domains.

Phase 2: CDN integration, analytics pipeline, abuse scanning.

Phase 3: Multi-region reads, custom domains, enterprise API tier.

Phase 4: Global active-active writes only if business requires sub-second global create visibility—most products do not.

Each phase should have measurable exit criteria (e.g., Phase 2: CDN hit ratio > 90% for 7 days).

15. Production Implementations​

SystemPattern
BitlyCustom domains, analytics, enterprise API
Internal wrappersPolicy enforcement, SSO, audit
CloudFront + Lambda@EdgeRedirect at edge from KV store

16. Alternatives and Tradeoffs​

Phase 10: Tradeoffs​

DecisionOption AOption B
Redirect code301 (cache forever)302 (easier revoke)
IDCounterHash
DBSQL shardedDynamo-style KV
AnalyticsSync writeAsync stream
Multi-regionRead replicasActive-active writes (conflict risk)

17. Common Misconceptions​

MisconceptionReality
"Use MD5, take 6 chars, done"Collisions; security; predictability
"DB can handle all reads"19K+ RPS needs cache/CDN
"301 is always better"Harder to change destination
"Count every click exactly"At-least-once + dedup is standard
"Count every click exactly"At-least-once + dedup is standard
"One global counter is fine"Bottleneck at scale
"Short URLs never expire"Storage and abuse grow unbounded
"Analytics in sync path is fine"Destroys redirect latency SLO
"Custom domain is just DNS"TLS cert lifecycle and routing complexity

18. Principal Architect Perspective​

  • Optimize the read path first—interview time spent on create is often misallocated.
  • State explicit consistency for cache vs. DB on revoke.
  • Separate analytics from redirect critical path.
  • Plan for hot keys before they happen (viral links).
  • Custom domains multiply operational complexity—phase them.
  • Document consistency model for multi-region reads explicitly in API docs.
  • Analytics sampling is a product decision—define accuracy SLO with stakeholders.
  • Capacity planning: model viral factor separately from average RPS; CDN contract limits matter at extreme scale.

18.1 Organizational implications​

Short links often become critical infrastructure embedded in marketing campaigns, SMS, and printed materials. A principal architect aligns platform teams with marketing on revoke policy (302 vs 301), establishes abuse response SLAs with security, and ensures legal review of terms for phishing liability. Migration from legacy shortener requires redirect compatibility and dual-run period—treat as data migration, not greenfield deploy.

19. Architecture Review Exercise​

Scenario: Team stores full redirect in PostgreSQL and queries on every GET. Expected 50K RPS.

Review: Calculate DB capacity (~5K QPS per beefy replica); identify 10× gap; propose CDN + Redis; estimate hit ratios; discuss revoke propagation.

20. Whiteboard Explanation​

"Users create links via an API that normalizes URLs, scans for abuse, and assigns a base62 alias from a sharded counter. The mapping lives in a sharded database. Redirects hit CDN first; on miss, a stateless redirect service reads Redis, then DB on cache miss, returns 302, and asynchronously publishes click events. Revocation deletes cache entries and purges CDN. The design is read-optimized with at-least-once analytics."

21. Interview Questions​

  1. Design a URL shortener for 100M DAU. — Signals: read/write split, CDN, ID strategy, sharding. Follow-up: custom domains. Red flags: DB on every redirect.
  2. How generate unique short IDs at scale? — Signals: sharded counters, base62, collision handling. Red flags: single MySQL auto-increment.
  3. 301 vs. 302 redirect—when which? — Signals: cache semantics, SEO, revoke ease. Red flags: "always 301."
  4. Handle a viral link with 1M RPS? — Signals: CDN 302 cache, analytics decoupling, local micro-cache. Red flags: scale Redis single key.
  5. Custom domain support architecture? — Signals: Host routing, ACME TLS, per-domain namespace. Follow-up: cert renewal ops.
  6. Ensure a revoked link stops working quickly? — Signals: cache purge API, short TTL, 410 Gone. Red flags: "delete DB only."
  7. Detect and block phishing URLs? — Signals: safe browsing, blocklists, report flow. Red flags: no abuse model.
  8. Multi-region deployment strategy? — Signals: write primary, read replicas, consistency caveats. Follow-up: read-after-write for creator.
  9. Analytics without slowing redirects? — Signals: async Kafka, sampling, edge aggregation. Red flags: sync DB increment on redirect.
  10. Collision handling for hash-based IDs? — Signals: retry, lengthen suffix, birthday paradox math. Red flags: ignore collisions.
  11. API idempotency for create? — Signals: Idempotency-Key header, unique constraint. Follow-up: TTL on keys.
  12. Estimate storage for 5 years of links? — Signals: BOE with growth rate, compression, archival tier.

22. Interview Follow-Ups​

  1. User wants to change destination URL. — Immutability vs. versioned mapping; cache invalidation across CDN/Redis; legal implications for marketing campaigns already printed.
  2. Predictable sequential IDs leak business metrics. — Random IDs; opaque snowflakes; rate limit enumeration.
  3. GDPR delete request. — Purge DB, cache, analytics; audit trail retention policy; right-to-erasure vs. fraud investigation hold.
  4. Enterprise wants private shortener. — Dedicated shard; VPC; no shared cache keys; custom SLA and support tier.
  5. How monetize analytics? — Aggregate trends without PII; differential privacy considerations; contract with enterprise tenants.
  6. Compare to redirect service inside API gateway. — Dedicated service scales independently; gateway redirect lacks analytics and custom domain depth.

22A. Production Low-Level Design (Interview + Game Day)​

Per Production LLD and Game Day Standard.

C4 context​

Step-by-step flow:

StepInteractionExplanation
1–2Edge hitViral link never storms origin
3–6OriginCache-aside Redis → DB
7AnalyticsOff redirect critical path
8–9Write / revokeInvalidate edge + Redis

Production stack​

LayerAWS / genericPurpose
EdgeCloudFrontCache 302; geo
RedirectECS / Lambda@EdgeResolve alias
CacheElastiCacheHot aliases
DBAurora / DynamoAlias → URL
ClicksKinesisAsync analytics
IDsCounter ranges / snowflakeUnique short codes

API contract (snippet)​

POST /v1/links
Idempotency-Key: …
{"long_url":"https://…","ttl_sec":86400}
→ {"alias":"aB3xY9q","short_url":"https://s.ex/aB3xY9q"}

GET /aB3xY9q → 302 Location: https://…

Data model​

EntityKeyNotes
linksalias PKlong_url, status, expires
idempotencycreate keySame alias on retry
Click eventsstreamat-least-once

Happy-path sequence​

Failure / revoke race​

SLOs (one-liner)​

SLOTargetHow achieved
Redirect p99 (edge)< 50 msCDN hit
Origin RPS viralFlatEdge cache
Create p99< 200 msSharded ID + DB
Revoke visibility< 60 sPurge API

22B. Game Day Playbooks​

Catalog​

#DrillFrequencySLO
GD-1Viral single aliasQuarterlyOrigin RPS flat
GD-2Redis flushQuarterlyDB holds with CDN
GD-3Revoke + stale CDNMonthlyPurge SLO
GD-4ID collision / range exhaustSemi-annualAlert + allocate
GD-5Phishing create floodQuarterlyRate limit + scan
GD-6Analytics pipeline downQuarterlyRedirect still works

GD-3 — Revoke​

Pass criteria: No 302 to bad destination after purge SLA; audit row written.

22C. Capacity Planning Worksheet​

InputValueNotes
Monthly creates200MDrives metadata storage
Avg redirects/create50Viral tail separate model
Peak RPS multiplier5–20×CDN contract sizing
Bytes per link row~500Index overhead extra
Redis memory per hot key~200 BJSON + overhead
CDN cache hit target85–95%Origin protection

Principal candidates should complete this table aloud in interviews, stating which assumption dominates cost (usually CDN egress for redirect-heavy analytics beacon endpoints, or Redis if CDN bypassed).

23. Strong Answer Example​

Question: "How handle 10M redirects/sec on one popular link?"

Answer this as a 45-minute design discussion using the PRACTICE framework. Strong students put the CDN/edge cache on the critical path and decouple analytics—not “MySQL behind a load balancer.”

Figure A: PRACTICE map — walk the board in this order.


Student interview script (PRACTICE)​

P — Problem framing​

"One celebrity link at 10M redirects/sec will melt any origin if every hit hits Redis/DB.

Short answer: CDN caches the 302 with appropriate Cache-Control. Click analytics are async/sampled. Hot key is replicated at edge—not sharded away. Monitor origin RPS as the SLO, not just edge latency."

R — Requirements​

Create, redirect, revoke, async analytics, abuse controls.

A — Assumptions​

Immutable mapping v1; 302 preferred for revoke; at-least-once clicks OK.

C — Capacity​

CDN hit 90%+ on viral → origin sees ≤10%
Never put sync DB increment on redirect path

T — Topology​

Edge → Redirect → Redis → DB; Kafka clicks; create API separate.

I — Invariants​

Unique alias; revoke invalidates all cache layers; create idempotent.

C — Constraints​

Phishing, SSRF on previews, GDPR delete, custom domains.

E — Evolution​

Single region → CDN → Redis → shard DB → custom domains → chaos viral drill.

Closing line:

"Viral traffic is an edge caching problem; keep analytics and DB off the redirect hot path."

Scoring signals​

StrongWeak
CDN + Cache-ControlLB + MySQL only
Async clicksSync counter on redirect
Purge on revokeIgnore stale CDN

Compact outline​

"CDN caches 302; Redis cache-aside; async Kafka clicks; purge on revoke; origin RPS SLO."

24. Weak Answer Example​

Weak: "Store in MySQL and use a load balancer."

Red flags: No cache, no CDN, no ID strategy, no abuse, no read/write split.

25. Hands-On Exercise​

  1. Build create + redirect API with SQLite.
  2. Add Redis cache-aside on redirect.
  3. Simulate viral traffic; measure DB QPS with/without cache.
  4. Implement revoke with cache purge.
  5. BOE: storage and RPS for stated assumptions.
  6. Extension: Implement consistent hash for multi-shard simulation; measure key distribution.
  7. Extension: Add idempotency table for create API; verify duplicate POST returns same alias.
  8. Written deliverable: One-page ADR choosing 302 over 301 with revoke and SEO tradeoffs documented.

25A. Mock Interview Timing (60 min)​

MinutesActivity
0–8Requirements and scale BOE
8–15APIs and data model
15–30Architecture diagram + deep dives (CDN, cache, IDs)
30–40Failure scenarios and security
40–50Tradeoffs and evolution phases
50–60Questions for interviewer; ops/SLO summary

23A. Additional Strong Answer​

Q: Walk through ID generation at scale.

Outline: Assign each DB shard a range of integers from a coordination service (ZooKeeper/etcd). On create, shard atomically increments local counter and encodes to base62. Custom aliases use transactional INSERT with uniqueness check. At 7 chars base62 (~3.5T space), random hash collision risk is low until billions of URLs—still verify for custom slugs.

26. Knowledge Check​

  1. Why cache-aside on redirect path?
  2. Name two ID generation approaches.
  3. What happens on Redis miss?
  4. Why async click logging?
  5. How enforce alias uniqueness for custom slugs?
  6. 301 vs 302 cache behavior?
  7. What is a hot key problem?
  8. How prevent open redirect vulnerabilities?
  9. What is the read:write ratio assumption driving architecture?
  10. When would you choose hash-based IDs over counters?
  11. How does CDN purge interact with revocation SLO?
  12. What metadata belongs in an idempotency record for create?

Answers in §6–§8; review Phase Summary table §7A before mock interviews.

27. Flashcards​

FrontBack
Base62URL-safe encoding for compact IDs
Cache-asideApp manages cache populate on miss
302 redirectTemporary; easier to update destination
Hot keySingle key receiving disproportionate traffic
Snowflake IDDistributed time-ordered identifier
Idempotency-KeySafe retry for create API

28. Cheat Sheet​

REQUIREMENTS: create link, redirect, analytics async, custom domain optional
SCALE: read-heavy; CDN + Redis + sharded DB
APIs: POST /links, GET /{alias} → 302
DATA: links(alias PK, long_url, expires, status)
ARCH: Edge → Redirect → Redis → DB; Kafka for clicks
DEEP: counter+base62 IDs; hot key = CDN + local cache
RELIABILITY: DB HA; degrade Redis to DB
SECURITY: SSRF block, safe browsing, rate limits
OPS: cache purge on revoke; SLO on redirect p99
TRADEOFFS: 301 vs 302; hash vs counter

30. References​

  • Kleppmann, M. (2017). Designing Data-Intensive Applications. O'Reilly — caching, partitioning.
  • RFC 7231 — HTTP redirect semantics (301, 302, 307, 308).
  • Twitter snowflake ID pattern — distributed ID generation (implementation reference).

Distinction: HTTP redirect semantics are normative (RFC); ID and cache choices are implementation tradeoffs.