System requirements
Functional:
List functional requirements for the system (Ask the chat bot for hints if stuck.)...
- store text and return url to access that .
- use login and signup and session management
Non-Functional:
List non-functional requirements for the system...
- Availability
- Scalability
Capacity estimation
Estimate the scale of the system you are going to design...
1. Estimating Traffic and Data Load
A. Writes (Paste Creations)
- Pastes per Day: Let's assume we anticipate around 1 million new pastes per day.
- Average Paste Size: We'll assume an average paste size of 10 KB.
- Total Daily Data Ingestion:
- $1 \text{ million pastes/day} \times 10 \text{ KB each} = 10 \text{ GB/day}$.
- Writes per Second:
- With 1 million pastes per day:
- $\frac{1,000,000}{24 \times 60 \times 60} \approx 12 \text{ writes per second}$ on average.
- During peak loads, this could spike to ten times the average, resulting in hundreds of writes per second.
- With 1 million pastes per day:
B. Reads (Paste Retrievals)
- Read to Write Ratio: Assuming a read-heavy workload with a read-to-write ratio of 5:1 or even as high as 10:1.
- Reads per Day:
- For a 5:1 ratio:
- $5 \text{ million pastes per day} = 50 \text{ million read requests per day}$.
- Reads per Second:
- Average: $\frac{50,000,000}{24 \times 60 \times 60} \approx 578 \text{ reads per second}$.
- This can burst significantly on viral pastes, potentially requiring thousands of reads per second at peak times.
- For a 5:1 ratio:
2. Storage Requirements
- Daily Storage: At 10 GB of new data per day, storage over time becomes significant.
- Monthly Storage: Approximately $300 \text{ GB/month}$.
- Yearly Storage: Approximately $3.6 \text{ TB/year}$.
- Retention Policy: If pastes expire or are deleted after a certain period, such as 3 months, average storage can be around 900 GB.
3. Unique ID Space and Collisions
- ID Size: Using a 6-character alphanumeric ID provides a vast space of $62^6 \approx 56 \text{ billion possibilities}$.
- With 1 million new pastes every day, it would take years to exhaust this key space, keeping collision probability low.
Key Considerations
- Scalability: The design should allow horizontal scaling to handle the peak loads efficiently.
- Data Durability: Critical to ensure no data loss occurs, particularly for popular pastes.
- Caching Strategy: This is crucial for handling the read-heavy nature of the workload, with frequently accessed pastes cached to reduce database load.
API design
Define what APIs are expected from the system...
1. Store Data
This endpoint is used to create a new paste by sending the text content and user information.
- Method:
POST - Path:
/pastebin/store
Request Body
- Content-Type:
application/json
| Field | Type | Description |
text | string | The text content to be stored. |
userid | string | The unique identifier for the user. |
Example Request:
{
"text": "This is a new paste for the service.",
"userid": "user-12345"
}
Response
- Status Code:
201 Created - Content-Type:
application/json
| Field | Type | Description |
url | string | The unique URL for the newly created paste. |
Example Response:
{
"url": "[https://pastebin.example.com/abcdef123] (https://pastebin.example.com/abcdef123)"
}
2. Fetch Data
This endpoint is used to retrieve the text content of a paste using its unique hash.
- Method:
GET - Path:
/{hash}
Response
- Status Code:
200 OK - Content-Type:
application/json
| Field | Type | Description |
text | string | The raw text content of the paste. |
Example Response:
{
"text": "This is a new paste for the service."
}
- Note: If the hash is not found, the API will return a
404 Not Foundstatus.
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...
In database design we have two things one we are going to store data in aws s3 and metadata in relational database.
High-level design
You should identify enough components that are needed to solve the actual problem from end to end. Also remember to draw a block diagram using the diagramming tool to augment your design. If you are unfamiliar with the tool, you can simply describe your design to the chat bot and ask it to generate a starter diagram for you to modify...
Pastebin Service ⚙️
This is the core service that orchestrates the entire process. Its responsibilities are:
- Data Storage: It's responsible for the primary action of the service—storing text data. It writes the raw text content to AWS S3 and saves the corresponding metadata to the database.
- URL Creation: It's tasked with generating the unique, shareable URL for each paste. This is done by creating a hash based on a UUID and User ID, which ensures uniqueness for each session. This hash is stored as
encoded_urlin the database, and the final URL is constructed asdomain/hash. - URL and Data Retrieval: When a user accesses a URL, the service must:
- Extract the hash from the URL.
- Use the hash to query the database and retrieve the associated metadata (including the UUID and User ID).
- Use the S3 key from the metadata to fetch the actual text data from AWS S3.
- Return the text content to the user.
How URLs are Created
The URL creation process is a key part of the service's design. The service combines two unique identifiers—a UUID (Universally Unique Identifier) and the User ID—to create a unique hash. This approach ensures that the URL is not easily guessable and is unique to a specific user and a specific "paste" session. The hash is then appended to the domain to form the complete URL, like domain/hash.
Service Components
AWS S3 ☁️
AWS S3 (Simple Storage Service) is an object store used to hold the raw text data. It's chosen for its key features:
- Cost-Effectiveness: It's a cheap and scalable storage solution, making it ideal for storing large volumes of data without high costs.
- High Scalability: It can handle virtually unlimited data, ensuring the service can grow as needed.
Database 💾
The database's primary role is to act as a metadata store. It's crucial for the service's functionality and stores information such as:
encoded_url: The unique hash used in the URL.- S3 Key: The pointer or key that links the database record to the actual text data stored in S3.
- User Details: Information about the user who created the paste.
- Other Metadata: This could include things like the creation date, expiration date, or privacy settings for the paste.
Request flows
Explain how the request flows from end to end in your high level design. Also you could draw a sequence diagram using the diagramming tool to enhance your explanation...
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...
1. Client
The Client is the user-facing component, which can be a web browser, a mobile app, or a command-line utility.
Key Responsibilities:
- User Interface: Provides an interface for the user to input text and a user ID.
- Request Handling: Sends
POSTrequests to the API to store new pastes andGETrequests to retrieve existing ones. - Data Presentation: Displays the generated URL for a new paste and presents the retrieved text content to the user.
2. API Gateway
The API Gateway acts as the entry point for all client requests. It provides a single, unified interface for the backend services.
Key Responsibilities:
- Request Routing: Directs incoming requests to the appropriate backend service. For this design, it forwards all requests to the Pastebin Service.
- Endpoint Management: Exposes the public endpoints (
/pastebin/storeand/{hash}). - Basic Validation: Can perform initial checks on the request path to ensure it is valid before forwarding.
3. Pastebin Service (Backend Logic)
This is the core business logic component of the system. It orchestrates the storage and retrieval of data by interacting with the data tier components.
Key Responsibilities:
- Store Logic:
- Receives the
textanduseridfrom the API Gateway. - Generates a unique hash based on the
useridand a newly created UUID. - Writes the raw
textdata to AWS S3 and receives a uniques3Keyin return. - Stores the metadata (including
hash,s3Key, anduserid) in the Database. - Constructs the full, retrievable URL and returns it to the API Gateway.
- Receives the
- Fetch Logic:
- Receives a
hashfrom the API Gateway. - Queries the Database using the
hashto retrieve the correspondings3Key. - Retrieves the text content from AWS S3 using the
s3Key. - Returns the raw text content to the API Gateway.
- Receives a
4. AWS S3 (Object Storage)
AWS S3 is the primary storage component for the raw text content.
Key Responsibilities:
- Object Storage: Securely and durably stores the text data as individual, immutable objects.
- Unique Keys: Provides a unique key for each stored object, which serves as the pointer for retrieval.
- Scalability & Durability: Handles massive volumes of data with high availability and reliability.
5. Database (Metadata Storage)
The Database is responsible for storing and providing fast lookups for the metadata associated with each paste.
Key Responsibilities:
- Metadata Storage: Stores a lightweight record for each paste, containing the
encodedUrl(hash), thes3Key, and theuserId. - Fast Lookups: Allows the Pastebin Service to quickly find the
s3Keyby querying theencodedUrl. - Data Integrity: Ensures consistency between the URL hash and the S3 object key.
Trade offs/Tech choices
Explain any trade offs you have made and why you made certain tech choices...
1. Choice of AWS S3 for Text Data Storage
You've correctly identified the primary trade-off with this choice: performance versus cost and scalability.
- Trade-off: Performance vs. Cost/Scalability
- Why S3 is beneficial: It is an incredibly cost-effective solution for storing large amounts of unstructured data. You pay per gigabyte stored and for data transfers, but the cost is significantly lower than a traditional relational database. S3 is also highly scalable and durable, meaning it can handle massive volumes of data without you needing to manage the underlying infrastructure.
- The Compromise: The trade-off is that S3 is not designed for low-latency, real-time access. Retrieval times can be slightly slower compared to fetching data directly from a database. This is a deliberate design choice, as the service is not intended for high-speed transactions where every millisecond counts. For a pastebin service, a small delay in retrieval is acceptable.
Failure scenarios/bottlenecks
Try to discuss as many failure scenarios/bottlenecks as possible.
Future improvements
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?
we improve this service in future by spending time on choose other object store.