Design a URL Shortener
Design a service like TinyURL. Discuss requirements, API design, database schema, and handling high traffic.
Why Interviewers Ask This
Meta asks this to evaluate your ability to architect scalable, distributed systems under constraints. They specifically test your understanding of hash collision handling, database sharding strategies for massive read-heavy workloads, and how to balance latency against consistency when billions of requests hit the service daily.
How to Answer This Question
1. Clarify Requirements: Define scope like 'shorten URL' vs 'redirect', estimate QPS (e.g., 10 million/sec), and storage needs for Meta's scale. 2. API Design: Propose REST endpoints like POST /shorten and GET /{code} with status codes. 3. Core Algorithm: Discuss generating unique IDs using Base62 encoding on a random string or a counter incremented across shards. 4. Database Schema: Explain storing key-value pairs in a NoSQL store like Cassandra or DynamoDB, emphasizing partitioning by user ID or hash range. 5. Scalability & Caching: Detail using Redis for hot redirects to reduce DB load and CDN integration for global low-latency access.
Key Points to Cover
- Explicitly defining non-functional requirements like QPS and latency targets before coding
- Choosing between random generation vs sequential counters based on collision probability
- Justifying NoSQL over SQL for horizontal scaling and write throughput
- Implementing a robust caching layer (Redis/CDN) to protect the backend database
- Addressing specific Meta-scale challenges like massive data volume and global distribution
Sample Answer
To design a URL shortener for Meta's scale, I'd first clarify requirements. We need high availability, sub-100ms latency for reads, and support for billions of URLs. The API would expose two endpoints: POST /shorten to cā¦
Common Mistakes to Avoid
- Focusing too much on the algorithm while ignoring database sharding and partitioning strategies
- Assuming a single database instance can handle billions of rows without discussing scalability
- Neglecting the impact of caching on consistency and TTL management for expired links
- Forgetting to discuss error handling scenarios like duplicate URLs or code collisions
Sound confident on this question in 5 minutes
Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free ā no signup needed.