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:
- List the key non-functional requirements (eg low latency, scalability, reliability, etc.)...
API Design
1) endpoint to take the url given by user, also domain is optional - returns shorteened url.
2) endpoint that redirects to actual endpoint from the database
High-Level Design
- Cloudflare acts as a reverse proxy, receiving incoming requests and handling them before they reach your backend services. This includes SSL termination, which secures connections by encrypting data in transit, thus protecting user information.
- Routing: Cloudflare can intelligently route requests based on various factors such as geographic location, load balancing, and health checks of your backend services. This ensures that requests are directed to the most appropriate server, enhancing performance and reliability.
- Interaction with Backend Services: After processing, Cloudflare forwards the request to your backend service (e.g., the URL shortening service). It can also cache responses for frequently accessed URLs, reducing load on your backend and improving response times for users.
ENtry level would be the doing of cloudfare.
url redirection and creating are in the same service file.
Would use load balancer and multiple kubernetes pods to distribute load.Querying from database is async. Frontend checks the correctness of url and encrypts the data that is sent over to backend
there is 1 backend and 1 controller, there is 1 service file that is repsonsible for the methods createUrl() and redirectUrl()
Generating a unique id: Utilizing UUID.
Requests can be secure by using certain headers and/or cookies. Can use rate limiting for fighting against botting.
Databse table has2 columns: unique id and the long url associated with it.
Controller that accepts requests and has endpoints in it.
Each method calls a urlservice method, like createUrl and redirectToUrl, first returns shortened url and second redirects to actual url.
createUrl():
takes the url given by user and optionally a domain.
Create mapping model that has 2 variables: longUrl and id of short url.
save the mapping to database table where mapping models are saved
redirectUrl():
input is the short url, from database retrieve the actual url by fetching where the id is the last part of short url.
then service makes 302 request.
Database has 1 table that stores mapping model:
longUrl, urlId
Hold existing unique identifiers in cache to quickly check what ids are used. WIll utilize hashset data structure and when randomly generating new unique identifier then checking whether the id is in there, if yes generate new one and check if not then go with the initial one.
Cache layer uses Redis cache, there will be hashset there. would use dynamodb.
Data storage is responsible for holding mappings of url and unique identifiers.
Will index the unique id column.
Detailed Component Design
Identifier Generation - base-62 encoding of a sequential ID, which allows for a compact representation. The algorithm must ensure that generated IDs are unique and hard to guess to prevent abuse. Trade-offs include balancing the ID space size against URL length; a longer ID can provide more unique combinations but may be less user-friendly. You should focus on ensuring that the identifier generation can handle many concurrent requests to avoid collisions. In the case of collisions we add a random symbol at the end. You check instsance by checking contains check in the hash set andif it does i retreis. basically a for loop until we get a unique id.
Data Storage Layer: - using dynamodb, indexing on unique id column., original link should be around 500 characters long.
caching layer: using redis cache, each item in cache lives for 60minutes.
Deep dive into 2-3 key components. Explain how they work, how they scale, discuss tradeoffs, capacity, and any relevant algorithms or data structures.