Requirements


Functional Requirements:


  • Create a unique, short URL for a given long URL.
  • Return the long URL associated with a given short URL.



Non-Functional Requirements:


  • Low latency
  • Reliability



API Design

POST api/v1/shorten

request body: longUrl

response: shortenUrl


GET api/v1/shorten/{shortUrl}

response: return 302 + longUrl



High-Level Design

We need an API Gateway, so it can helps us with following topics:

  1. Throttling
  2. Auth
  3. IP whitelist


URL Redirection Service: GET API request will handled by this service, it will use read-through fetch the cache to see if the short url there, if not, it will query DB (NoSQL) and store the pair in the cache.


URL Generator Service: POST API request will handled by this service, it will forward the request to id generator service to generate an unique, short URL and store the in DB (NoSQL).


Id generator: is a bunch of server, we can assign an id to each machine, we can generate a shorturl based on machine id + sequence number and base62(a-z, A-Z, 0-9).


NoSQL: we choose to use NoSQL as we don't need ACID in this design and NoSQL is easy to do horizontal scaling.


Cache: LRU cache.



Detailed Component Design

We need an API Gateway, so it can helps us with following topics:

  1. Throttling
  2. Auth
  3. IP whitelist


URL Redirection Service: GET API request will handled by this service, it will use read-through fetch the cache to see if the short url there, if not, it will query DB (NoSQL) and store the pair in the cache.


URL Generator Service: POST API request will handled by this service, it will forward the request to id generator service to generate an unique, short URL and store the in DB (NoSQL).


Id generator: is a bunch of server, we can assign an id to each machine, we can generate a shorturl based on machine id + sequence number and base62(a-z, A-Z, 0-9).


NoSQL: we choose to use NoSQL as we don't need ACID in this design and NoSQL is easy to do horizontal scaling.


Cache: LRU cache.