We will have a simple schema in a relational DB.
Primary key: shortURL
other columns: longURL
We will need a DB to store the shortURL, longURL pairs.
We will need a cache to improve the read latency.
Write flow:
The client sends a long URL for shortening. The service first checks if the long URL is already in the database. For this we require the long URL to be part of the index. We can have a secondary index for this.
If the long URL is not present, the service interacts with the UUID generator to get the monotonically increasing ID.
The service then converts the ID to a base64 number and adds it to the Database.
Read flow:
The client receives a short URL.
The client checks if the short URL is present in the cache or database.
If so, the long URL is returned with status code 301
Otherwise an appropriate response it given to the client to say that the short URL does not exist in the system
For the UUID generation, we can use the Twitter snowflake ID generation. This provides unique and monotically increasing IDs.
We will need to convert the IDs to base 64 and store.
Explain any trade offs you have made and why you made certain tech choices...
Try to discuss as many failure scenarios/bottlenecks as possible.
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?