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:
- scalable
- highly available
- low latency 200ms
- zero data loss
- fault tolerant
API Design
POST tinyurl.com/create/long_url
DELETE tinyurl.com/delete/short_url
GET tinyurl.com/short_url
High-Level Design
We will have API Gateway, tinyURL Service, cache, encoder and database. API Gateway handles request forwarding, authentication, rate limiter. tinyurl service is the main service which has APIs hosted and acts as bridge between users and backend system. Encoder Service encodes long url to short url. For create API, flow is API Gatway, tinyurl service, cache, encoder, database then shorturl retuned to user. For GET REQUEST the flow is tinyurl service, cache, database and return the url with 303 HTTP code.
Detailed Component Design
1) For create API, Request comes to API Gateway, it is forwarded to tinyURL Service then check whether already a mapping exists if so return the mapped short url. Else Service passes the request to encoder, and saves tinyurl and original url mapping along with other details like user_id, created_dt, expiry into Database.
For get url, Request comes to API Gateway, it is forwarded to tinyURL Service then cache is checked, if found then long url is returned with redirect http code. Also tinyurl service writes to message queue to build analytics like count and user region. analytics service consumes and builds analytics and saves it into OLAP DB.
Encoder takes long url and converts into shorturl by getting a pre-generated fixed length strings or can use base62 method to hash longurl to short url.