Design a production-ready service that:
-
Creates a short URL for a given long URL (URL shortening).
-
Redirects when a user visits the short URL.
-
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).