POST /createURL?destination=example.com/longurl
GET shortener.com/abc123
We will have a key-value store to store the mappings of short URL to original URL.
We use a stateless web tier to make horizontal scaling easy. We use a load balancer to avoid overload of web tier servers. We use a key-value store database because the most important query is getting a value for a key and it's easier to scale such databases compared to relational databases.
We use a cache to store frequently-used URLs to reduce load on the database.
The client's requests are sent to a load balancer which chooses a stateless Web tier service to handle the request. If it is a read request, it first checks the URL cache to see if the long URL is there. If not, it queries the URL database and then writes the answer to the cache. The read query is completed by returning a redirect to the client. For write requests, it converts the URL to a short URL and adds that key and value to the URL database.
Short URL algorithm: In order to make URLs short, we encode the URL as a number with base 62, using a-z and A-Z for digits larger than 9.
For performance scalability and reliability, the load balancer and web tier will be replicated, and the URL Database will be replicated and sharded by short URL hash. The sharding can be done with consistent hashing to make it easier to add or remove servers.
Since we have a simple deterministic algorithm to produce the short URL for a URL, this may have some security concerns if malicious users can predict the short URL for a URL.
A load balancer could go down. It would be replaced by a replica. Similar for web tier.
If a URL cache replica goes down, it could be served by other replicas. If the primary goes down, it could be replaced by promoting a replica. Similar for URL database.
We can add a service to do analytics on links. We can asynchronously invoke that service any time a link is used, so we can avoid adding latency. Analytics can be done in a lakehouse. If the link owner needs to see the analytics in a dashboard, we could make additional infrastructure for serving that surface.