Develop endpoints for below requirements:
Traffic
Assuming 20:1 read/write ratio
Number of unique shortened links generated per month = 10 million
Number of unique shortened links generated per seconds = 10 million /(30 days * 24 hours * 3600 seconds ) ~ 4 URLs/second
With 20:1 read/write ratio, number of redirections = 4 URLs/s * 20 = 80 URLs/s
Storage
Assuming lifetime of service to be 10 years and with 10 million shortened links creation per month, total number of data points/objects in system will be = 10 million/month * 10 (years) * 12 (months) = 1.2 billion
Assuming size of each data object (Short url, long url, created date etc.) to be 500 bytes long, then total require storage = 1.2 billion * 500 bytes = 0.6 TB
Build Utilities
Clean-up Service/Batch Design
Clean up service (daily batch) would pick up the urls which are present in the system for longer time(configurable) like 5/10/20 years and put effective end date as current date.
As the system needs high availability and needs to handle high volume of data, NoSQL DB like MongoDb, Cassandra, Redis would be better choice.
URL Mapping Table/Collection: Each document should have longUrl, shortUrl, effectiveStartDate, effectiveEndDate, createdby, updatedBy, createdDate, updatedDate
Spring Batch Tables: In-built tables provided by spring batch to capture batch context, params and other details. this would be needed for clean up service.
There will be 2 microservices -
Shorten URL Design:
This component will also have Redis Cache integration to avoid hitting DB for records which are already in cache, hence improving performance of the system.
Shortened URL Generator Utility
Step 1: Generate an MD5 hash of the long URL
Step 2: Get First 6 bytes of the hash
Step 3: Convert these bytes to decimal:
Step 4: Encode the result into a Base62 encoded string
Clean up Service:
This is a Spring batch service which would be configured to run once a day. It will put effective end date as current date + 1 for the URLs which have start date greater than configurable time (5/10/15 years)
If the clean up service puts effective end date for a URL, then the redis cache server for shorten URL service should be evicted. Else, it would still refer to older set of valid records.