List functional requirements for the system (Ask interviewer if stuck)...
List non-functional requirements for the system...
The service should be scalable, low latency, highly available.
The system should have proper authentication and authorization.
Ensure data durability and high throughput.
The service should contain monitoring and logging system.
Estimate the scale of the system you are going to design...
Assume the service generates 100 shortened URLs per second, everyday 8640000 URLs will be generated.
Assuming the URLs expire 1 year after it is generated. We will have approximately 8640000 * 365 ~ 10^7 * 400 = 4 * 10 ^ 9 urls
Assume an average URL takes 100 bytes to store and each short URL takes 20 bytes to store. The metadata of a Url pair would be around 80 bytes. The total size of storage will be approximately 4 * 200 * 10 ^ 9 = 8 * 10 ^ 11 bytes or 8 * 10 ^ 2 GB of storage.
Assume every URL get clicked 10 times a day, that would make it 4 * 10 ^ 10 clicks a day, and 4 * 10 ^ 5 clicks per second.
Define what APIs are expected from the system...
Defining the system data model early on will clarify how data will flow among different components of the system. Also you could draw an ER diagram using the diagramming tool to enhance your design...
The system use one table only
Schema:
id: (auto increment) (PK)
shortUrl: varchar
url: varchar
owner: varchar # userId
expirationDate: datetime
numberOfClicks: int
You should identify enough components that are needed to solve the actual problem from end to end. Also remember to draw a block diagram using the diagramming tool to augment your design...
The system should contain:
Explain how the request flows from end to end in your high level design. Also you could draw a sequence diagram using the diagramming tool to enhance your explanation...
Dig deeper into 2-3 components and explain in detail how they work. For example, how well does each component scale? Any relevant algorithm or data structure you like to use for a component? Also you could draw a diagram using the diagramming tool to enhance your design...
Explain any trade offs you have made and why you made certain tech choices...
Using randomly generated urls for its simplicity, trading for the chance of generating duplicates of short urls and have to re-run. It is chosen over hash function for simplicity, and over incremental id for security
Try to discuss as many failure scenarios/bottlenecks as possible.
If a server fails - we should have duplicate servers to serve traffic and ensure availability of the service.
If a database server fails - as we've discussed, we should have backup database machine to serve data.
If cache fails - we will fallback to database, quickly spin up a new cache machine, and have the cache being filled from database again.
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?
If traffic increases and more urls are introduced, we will eventually have a database that cannot be fitted into a signal machine. In that case, partitioning could be introduced. Consistent Hashing can be used to balance the load.