User interacts with the url shoretning service via UI, he can either shorten the url or provide a shrot url for redirection to the long link. API gateway verifies and routes the request to the shortening service. the shortening service decides, basing on the request if it must rertieve long url (by checking or udpating the cache first and then redirecting to the logn url address) or shortening and storing the url. We'll use the hashing function to generate a short url. Storage layer will consist manly of the URL mapping table, with fields: short URL identifier, long url, creation_timestamp, user_name, user_email. Hash index on the short url field should speed up the retrieval process. The partition key might be the short URL. We'll use the key-value DB (Cassandra or DynamoDB) due to the high-read properties fo the service
API Gateway
Key responsibilities:
URL Shortening service
A small, dockerized service, so we can spin up many instances of it. Doesn't have to work in an async/await way, nice improvement though. Main problem is to properly hash and store short ulrs in case of many services. We can u In case of many services we would have to assign intervals in which particular instance can operate on so there will be no collisions between the instances. Quering the DB on every save is not feasible.
Redis will be used as a backend cache, fixed number of most popular urls for retrieval will be stored there and almost instantly returned, cache will be managed using the Time To Live with the LRU algorithm. Read-through caching system should be in place. this should mitigate the hot-spot potential problems. in case one or couple of short urls will become highly popular at some point, if not present in the cache this popular url will be saved there after the first usage. LRU mechanism will make sure that this popular url stays in the cache and is retrieved from it in many subsequentiall hot-spot traffic spike
A simple ragne managers service shoudl be in place - it sets and applies id ranges (e.g. 1-1000000 range) to a specific urls shortening service instances, which avoid collision risks further and allows the services to operate on their range even during outages and network partitions.
To achieve high concurrency and zero-collision architecture without querying the database, the system will use a Distributed Unique ID Generator inspired by the Twitter Snowflake algorithm, managed by a centralized Range Manager.
1001 -> aX9zW, ID 1002 -> mP3kQ), ensuring security while maintaining 100% uniqueness without DB overhead.[a-z, A-Z, 0-9]), resulting in a short URL string of 7 to 11 characters.. We can use the optmistic locking mechanism (we check before commiting the transaction if the id is still possible to save) and additioanlly an unique constrain on a DB short_id field to avoid duplicates finally.
A monitoring service can be created that would count and inform us about unusally high number of collision