We will create two endpoints. The first one will encode the long URL. It will be a POST endpoint that will receive the long URL in the body of the request, and the backend will encode it, persisting it into both a cache layer and also in the database.
The second endpoint will be a GET endpoint that will redirect the user to the long URL in a 301 status code. If the URL is already in the cache layer, we redirect the user directly. If not, we will get the record in the database, persist it in the cache, and then redirect the user to the long URL.
The components are:
The load balancer will help us to redirect the request to a given server based on a strategy that we will define.
The cache layer will help us give a shorter response to the user, instead of needing to fetch the data from the database on every request. This will favour the most frequently accessed long URLs.
The multiple database servers will help us with the increase in the number of users and the amount of encoded URLs that will probably increase during the application growth.