Detailed component design
Dig deeper into 2-3 components and explain in detail how they work. For example, how well does each component scale? Any relevant algorithm or data structure you like to use for a component? Also you could draw a diagram using the diagramming tool to enhance your design...
- To generate an alias, we can take hash of the API server ID and append a monotonically increasing integer value generated on the alias generator.
- To avoid the same alias being generated in case the alias generator crashes, we can have a DB to store ranges of integers, such as 1-100, 101-200, so on and so forth, the alias generator can mark a range of values as "in use" and start incrementing the range. If it needs a new range, it can grab a lock on the table and get a new range.
- When we lookup an alias, we can return the cached value if it exists, otherwise we need to do a DB fetch
- If an alias is already generated for a URL by a user, we do not re-generate alias, which ensures the request is idempotent. This can be ensured by looking up in the table storing mapping from URL to alias. We write to this table after we write to the alias to URL table.
- We use a durable message broker to add the URLs and aliases. DB writer picks messages from the queue and only updates offset to the queue if both writes succeed, to prevent partial DB write failures.
- To prevent hot partitions on the DB writes for popular URLs, we can partition by user ID + URL isntead of just URL
Database design
Defining the system data model early on will clarify how data will flow among different components of the system. Also you could draw an ER diagram using the diagramming tool to enhance your design...
URL to alias map
- Key value store mapping URL to alias
- 3 fields - URL, userID, alias
Alias to URL map
- Key value store mapping alias to URL
- 2 fields - alias, URL