Explained step by step
A URL shortener, like Bitly or TinyURL, creates a short alias for a long link. When someone visits the short link, it redirects them to the original. For example, a 60-character link becomes bit.ly/3I71d3o, with 7 characters after the slash. It saves space when sharing, gets mistyped less, and lets you track each link separately. It's like a nickname: «Bob» is shorter than «Robert James Smith», but everyone knows who you mean.
Creating a link: the API server asks the key generation service (KGS) for a unique key, which marks it as used; the server stores the link in the database and cache, and answers HTTP 201 (created). Visiting a link: the request reaches the server, it looks in the cache first, and if it's not there, in the database; if found, it answers HTTP 301 (redirect) to the original; if not, HTTP 404. It's like a coat check: when you leave your coat you get a number; when you come back, they look up the number and hand it over.
Functional requirements: given a URL, generate a short, unique alias; redirect to the original when visited; and links expire after a default timespan. Non-functional: high availability with minimal latency, and it must be scalable and efficient. Extended: prevent abuse and record redirection metrics. It's like a client's order to a carpenter: which piece of furniture (functional), how sturdy (non-functional), and whether to add a drawer (extended).
With a 100 to 1 ratio and 100 million new links per month: about 40 writes per second and 4,000 reads per second. If each request weighs 500 bytes, 20 KB per second come in and about 2 MB per second go out. Keeping each link 10 years, that's 12 billion records: about 6 TB. It's like calculating a year of stock before renting a warehouse.
For the cache you use the Pareto principle, or 80/20 rule: 80% of requests go to 20% of the data, so caching 20% is enough. With 4,000 redirects per second, that's about 350 million requests per day. 20% of 350 million times 500 bytes gives about 35 GB of memory per day, in Redis or Memcached. It's like an ice cream shop: 80% of people order 3 of the 15 flavors, so those go up front.
The API has 3 functions: createURL(apiKey, originalURL, expiration?) returns the short link; getURL(apiKey, shortURL) returns the original; deleteURL(apiKey, shortURL) returns whether it was deleted. All take an API key, which identifies the caller and lets you limit them to a certain number of requests per second or minute: that covers the abuse-prevention requirement. It's like a club membership card: you get in with it, and the club knows how many times you came.
To make the shortener withstand failures: run several instances of the servers and the KGS; put load balancers between clients, servers, databases and caches; use several read replicas, since it reads much more than it writes; keep a standby replica of the key database; and several instances and replicas of the distributed cache. You can also add an API gateway for authorization, rate limiting and load balancing. It's like a hospital: it has a power generator, 2 entrances and more than 1 doctor on call.