System Design Problems
Design Pastebin
Pastebin allows users to paste text or code and share it via a unique URL. Services like GitHub Gist, hastebin, and dpaste handle millions of pastes daily with simple creation and reading workflows.
- Paste Creation β Accept text input, generate unique URL, store with metadata
- Paste Reading β Retrieve paste content via short URL with minimal latency
- Expiration β Pastes auto-delete after configurable TTL periods
Pastebin is simpler than most system design problemsβit's essentially a write-once-read-many (WORM) system with time-based expiration.
Requirements
Functional Requirements
- Users can create a paste with text content (up to 10MB)
- Each paste gets a unique, shareable URL
- Pastes can be public or private (unlisted)
- Pastes expire after a configurable duration (10 min, 1 hour, 1 day, 1 week, never)
- Support syntax highlighting for code pastes
- Users can set a custom name for the paste (optional)
Non-Functional Requirements
- Latency: Read paste in < 100ms
- Availability: 99.9% uptime
- Durability: Pastes must not be lost before expiration
- Scalability: 10M new pastes/day, 100M reads/day
Back-of-the-Envelope Estimation
API Design
POST /api/v1/pastes
Request: { "content": "...", "syntax": "python", "visibility": "public", "expires_in": "1h" }
Response: { "paste_id": "abc123", "url": "https://paste.example.com/abc123" }
GET /{paste_id}
Response: { "content": "...", "syntax": "python", "created_at": "...", "expires_at": "..." }
GET /api/v1/pastes/{paste_id}/raw
Response: Plain text content (no JSON wrapping)
DELETE /api/v1/pastes/{paste_id}
Response: { "status": "deleted" }
High-Level Architecture
Detailed Design
Storage Layer
Pastebin has two distinct storage needs:
| Data Type | Storage | Reason |
|---|---|---|
| Paste content (large) | Object Storage (S3) | Cost-effective, durable, high throughput |
| Paste metadata | Relational DB (PostgreSQL) | Structured queries, relationships |
| Hot pastes | Redis | Sub-millisecond reads, TTL support |
| Expired paste tracking | Redis sorted set | Efficient expiration scanning |
Expiration Strategy
Pastebin requires automatic deletion of expired pastes:
Option A: Lazy Expiration (Recommended)
- Check
expires_aton every read - Delete if expired; return 404
- Simple, no background processing
- May show stale data in cache
Option B: Active Expiration
- Background worker scans for expired pastes
- Deletes from database and cache periodically
- More consistent, but adds complexity
- Use sorted set in Redis:
EXPIRE_AT score=<timestamp>
Content Storage Pattern
Store large paste content in object storage, metadata in the database:
// Metadata record
{
paste_id: "abc123",
user_id: "user_456",
content_path: "s3://pastes/ab/c1/abc123.txt",
syntax: "python",
visibility: "public",
created_at: "2026-06-20T10:00:00Z",
expires_at: "2026-06-20T11:00:00Z",
size_bytes: 10240
}
Syntax Highlighting
Support syntax highlighting for code pastes:
- Client submits paste with
syntaxparameter (or auto-detect) - Server stores raw content in object storage
- On read, apply syntax highlighting at the CDN edge or application layer
- Cache highlighted HTML alongside raw content
Scaling Considerations
Database Partitioning
Partition paste metadata by paste ID hash:
shard = hash(paste_id) % NUM_SHARDS
This distributes pastes evenly across shards. For time-based queries (e.g., "recent pastes"), maintain a separate time-indexed table or use a time-series database.
Read Path Optimization
- Check Redis cache for paste metadata
- Cache hit: return content from object storage (cached at CDN)
- Cache miss: query database, populate cache, return content
- CDN caches public paste content at edge locations
Write Path Optimization
- Generate unique paste ID (Snowflake or random)
- Write metadata to database (async if possible)
- Write content to object storage (S3 multipart upload for large pastes)
- Populate Redis cache proactively
- Return paste URL to user
What to Learn Next
-> Design URL Shortener Similar WORM pattern with ID generation and caching strategies.
-> Caching Strategies Cache-aside, write-through, and TTL-based expiration patterns.
-> CDNs Caching static content at the edge for global low-latency access.
-> Databases Choosing between SQL and NoSQL for metadata storage.
-> Design Object Storage Building scalable blob storage for large content objects.
-> Design Unique ID Generator Snowflake IDs, UUIDs, and distributed ID generation.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type β Always start by categorizing the problem
- Incorrect formula application β Verify the formula applies to your specific case
- Unit errors β Always check units are consistent throughout
- Sign errors β Pay careful attention to positive and negative values
- Rounding too early β Keep full precision until the final answer
- Forgetting constraints β Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic β Build on the foundation laid here
- Next Topic β See how this concept extends to more advanced material
- Practice Problems β Additional exercises with solutions
- Video Explanation β Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions