Design a URL shortener like bit.ly that handles 100 million new links per month and many more redirects. Cover IDs, storage, caching and scale.
What the interviewer wants to hear
- Estimate scale: read-heavy, storage size, QPS
- Short ID generation: counter with base62, or random with collision check
- Key-value style storage, indexed by short code
- Heavy caching and CDN for redirects; 301 vs 302 trade-off
- Analytics asynchronously via a queue; abuse protection
Model answer
At 100 million links a month, writes are around 40 per second on average, but redirects may be 100 times more, so the system is read-heavy. Over five years that is about 6 billion links; with around 500 bytes each, a few terabytes. For IDs, 7 base62 characters give about 3.5 trillion combinations. I would use a distributed ID generator, for example ranges of counter values handed to each server, then base62 encode, which avoids collisions. Random IDs also work with a unique constraint and retry. Storage is a simple key-value lookup by short code, so DynamoDB, Cassandra or sharded PostgreSQL fit. Redirects go through Redis and a CDN since popular links are hot. I would use 302 if we need click analytics, because 301 is cached by browsers. Click events go to Kafka and are aggregated asynchronously. I would add rate limiting and malware URL checks.
Can you answer it under time pressure?
Take a timed hard round and get scored on every answer.
More hard Backend Developer questions
- A product manager says each payment event must be processed exactly once. How do you achieve that with real message queues and networks?
- Your single PostgreSQL database is reaching its limits. How would you scale reads and then writes, and what problems does each step bring?
- In a microservices system, placing an order touches the Order, Payment and Inventory services, each with its own database. How do you keep data consistent?
- You are building a B2B SaaS backend used by many companies. How would you design multi-tenancy and make sure one tenant can never see another tenant data?
- Design a notification service that sends email, SMS and push messages for many products, up to tens of millions of messages a day.
- Your company wants to split a large monolith into microservices. How would you approach it, and when would you argue against it?
- Design the backend for a real-time chat app with one-to-one and group chats, online status and message history.
- One slow downstream dependency caused your whole platform to go down. How would you handle the incident, and what would you change afterwards?
- How would you design a ledger for a wallet or payments product so balances are always correct?