Detailed Component Design
Deep dive into 2-3 key components. Explain how they work, how they scale, discuss tradeoffs, capacity, and any relevant algorithms or data structures.
- For each URL being input to get the shortened URL, we need to check if it already exists in the system. Most frequently used will be kept in Redis with some set key expiration time.
- If the URL is not present in cache or DB, the ID will be assigned by API server and base62 of that ID will be used to generate shortened URL. How the ID will be generated is discussed below.
- API servers will also handle any requests to access shortened URLs by redirecting them to the actual long URL.
- We can use Apache zookeeper to give a range of IDs to each API server. If a new URL comes, each API server can assign it the required ID and use it to generate shortened URL. This will not allow any collision.
- For scalability, we will need to have read replicas for DB and multiple Redis nodes. We can use consistent hashing to distribute the keys in Redis.