High-level design
The URL shortener is built around a microservices architecture with the following components:
- CDN: Serves static content to the user.
- Load Balancer: Routes the requests to various shards, to ensure maximum performance.
- Rate Limiter: Checks to ensure that the request is within the allowed limits. If not, the request fails.
- URL Creation Service: Allows the user to create a short URL. It will first check the cache for existing records, and then the database. It will then create a new record if necessary.
- URL Redirection Service: Allows the user to convert the short URL back to the corresponding long URL. It will also check the cache and then the database if the record is not found in the cache.
- Cache: The cache will store records that were recently accessed or created in the database. Say a short URL is recently queried, it would make sense to store that in the cache. This will minimize the amount of database queries. The same goes for if there is a common long URL which is recently shortened.
- Database: The database will store all of the records. Unlike the cache, this will be rigid, persistent storage. The downside is that it is slower to access. However, it is a necessary component to ensure that all data remains organized and secure.
- Monitoring and Logging Service: This will track user metrics and overall system performance. It will also be responsible for logging errors.