GET api/v1/getShortUrl?long_url={long_url}
response
{
short_url: {short_url}
}
GET api/v1/redirect?short_url={long_url}
we should have two tables.
create table long_to_short{
long_url varchar(60),
short_url varchar(50),
PRIMARY KEY(long_url)
}
create table short_to_long{
short_url varchar(60),
long_url varchar(50),
PRIMARY KEY(short_url)
}
The user sends a post request with a long URL to a short URL generator server to get a short URL.
The generator will first check the cache and database to see if a corresponding short URL has been generated, if yes, return the result. If not, it will convert the long URL using 'snowflake id' algorithm to generate the the unique short URL and store it into the database and return the response
Both the generator and redirect servers are stateless which are easy to scale.
we are using SQL server because we dont have update to the existing records and it should have no concurrency issue.
if we experience high DB rps, we could create more replica of DB
The cache could be expired at the same time which could cause a spike in DB qps.
The current design has only a single generator server and redirect server, it may have a single point of failure.
The cache could be preloaded with hot URLs to reduce the workload.
both the generator server and redirect server are stateless, so it is easy to scale horizontally