As asked
Design a URL shortener service that stores 10 billion URLs, handles 100K redirects per second, and generates unique 7-character short codes. Walk me through storage, ID generation, and caching.
Sample answer outline
For ID generation: base62 encoding of a counter from a distributed ID generator (Snowflake or a Postgres sequence with a hash). Storage: Postgres or DynamoDB for the mapping table; 10 billion rows at ~200 bytes each is around 2 TB, which is manageable with sharding on the short code. Redirects at 100K/s require Redis caching of the hot URLs; the cache hit rate for a short-URL service is very high because popular links get most of the traffic. Read path: check Redis, miss goes to DB, populate cache with TTL. Analytics: async write to a Kafka topic from the redirect service. The candidate should discuss eventual consistency for analytics vs strong consistency for URL creation.
Expect these follow-ups
- How do you prevent the cache stampede when a newly-viral URL is first accessed by millions of users?
- How would you implement custom slugs (like bit.ly/mylink) and handle slug collisions?