Design short URL service with click counting
Company: Snapchat
Role: Software Engineer
Category: System Design
Difficulty: hard
Interview Round: Technical Screen
Design a production-ready service that:
1. Creates a short URL for a given long URL (URL shortening).
2. Redirects when a user visits the short URL.
3. Maintains a **click counter** (number of times each short URL was visited), queryable via an API.
### Functional requirements
- `POST /shorten` takes a long URL and returns a short code + short URL.
- `GET /{code}` redirects (HTTP 301/302) to the long URL.
- `GET /stats/{code}` returns:
- total click count
- optionally breakdown by time bucket (e.g., per day) if feasible
### Non-functional requirements
- Very low latency for redirects.
- High availability.
- Must scale to high QPS (both reads and writes).
- Prevent obvious abuse (spam/DoS).
### Clarifications to address
- Custom aliases? Expiration?
- Idempotency (same long URL returns same code or always new)?
- Consistency expectations for click counts (exact vs eventually consistent).
Quick Answer: This question evaluates the ability to design scalable, highly available web services encompassing URL shortening, redirect semantics, API design, click-counting and analytics, plus related trade-offs in consistency, data modeling, and operational concerns.