Requirements
Functional Requirements:
- Create a short URL for a given long URL.
- Return the long URL associated with a given short URL.
Non-Functional Requirements:
- There should be no collisions between two short urls for different long urls
- The same short url should always lead to the corresponding long url and should be persisted
- High availability
- Low latency
- Horizontal scaling
API Design
There should be two endpoints
- POST /shorten checks for existing short url for given long one and creates a short url
- GET /long looks up the given short url and returns the long version
High-Level Design
The short and long url pairs are stored in a relational database while a key value store handles the caching. The server checks the cache and if found redirects otherwise it checks the DB and updates the cache. There needs to be a DB replication in case of outage.
Detailed Component Design
The shorten API uses an base64 encoding to create shorter urls.
The relational DB should only be used if there is a cache miss. If one DB goes down the replica should be used instead.