Lesson 13 of 15
31. Design a URL Shortener

Variations to practise

3 min read+10 XP

Chapters used

Approach (1), Estimation (2), Load balancers (7), SQL vs NoSQL (8), Unique IDs (10), Caching (12, 13), Replication (14), Message queues (22), APIs (24), Rate limiting (25), Failures (29).

Try these variations

Each is a common follow-up. Answer in two minutes, then check the sketch.

  1. "Add custom aliases." → Store them in the same table; the alias is the code. Check it's free before inserting (the primary key does this) and keep aliases out of the generated-code space, for example by requiring at least one character a generated code can't have, or a minimum length.
  2. "Links should expire." → Add expires_at. Check it on redirect (return 404 or 410 when expired), set the cache TTL no later than the expiry, and run a cleanup job.
  3. "Make it work across two regions." → Each region gets its own ID blocks so codes never clash, writes go to one primary or are replicated between regions, and DNS sends users to the nearest region (Chapter 4).
  4. "10 times the traffic." → Recalculate: 120,000 redirects a second at peak. Add Redis nodes (shard by code, Chapter 13), more replicas or a CDN for redirects. Writes are still small.