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:


  • Low latency: P95 should be below 2 secs.
  • High Availability: The system should be highly available: 5 9's = 99.999% available.
  • Use multiple instances of api gateway, load balancers and shortening services in order to make the system highly available and reliable.
  • Trade Off between consistency and availability: (CAP Theorem): Availability over Consistency.


API Design

  1. POST /api/v1/url

payload: long_url: long_url

success response: 201 Created

success payload: short_url: short_url

2.GET /api/v1/url

params: short_url = short_url

success response: 200 OK

success payload: long_url: long_url



High-Level Design

  1. The User (web/app) will send a post request to generate a short url for the given long url, this will be sent as payload.
  2. The request will pass through an api gateway and based on the route, it will go to the load balancer.
  3. The load balancer with round-robin configured ( static load balancer) will route the specific request to the Shortening microservice.
  4. The shortening microservice will create a short url using hash based methods and store the value in the DB (key -value store).
  5. There will be worker (bull worker if using nodejs) in shortening service that polls for jobs in the redis queue and updated the redis for caching the data with a specific TTL. (This helps in reducing read req to DB)




Detailed Component Design

  1. Load Balancer : A Static Load balancer with round-robin algorithm will be good fit as it will ensure equal distribution of traffic between different instances. This works in Layer 4 hence can consume more traffic, and also handle SSL/TLS encryption and termination.
  2. API Gateway: Works in Layer 7, forwards requests based on the route. Handles Rate limiting using token bucket algorithm for protection against DDOS. Handles Authentication and Authorization of USERs using RBAC. Responses can also be cached for lower latency.
  3. Shortening Service: Generates a short_id based on the hash of first 6-8 characters of the long_url. This is used as a unique key to be stored for mapping the original url in the KV DB.
  4. KV Store: A Key -Value Persistent DB is used for storing short and long_url mapping due to high scalability and high availability. Amazon dynamo db is a classic cloud solution for the same, since it is fully managed by aws.
  5. Redis: Used as a cache to store
  6. Tradeoffs