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.
- shortURLsvc: How to shorten URL? We can hash the url. Then, we can encode the hash with base 64 encoding to have better readability. We can take the first 6 characters of that base 64 encoding. Ex: user enters thisismycoolwebsiteletsgo.com. Our service would hash the url. Then encodes with base 64 and we would get a long string like abc32456..4332. We would then take the first 6 characters of that encoding. So final URL, would look like tinyurl/abc324.com. So in our database we would store abc324.com -> thisismycoolwebsiteletsgo.com.
- Con: We can get duplicate shortened url's. To fix this, we can keep a counter whenever we get a duplicate or rely on our backend process to get unique keys to shorten urls.
- redirectionURLsvc: it just looks into the database to find the shortened url and get the original url as a value. If it is not in cache, we store it in the cache. In order for not having stale data, we can have a TTL on the url in the cache.
- how to handle cache or database misses? If the url is not in cache, look for the shortened url in database. If the url is not in database, return an error saying that user needs to create a valid shortened url.
- how to handle TTL expiry of cache? First, we want to have the expiration date of the shortened url to be greater than the TTL expiry of cache. When shortened url expires, it should also have expired from the cache. To handle TTL expire of cache, we can expire the urls with the highest times that they have not been accessed.
- backend process: These unique keys will be stored in database and they can have a field as not used(meaning that it has not been assigned as a short url. It isn't mapping to any url). This is useful when we don't want to perform a calculation like hashing and then encoding, let's just take a unique key and use that as a shortened url.
- con: we would need to keep track of the keys not being used as urls
Another drawback of approach: is not straightforward to reuse shortened url's that have expired if a user is keeping track of url stats.