Q:
What if the system already has a short URL for that long URL. E.G User 1 adds Google.com to tinyURL and user2 does the same. I take it only 1 tiny URL should be made?
A:
Unique Mapping: When a user requests to shorten a long URL, the system should first check if that particular long URL already exists in the database. If it does, the service can return the existing short URL instead of creating a new one.
Scalability:
The system should handle growing numbers of URL creation and redirection requests without performance drops. Horizontal scaling (adding more servers) is key.
Security:
The service must protect against abuse, such as malicious URLs, spamming, and ensure that short URLs do not expose sensitive data.
Performance:
Lookups and redirects must be fast. Using caching (like Redis) and efficient database indexing ensures minimal latency.
Availability:
For a URL shortening service, you might expect a varying number of users. For our estimate:
Based on the above assumptions:
Let's assume each entry for a shortened URL includes:
The total size for one mapping might look something like this:
Now, rounding up for growth and redundancy, you might plan for about 200 GB to 500 GB of data storage in the first year, depending on how aggressively you believe users will use the service.
You might design your application under the assumption that:
As our URL Alias System only requires CREATE, RETRIEVE we can use a simple RESTful API approach.
POST /api/v1/shorten
Request:
longUrl: string (required)Response:
shortUrl: stringBehaviour:
longUrl already exists.shortUrl.GET /api/v1/{shortUrl}
Request:
shortUrl: path parameterResponse:
longUrlBehaviour:
shortUrl.longUrl.Defining the system data model early on will clarify how data will flow among different components of the system. Also you could draw an ER diagram using the diagramming tool to enhance your design...
MongoDB Collections:
1. User
userId (primary key)emailAddresscreatedAt2. UserSession
sessionId (primary key)userId (foreign key)jwtTokencreatedAtexpiresAt3. URLAlias
shortUrl (primary key)longUrl (indexed)userId (foreign key, optional if anonymous usage is allowed)createdAtUse a Master/Slave DB Replica approach to split WRITE/READ operations to minimise DB Load.
Use a REDIS cache (Redis can be horizontally scaled) to cache frequently used URLs. Cache Invalidation will be LRU (Least Frequently Used) and TTL
You should identify enough components that are needed to solve the actual problem from end to end. Also remember to draw a block diagram using the diagramming tool to augment your design. If you are unfamiliar with the tool, you can simply describe your design to the chat bot and ask it to generate a starter diagram for you to modify...
Here's how the flow would typically work:
Explain how the request flows from end to end in your high level design. Also you could draw a sequence diagram using the diagramming tool to enhance your explanation...
Here's how the flow would typically work:
Dig deeper into 2-3 components and explain in detail how they work. For example, how well does each component scale? Any relevant algorithm or data structure you like to use for a component? Also you could draw a diagram using the diagramming tool to enhance your design...
1. API Gateway
The API Gateway is your first line of defense and smart routing.
/shorten to web servers, /shortUrl to redirection handlers)2. Redis Cache
Redis dramatically reduces lookup latency for popular URLs.
{shortUrl: longUrl} temporarily in memory.3. MongoDB
MongoDB is your durable storage for mappings.
{shortUrl, longUrl, userId, createdAt}.shortUrl (primary key) and longUrl (for uniqueness checking).shortUrl ensures even distribution.shortUrl to guarantee no collisions.Explain any trade offs you have made and why you made certain tech choices...
1. NoSQL (MongoDB) vs SQL (Postgres/MySQL)
2. Redis Caching
3. JWT Authentication
4. URL Shortening Approach
5. Single Region Deployment Initially
Try to discuss as many failure scenarios/bottlenecks as possible.
1. API Gateway Single Point of Failure
Load Balancer Failure
3. Redis Cache Failure
4. MongoDB Bottlenecks
5. Hot URL Redirects (Cache Miss)
6. Authentication Service Latency
7. Link Overload (DDoS attack on short URLs)
9. URL Alias Collision
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?