Requirements
Functional Requirements:
- Create a short URL for a given long URL.
- Return the long URL associated with a given short URL.
Non-Functional Requirements:
- High availability
- Low Redirect Latency
- Horizontal Scalability
API Design
RedirectUrls => /redirectUrls
/redirectUrls/{originalUrl}
/shorten
High-Level Design
We have redirect urls service which is main component for generating redirect urls and shortener service to create short version of original long url. Api gateway in case we need to create more services. Cdn to deliver traffic very fast to users in different locations. Load balancer for incoming traffic distribution. Rate limiter for overload protection. Analytics service for gathering information from our shortener and redirect services.
High-Level Design
We have users who wants to generate redirect urls for their long url. To create redirect urls we will need redirect urls service which will create redirect urls, Shortener service which will generate shortened versions of url and sql database to store urls. Since one of non functional requirement is high availability we would use replication pattern of databases as well as replication of services with load balancer. To decrease latency we will use api gateway with caching mechanism as well as cdn to deliver trafic instantly to customers in different locations. If there are too many data to store we would use sharding mechanism and make shards based on original URL for storing urls. We would also include Rate limiter to protect services from overloading. Id generation should be handled by database itself as uuid of postgres for example. To decrease latency we would use cache layer such as redis or memcached. If time to live expires or there is no cache entry system should fetch data for cachin and return loaded data which would create longer response for first request. If there is update request cache should be invalidated for get requests. To manage cache size effectively system should utilize Least Recently used policy for ttl policy at first and then for maximum efiiciency i would monitor cache hit ratios and adjust ttl based on analysis. To make analytics possible we should create analytics service which would gather information from our shortener and redirect services.