Design a web service that takes long URLs and returns short unique aliases (e.g., https://tiny.url/7bXk9q), redirection within 50ms, and analytics tracking.
Issues POST to shorten URL or GET to follow short link redirection.
Terminates TLS, routes traffic to nearest API server, handles rate limiting.
Handles Base62 encoding, ID generation, database writes, and cache lookups.
Pre-generates unique 7-character Base62 keys in memory to guarantee zero collision and instant writes.
Stores top 20% hot URL mappings (short_key -> long_url) for sub-5ms lookups.
Stores all URL mappings reliably. Sharded by short_key hash.
Asynchronously logs click metadata without blocking HTTP redirect response.
Aggregates click counts, geo data, and user agents into time-series store.
User clicks shortened link.
Service checks distributed in-memory cache.
If cached, return immediately without touching DB.
If cache miss, fetch long_url from DB and async populate Redis.
Async queue message for background click aggregation.
Redirect browser to target URL.