User database will contain the user's data and login credintials in on collection of documents
The URLService will contain the shortURL, longURL and creationDate.
High-level design
API Gateway that performs load balancing.
Replicas from the URLShorter service
Replicas for User Service
Distirbuted caching to store frequently access urls
NoSQL database Database for user service
NoSQL db for URLShortner service
Sequencer to generate unique ids for short URLs
The Gateway will decide which service to forward the request based on the url (if it contains /user will redirect the user service)
The Gateway will also balance the load over the service replicas.
The user service will be responsible for signup, signin and authorizing the user by handing an access token
URL service is resoponsible for adding, deleting and listing short urls, and caching the frequently access URLs using cache aside pattern.
The sequencer will generate a unique strings for the short urls, making sure that each dirtibuted replica has its own unique range of IDs to prevent duplication and avoid race conditions
The urls database will use the userId as the sharding key
Request flows
Create new short URL
Client will send a post request for creation
The gateway to redirect to one of shortner serice replicas based on round robin algorithm
The service will call the sequencer to generate the unique string
The service will save the unique Id paired with the longURL, userId and creation date
The database will decide which shard to save in based on the userId
Read a short URL
Client will try to access a short url
The gateway will recieve the request and redirect it to one of the available url service replicas
The service will search in the cache and return the result if existing
If not, will read from the db and return the result, then save it into the cache asyncrounously while defining a 1 day TTL.
If not existing, redirect to error page
Delete url:
Client will try to delete a short url
The gateway will recieve the request and redirect it to one of the available url service replicas
The service directly send a delete request to the db.
If existing, the service will return success, then remove the same Id from the cache if existing
Detailed component design
Check the class diagrama
Trade offs/Tech choices
The sequencer call and db sharding might affect the write performance, but it scales better and guarantees more availability
Caching on the read instead of the write to not cache useless URLs
Failure scenarios/bottlenecks
The sequencer might be a bottleneck, so it also has to scale with leader follower algorithms
Distributed cache might be overloaded, so it also has to scale and we might need explicit invalidation to the LFU urls after specifc threshold
Future improvements
Clear the LFU urls from the cache even before the TTL with specific threashold reached
We might use a queuing system to offload the creation requests rather than overscaling the URL service to handle more creation requests