Overview
A URL shortening service built as a system design exercise, implementing the core features of services like Bitly and TinyURL. The system generates short, unique URLs, handles high-throughput redirects with sub-10ms latency, tracks click analytics, and supports custom aliases.
Problem
URL shorteners appear simple on the surface but involve interesting engineering challenges: generating unique short codes at scale without collisions, handling massive read-heavy traffic (100:1 read-to-write ratio), providing real-time analytics, and ensuring expired links are properly cleaned up.
Architecture
- Write Path: API receives long URL → generates base62-encoded short code from a distributed ID generator → stores mapping in PostgreSQL → caches in Redis
- Read Path: Short URL request → Redis cache lookup (99% hit rate) → PostgreSQL fallback → 301/302 redirect → async analytics event
- Analytics Pipeline: Click events are batched and written asynchronously to avoid impacting redirect latency
- Rate Limiter: Token bucket algorithm per API key, backed by Redis
Tradeoffs
- Base62 encoding over hashing: Deterministic ID-based encoding avoids collision handling complexity at the cost of predictable URLs
- 301 vs 302 redirects: Configurable per URL — 301 for permanent links (better for SEO) and 302 for trackable marketing links
- Async analytics over sync: Click counting is eventually consistent but redirect latency stays under 10ms
Implementation
- Distributed ID generation: Snowflake-inspired ID generator with datacenter and worker IDs, producing 7-character base62 codes
- Read-through cache: Redis caches URL mappings with a 24-hour TTL, refreshed on access
- Link expiration: Background job scans for expired links and removes them from the cache and database
- Custom aliases: Users can request specific short codes with uniqueness validation
Challenges
- Preventing enumeration attacks — sequential IDs make it easy to guess valid short codes, mitigated by adding random salt to the encoding
- Handling redirect chains and circular references
- Database hotspotting on popular short URLs during viral sharing events
Future Improvements
- QR code generation for shortened URLs
- A/B testing support with multiple destination URLs
- Geographic redirect routing based on user location
- Browser extension for one-click shortening