Pastebin System Design

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-03-01

Problem Statement

Design the Low-Level Design (LLD) for a system similar to Pastebin, where users can paste and share plain text or code snippets via a short unique URL.

The system should allow users to create, retrieve, and optionally expire snippets.


Focus on object-oriented design, data storage abstraction, URL shortening logic, and expiry management.


🎯 Core Requirements


1. Create Paste


•Users can submit a block of text or code.
•The system generates a unique short URL/key for the paste.
•Users can optionally specify an expiry time (e.g., 10 minutes, 1 hour, 1 day, never).


2. View Paste


•Anyone with the short URL should be able to view the paste content.
•If the paste has expired, show an appropriate message.


3. Expiry Management


•Support automatic deletion or invalidation of expired pastes.
•Background cleaner can periodically remove expired pastes.


4. Optional Features


•Allow anonymous and logged-in users to create pastes.
•Logged-in users can view all their created pastes.
•Support syntax highlighting for code snippets (optional, UI layer).


⚙️ Functional APIs


OperationDescription


createPaste(String content, Duration expiryTime)


Creates a new paste and returns a short URL.


getPaste(String pasteId)


Retrieves the paste content by its unique key.


deletePaste(String pasteId)


Deletes a paste manually.


listUserPastes(String userId) (optional)


Lists all pastes created by a user.


🧱 Suggested Class Design


ClassResponsibility


PasteService


High-level service handling paste creation, retrieval, and expiry logic.


PasteRepository


Manages storage and retrieval of paste data (in-memory, DB, etc.).


Paste


Entity representing a paste (content, id, creationTime, expiryTime).


IdGenerator


Generates unique short keys (like base62 encoded strings).


ExpiryManager


Handles expiry checks and cleanup of expired pastes.


User (optional)


Represents users who can create and manage pastes.


🧩 Entity Example


Paste

1 2pasteId: "aB12xY" 3content: "print('Hello World!')" 4createdAt: 2025-10-24T10:00:00Z 5expiryTime: 2025-10-24T11:00:00Z 6ownerUserId: "user123" (optional)


🧠 Design Considerations


Key Generation:

•Use a base62 encoding of a counter, hash, or random UUID substring to generate short unique IDs.

Data Storage:

•Store pastes in an in-memory cache (like a HashMap) or persistent database (SQL/NoSQL).

Expiry Handling:

•Each paste should have an expiryTime.
•The system can use:
•A background cleaner thread to delete expired entries, or
•Check expiry upon access.

Scalability (Optional Extension):

•Use distributed key-value storage (like Redis).
•Introduce sharding or consistent hashing for large-scale storage.
•Add caching for frequent paste retrievals.


🧩 Example Flow


Example 1: Create and Access Paste


1.User submits:
1 2Content: "Hello World!" 3Expiry: 10 minutes
1.System generates:
1 2URL: https://pastebin.com/aB12xY
1.Another user visits the URL:
1 2-> Displays: "Hello World!"
1.After 10 minutes:
1 2-> Displays: "This paste has expired."


🧩 Sample Class Diagram (Conceptual)

1 2+-------------------+ 3| Paste | 4+-------------------+ 5| id | 6| content | 7| createdAt | 8| expiryTime | 9| userId (optional) | 10+-------------------+ 11 12+-------------------+ 13| PasteService | 14+-------------------+ 15| +createPaste() | 16| +getPaste() | 17| +deletePaste() | 18+-------------------+ 19 20+-------------------+ 21| PasteRepository | 22+-------------------+ 23| +save(paste) | 24| +findById(id) | 25| +delete(id) | 26+-------------------+ 27 28+-------------------+ 29| IdGenerator | 30+-------------------+ 31| +generateId() | 32+-------------------+ 33 34+-------------------+ 35| ExpiryManager | 36+-------------------+ 37| +cleanExpired() | 38+-------------------+


🚀 Extensions (Optional)


•Private Pastes: Pastes accessible only via login.
•Encrypted Pastes: Encrypt paste content for confidentiality.
•Version History: Allow editing and version tracking.
•Rate Limiting: Prevent abuse by anonymous users.
•Analytics: Track paste views.
•Paste Forking: Allow creating a copy of an existing paste.


✅ Goal:


Design an extensible, object-oriented Pastebin LLD that supports paste creation, retrieval, expiry, and short URL generation, with clean class structure and clear separation of concerns.

Solution

Comments