Designing PasteBin
By: Tannishk Sharma
Tags: Interview Questions, Scalability, API, Paste Bin, Computer Science

Lets design pastebin

Pastebin is a simple service which allows its users to share text or images over the internet by generating a unique URL . Others can access the contents of the taste via the URL .

Step I : Gather the requirements

Requirements:

Function Requirements :

Users should be able to paste their data and get a unique URL Data and Urls should expire after a given amount of time . Users should be able to specify expiration date.

Non Function Requirements :

System should be highly available and reliable Users should be able to access their Pastes in real-time with minimum latency.

Step 2: Capacity Estimation

Well I am skipping this step for this problem

Step 3 : Define the Apis

We can use either SOAP or REST architecture to implement the system .

create_paste (api_dev_key,paste_data,custom_url,expiry_date)

Type : Post

Parameters:

api_dev_key(String):The API key of a registered account. It can be used to apply quotas

paste_data(String):Textual data of the paste.

custom_url (String) :Optional custom URL.

expiry_date(Date):Optional Custom Date

Returns:

success(Boolean) : Success or Failure

url (string): Generated Url of the paste

get_paste(api_dev_key, url)

Type: GET

Parameters:

api_dev_key(String):The API key of a registered account. It can be used to apply quotas

url(String):Url to access the paste

Returns:

success(Boolean) : Success or Failure

text (string): Textual data of the paste

delete_paste(api_dev_key, url)

Type: DELETE

Parameters:

api_dev_key(String):The API key of a registered account. It can be used to apply quotas

url(String):Url to access the paste

Returns:

success(Boolean) : Success or Failure

Step 4: Database Design

There will be one scheme one is for storing information of a paste . We are going with the assumption that user table will exist which contains user information

Database schema for the Paste

Extra : Instead of content_data we can also store content_key which is a reference to the external storage where paste is being stored.

Step 5: High Level Design

High Level Design for any service (Source:educative.io)

Step 6 : Business Logic

How to handle write requests?

Upon receiving the write request you can generate a random number which might act as a key for the paste . Insert the key and the paste into the Database .

Problem with the this method is if generated random key is already stored we need to generate a new one and try again .

Other possible solution is instead of generating random six letter key either use key generating service or use auto increment Primary key of the paste table as the key

After key is generated use encryption algorithm to convert the key to six letter url .

We should return success as false to the user if the custom key they have provided is already present in our database .

How to handle read requests?

We should convert our url to the key to be searched using our decryption algorithm and then searches for the key which we obtain from the Url in the paste table . If it is found, returns the paste’s content_data . Otherwise, success is returned as false.

Additional Info :

For Large Texts we can use meta Objects Storage Databases like amazon s3 or Amazon Redshift

For Load Balancing Refer to : https://medium.com/@tannishk/load-balancers-system-design-c8a95b57a651

For Caching Refer To : https://medium.com/system-design-blog/what-is-caching-1492abb92143