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:


  • High Availabilty - Upto 99% uptime.
  • Low Latency - Request taking upto 5ms.
  • Scalable - The system should support horizontal scalability by allowing the addition of more servers and utilizing a load balancer to manage incoming traffic. This ensures that if one server fails, others can continue to handle requests, maintaining high availability.


API Design

/create - POST - Route to get the longURL from the user, and give back a shortURL.

/redirect/:shortURL - GET - Route to redirect the user the original URL.


High-Level Design

  • A Read Web Server for handling the read requests which have to be super fast.
  • A Write Web Server for handling the heavy write operations.
  • A Id generator which takes the Long URL create short URL for it using techniques like the Base 62 encoder to provide a collison-less id's.
  • A Database storing the URL mappings (short URL to Long URL).
  • Load Balancer to distribute traffic across multiple servers.
  • A Cache Layer to improve the reads.



Detailed Component Design

  • Read Web Server - A read server which has a cache like redis which look up for the mappings super fast with LRU(Least Recently Used) clean up stratery.
  • Write Web Server - A id generator and database writer server, which takes a long url from the user, encode's it using a base62 encoder, while we can get distinct id's using a ticket service.
  • Multiple Backend Servers - To handle the requests form the users, like shortning the URL, getting the mapping for the short URLs.
  • Database - For storing the Mapping of short and Long URLs along with an expiration date so that the database does not gets overloaded.Use sharding technique to separate databases into different parts so that there should not be the single point of failure. Sharding can be done according to the hash of the short URL.
  • Load Balancer - For redirecting the traffic according to the server availability. Checking the short URL in the request and redirecting it to the correct web server which has that shard.
  • A Cache Layer - For faster retrieval of mappings.Use Redis for in-memory faster cache and using the LRU(Least recently used) eviction algorithm to clear the cache. First check in the redis does it contains the mappings, if true send it from right there, if not make a database call store the result in cache and deliver it to the user.
  • A Rate Limiter - For limiting the requests made per IP so that the servers do not get overwhelmed with the requests