Define the APIs expected from the system. This is your chance to analyze and define the read and write paths so that you can come up with the high-level design...
Domain name: trnc.io
Available endpoints:
POST trnc.io/api/v1
{
url: 'https://codemia.io"
}
returns {url: 'trnc.io/api/v1/cmio'}
GET trnc.io/api/v1/cmio
302 redirect to https://codemia.io
GET trnc.io/api/v1/analytics
(internal analytics use)
API Gateway (Edge Tier) This is the single entry point for all client traffic. It handles rate limiting to prevent abuse or denial of service attacks. It also manages load balancing, routing incoming traffic to the appropriate backend service based on the request endpoint.
Write Path (Shorten URL Service) This service handles POST requests to create new short links. Instead of generating IDs on the fly, it requests a precomputed unique string from the Key Generation Service. It pairs this short string with the original long URL and saves the record to the primary MongoDB database.
Read Path (Retrieve URL Service) This service handles GET requests for redirection. It is separated from the write service so it can scale independently to handle the massive read volume. It checks the Caching Layer first. If the short URL is found, it immediately returns a 302 redirect. If it is a cache miss, it fetches the data from a secondary MongoDB database, updates the cache, and then redirects the user.
Key Generation Service (KGS) A dedicated background service that precomputes millions of unique random base62 strings. It stores these unused strings in a separate database table. The KGS assigns small blocks of these precomputed keys to the Write Path servers. This guarantees that IDs are hard to guess, prevents database collisions entirely, and keeps the write process extremely fast.
Caching Layer (Redis) An in-memory cache placed between the Retrieve URL Service and the databases. URL shorteners are extremely read-heavy. Storing the most frequently accessed URLs in Redis prevents the database from being overwhelmed and ensures the redirection latency stays as low as possible. When a cache miss occurs, the system fetches the URL from the database and writes it to Redis with a Time-to-Live (TTL) setting to prevent memory bloat.
Deep dive into 2-3 key components. Explain how they work, how they scale, discuss tradeoffs, capacity, and any relevant algorithms or data structures.