Skip to content
We areTOP 1%

Design a URL shortener

MediumConceptWeb Services
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

Requirements: create a short alias for a long URL, redirect quickly, and optionally support custom aliases, expiry and click analytics. Traffic is read-heavy, often around 100 redirects per URL created.

  • Key generation: encode a unique numeric ID in base62; seven characters give about 3.5 trillion keys. IDs can come from a distributed counter or from ranges pre-allocated to each server, which avoids the collision checks that hashing the long URL would require.
  • Storage: a key-value or wide-column store mapping short key to long URL, which shards easily by key.
  • Read path: cache hot keys in Redis in front of the database, so most redirects never reach storage.
  • Trade-off: a 301 (permanent) redirect lets browsers cache it and cuts load, while a 302 routes every click through your servers and enables analytics.

More in Web Services

Web Services questions
#ProblemPlatformDifficultyOpen1st1st Visit2nd2nd VisitRevRevise
1Design a URL shortener
MediumConcept
ConceptMedium Read the answer to Design a URL shortener
2Design a pastebin service
MediumConcept
ConceptMedium Read the answer to Design a pastebin service
3Design a rate limiter
MediumConcept
ConceptMedium Read the answer to Design a rate limiter
4Design a notification system
HardConcept
ConceptHard Read the answer to Design a notification system

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account