31. Design a URL Shortener
1
The interview prompt2
31.1 What do you ask first?3
31.2 How big is this, really?4
31.3 What are the doors?5
31.4 What do we store?6
31.5 What does the whole system look like?7
31.6 How do you generate the short code?8
31.7 301 or 302?9
31.8 How do you keep redirects fast at 12,000 a second?10
31.9 How do you count clicks without slowing redirects?11
31.10 What would you say in the last five minutes?12
Score yourself13
Variations to practise14
Sources15
Code lab31. Design a URL Shortener
31.3 What are the doors?
3 min read+10 XP
Step 3: API and data model (10 minutes)
POST /api/links
body: { "long_url": "https://example.com/some/very/long/page" }
returns: 201 Created
{ "code": "aB3x9Qz", "short_url": "https://sho.rt/aB3x9Qz" }
errors: 400 if long_url is not a valid http(s) URL
429 if this client is creating too many links (Chapter 25)
GET /{code}
returns: 302 Found, Location: https://example.com/some/very/long/page
errors: 404 if the code doesn't exist
GET /api/links/{code}/stats
returns: { "code": "aB3x9Qz", "clicks": 18213, "updated_at": "..." }