medium general · part of Practice Questions · Senior SWE Roadmap
Requirements to clarify
- Functional: shorten a long URL, redirect a short URL to the original, optional custom aliases, optional expiration.
- Non-functional: read-heavy (redirects far outnumber creations), low-latency redirect, high availability, IDs should be hard to guess (if not sequential).
Core components
- ID generation: base62-encode a counter (from a Unique ID Generator / DB auto-increment) into a short string, or hash the URL and truncate (handling collisions).
- Storage: a key-value store (short code → long URL) — read-heavy, so a cache (see Caching Strategies) in front of the DB is essential.
- Redirect service: on GET /{code}, look up the long URL and issue a 301/302 redirect.
- Custom aliases: a uniqueness check against existing codes before insert.
Key tradeoffs
- 301 (permanent, cacheable by browsers, less traffic to your service) vs 302 (temporary, lets you track every click) redirects.
- Counter-based IDs are shorter and collision-free but predictable/sequential; hash-based IDs are less guessable but need collision handling.