In a URL shortener system, user interactions with the system have different outcomes based on whether the user is creating a shortened URL or navigating to a short URL to be redirected to the long URL.
In the scenario where a user is creating the short URL, the application would go directly through the CDN to the load balancer, which would forward the POST request to the URL Creation service, which in turn will create the URL and store it in the database. The URL creation service generates the unique keys for the URL using a snowflake-style ID (Based on a timestamp, node ID, and a sequence number) generation approach, which would generate a 47-bit integer and then transforms that into a base62 (8-letter key) to be used in the short URL. once the URL is generated, the service will hit the database and store the urls in the relevant database shard, based on the short_code which would be hashed and put in the correct shard, and another record would be created to link that short code to the user that created it.
In the scenario where users are navigating a short URL, the request first reaches the nearest CDN edge locations. The edge checks if that request is already in the cache. If it's cached, the CDN returns the redirect immediately and asynchronously logs the request event to the events queue. If the request was not in the cache, the CDN forwards it to the load balancers and also asynchronously log the request event to the event queue. The load balancers in turn forwards to a URL redirection service, the redirection service then looks up the short_code in the relevant shard, and returns the response to the client, the CDN also caches the response for future requests with a TTL 60 seconds.
The analytics service periodically reads the events queue and looks up for click/redirect events, transforms them into relevant analytics data, stores them in the analytics database, making sure to use the correct sharding strategy to store the data, based on region.