Software Engineer
harddesign-url-shortener
How would you design a URL shortening service like Bitly?
Answer
A good URL shortener design covers requirements, data model, and scaling.
**Requirements:** create short links, redirect quickly, analytics (optional), high availability.
**Key design choices:**
- **ID generation:** Base62 encode a unique ID (Snowflake/sequence) to avoid collisions.
- **Storage:** Key-value mapping `shortKey -> longUrl` (plus createdAt, expiry, owner).
- **Caching:** Cache popular keys in Redis to reduce DB reads.
- **Redirects:** Use 301 (permanent) or 302 (temporary) depending on needs.
- **Scale:** Partition by `shortKey`, add read replicas, and front with CDN for edge redirects.
**Risk areas:** abuse/spam, link expiration, and analytics write amplification.
Related Topics
System DesignScalabilityDatabases