POST /pasteBin/ -> shortLink
{
text : string
}
GET /pasterBin/:pasteId -> text
pasteId, string --- partition key
text , string
createdAt, ts
updatedAt, ts
client -> LB -> pasteBinService -> DynamoDB <- shortLinkGenerator
-> Cache(redis)
Write Flow:
client -> load balancer -> pasteBinService -> Cache -> shortLinkGenerator -> DynamoDB
Read Flow:
client -> load balancer -> pasteBinService -> Cache -> DynamoDB
Purge Flow
For write flow, to avoid duplicate shortLinks, we can have distributed lock. When a request comes in a shortLink is picked up to map the link to the text, we can lock the link, so other requests can not pick up the same link.
Another way to avoid this is when we write to the db, we check the status of the cache to see if it already taken by other.
We can have a write thorugh cache, write to the cache first and then trigger an event to write to DB to get consistency.
The cache schema will be:
{
pasteId, string --- partitionKey
text, string
}
By using cache, we add complexity but would like to achieve better latency and we also need to handle the consistency between cache and db.
If we have a high traffic that consumes the shortLinks quickly. The shortLinkGenerator needs to be run in a event driven mode, not a cron job, to avoid high waiting time when the links exhausts.
If we hit a duplicated shortLink (means that when the service picked up a link for a text and trying to write to cache/db, we find that it is aleady taken).
Instead of fail immediately, we can pick up a new one for it.