Traffic Estimation
Data Estimation
1) API to generate the short URL
Request
POST /v1/url
{
url: "some_long_url"
}
Response
{url: "some_short_url"}
2) API to get the long URL
Request
GET /v1/url/{hash}
Response
{url: "some_long_url"}
We will use MySQL db to store the records having schema
hash - varchar(5) - primary key
long_url - varchar(255) - unique key
is_active - tinyint
created_at - datetime
expiry - datetime
```
flowchart TD
Client["Client"];
APIServer1["API Server 1"];
APIServer2["API Server 2"];
APIServer3["API Server 3"];
APIGateway["API Gateway"];
LB["Load Balancer"];
DB[("DB")];
ES[("ES")];
Analytics["Analytics"];
Client --> | Send URL | APIGateway;
APIGateway --> | Rate Limited Req | LB
LB --> APIServer1
LB --> APIServer2
LB --> APIServer3
APIServer1 --> | Logs | ES;
APIServer2 --> | Logs | ES;
APIServer3 --> | Logs | ES;
DB --> APIServer1;
DB --> APIServer2;
DB --> APIServer3;
APIServer1 --> DB;
APIServer2 --> DB;
APIServer3 --> DB;
APIGateway --> | Response | Client;
ES --> Analytics;
```
Let's talk about the hash generating mechanism.
String md5Str = md5(long_url);
String base62Str = Base62(md5Str);
String hash = base62Str[:6];
MySQL vs NoSQL, we used MySQL as opposed to NoSQL that may look very prominent candidate here. But the fact the use case is just limited to fetching based on the primary key and URL, and that it takes less memory in MySQL as compared to NoSQL, I chose MySQL.
Scaling up MySQL can be a bit challenging as we may need to use replication using active-passive to start with and then it may need a combination of active-active and active-passive because of huge read requests.
I choose servers in the autoscaling group at AWS so that whenever the spikes of requests happen, the system will be able to handle it based on the load by increasing or decreasing the number of servers.
The failure scenario could be that there's a sudden spike in the request flow and the AWS autoscaling group does not get a chance to boot up new servers fast resulting to throttled requests for some users.
Since we're using a single LB and API Gateway, they pose a single point of failures and may need to be scaled up just like the others.
The Application Server can be enhanced to check for quota for a short URL based on the number of clicks in a day or overall. This data can be store through Analytics in some database so that API Servers can request to it.