TOUGH TONGUE AI
  • Home
    LogoTOUGH TONGUE AI

    RESOURCES

    Loading...

    HomeLibrarySD Mock · Design Pastebin

    SD Mock · Design Pastebin

    Overview

    Design a Pastebin-like text sharing service on a live architecture board — drag components, wire them up, and defend your design to an expert interviewer who watches every move.

    Your Instructions

    Design Pastebin

    A 20-minute mock system design interview. You drive the pace. Jump to the board when you are ready; skip capacity math if you want. The interviewer grades your thinking, not a template match. This is an easier question than most, so the bar shifts to execution: clean estimation, the storage split, key-generation correctness, a real expiry story. Strong candidates finish with time to spare and volunteer extended topics.

    The trap is treating this as a CRUD app with a UUID primary key. Pastebin is a short-URL + blob store: unique non-guessable keys, a 5:1 read/write mix, metadata that wants a database and content that wants object storage, and expiration that must actually delete bytes.

    Two shared surfaces (only one is on screen at a time; use the panel arrow to switch):

    • Notepad. Shared notes. Requirements, capacity math, schema, and end-of-session feedback live here. You can type in it too.
    • Architecture board. Your design canvas. Drag typed components, wire them, label edges, mark scaled tiers as distributed. If it is not on the board, it is not in your design.

    Build incrementally while you talk. Trace one paste create and one paste read through your diagram. Argue the tradeoff, not "industry standard."

    Design choices interviewers probe

    Read this before you go in. These are the decisions, and why they matter.

    Unique keys that are not enumerable

    A sequential integer URL (pastebin.com/1, /2, /3) is guessable: crawlers and attackers walk the namespace. You need a short, unique, hard-to-enumerate key.

    Charset. Base62 (a-zA-Z0-9) or Base64. Six Base64 characters → 64^6 ≈ 68.7 billion keys. Seven Base62 characters → 62^7 ≈ 3.5 trillion. Pick a length from the 10-year paste count plus a safety factor, not from vibes.

    How you mint them. Three families, pick one and defend it:

    • Counter + encode. Simple, unique, but sequential and therefore enumerable unless you hash/shuffle the counter.
    • Random + collision check. Cryptographically random, but uniqueness is enforced only by the duplicate-key constraint: two app servers can mint the same key at the same instant, one insert fails, you retry. Fine until write QPS makes the retry loop a hotspot.
    • Key Generation Service (KGS) with a pre-generated pool. Offline workers fill a key DB; app servers grab keys in batches, so the write path never runs a collision check at all. The detail that makes it correct: the key DB keeps two tables, unused and used, and a batch moves to used the moment it is handed out — two servers can never hold the same key. The costs you must own: the KGS is a single point of failure (run a standby replica), and a crashed KGS or app server wastes the keys it held in memory — acceptable when you have 68 billion.

    Do not hash the content (MD5, truncate). It fails three ways: identical content from two users lands on one key, truncation collides anyway, and content-derived keys can be precomputed by an attacker — which breaks the unguessability requirement.

    Custom vanity URLs are a separate uniqueness check in the same namespace, not a replacement for the default generator. A taken alias returns an error — never silently mutate it. Cap alias length.

    Metadata vs content

    Pastes are two different data types. Metadata (key, created_at, expiry, user, size, visibility) is small and queried. Content (the blob, up to ~10 MB) is large and rarely filtered. Putting both in one SQL row works at toy scale and then the table becomes a blob store with indexes on the wrong thing.

    The standard split: metadata in SQL (or a small KV), content in object storage (S3). Cache hot pastes (80/20: 20% of pastes take 80% of reads) in Redis with TTL ≤ paste expiry.

    Reads vs writes

    Typical load is ~5 reads per write. Optimize the read path: CDN → cache → metadata DB → object storage. The write path can be a single region; the read path wants a CDN.

    Expiration

    "TTL on the row" is not a design. Object storage does not vanish when a SQL row does. And a cron that scans for expired rows every minute hammers the DB for no user-visible gain. The pattern is lazy plus scheduled: the read path checks expiry and deletes on touch; a lightweight cleanup worker (or bucket lifecycle rules) sweeps object + row + cache during off-peak windows; freed keys go back to the unused pool. The contract you state out loud: an expired paste may physically persist for a while, but it is never served. Pastes with no expiry get a default TTL (say two years) or the store grows forever — though "storage is cheap, keep forever" is defensible if argued.

    Reference solution

    One complete design you can study, then rebuild in your own words. Do not recite it. In the interview, draw your graph and defend the choices.

    Requirements (lock these or take the defaults)

    • Upload text, receive a unique short URL; fetch by URL; optional delete by owner
    • Text only, max ~10 MB; optional expiry; public by default; optional custom URL
    • URLs must not be guessable; API for developers
    • Non-functional: highly available reads, durability of content, low latency on hot pastes
    • Out of scope unless they ask: syntax highlighting (client-side), versioning, full-text search

    Capacity

    • 1M new pastes/day, 5M reads/day (5:1 read:write) → ~12 writes/sec, ~58 reads/sec (peak 2–3×)
    • Average paste 10 KB — the average drives storage, not the 10 MB cap → 10 GB/day; ~36 TB over 10 years, ~51 TB provisioned at a 70% capacity ceiling
    • Keys: 3.6B pastes in 10 years vs 64^6 ≈ 68.7B six-char Base64 keys — ~19× headroom. The key store is 3.6B × 6 B ≈ 22 GB, negligible next to 36 TB
    • Cache: 20% hot × 5M daily reads × 10 KB ≈ ~10 GB working set — fits in Redis

    Rough numbers are fine; the method is what gets graded. Use the 5:1 ratio and the 36 TB to justify object storage + CDN, not a giant SQL table.

    Components to put on the board

    Web Client, Mobile Client
        → Load Balancer / API Gateway
        → Paste Service (stateless, mark distributed)
            ↔ Key Generation Service → Key DB (unused / used keys)
            ↔ Metadata DB (SQL)
            ↔ Blob Store (S3; paste bodies)
            ↔ Redis (hot pastes; TTL ≤ expiry)
            → CDN (read path)
        → Worker (expiry / cleanup)
    
    • Paste Service. Create: mint key, put object, insert metadata, return URL. Read: cache → metadata → blob.
    • Key Generation Service. Serves keys in batches so create never runs a uniqueness lottery. Key DB has unused and used tables; a handed-out batch moves to used immediately. Standby replica because the KGS alone is a SPOF; wasted keys on a crash are fine at 68B.
    • Metadata DB. Key, user, timestamps, expiry, size, visibility. Not the body.
    • Blob Store. The paste bytes. Cheap, durable, independent scaling.
    • Redis + CDN. Hot reads never hit S3. CDN for public pastes; cache-control aligned with expiry.
    • Cleanup Worker. Sweeps expired metadata, objects, and cache entries during off-peak windows and returns freed keys to the unused pool. The read path's lazy delete handles anything touched before the sweep.

    Walk one paste: create, then a later read

    1. Client POSTs body + optional expiry to the Paste Service.
    2. Service takes a key from its KGS batch (or refills from the Key DB).
    3. PUT body to Blob Store at pastes/{key}; INSERT metadata; SET cache with TTL.
    4. Return https://pastebin.example/{key}.
    5. Later GET: CDN or Redis hit for hot pastes; miss loads metadata then the object, then fills cache.
    6. At expiry the Worker deletes metadata + object + cache. A GET after that is 404, not a ghost object.

    GET on an expired paste: the read path checks expire_at before serving. Expired → 404 (or 401 for permission failures) and delete lazily right there; the off-peak Worker sweeps whatever was never touched, and the freed key returns to the unused pool. Expired content may persist physically for a while — it is never served.

    Collision / enumeration: random keys with no KGS still need a uniqueness check (duplicate-insert + retry); sequential keys fail the security probe.

    Cache vs expiry: a cache TTL longer than paste expiry serves deleted content. TTL must be min(popularity window, remaining life).

    Schema

    Pastes(key PK, user_id, created_at, expire_at, size_bytes, visibility, content_uri)
    Keys(key PK, status: unused|used, allocated_to, allocated_at)
    Users(id, email, plan)   — optional, for custom URLs and delete-auth
    
    • Partition metadata by hash of key if it outgrows one primary. Range-by-prefix is the obvious wrong answer: some prefixes run hot and servers unbalance. Consistent hashing eases rebalancing. Content is already sharded by the object store
    • Never store the body in Pastes

    Scaling and wrap-up

    • Stateless Paste Service scales horizontally; Key DB and metadata primary are the write bottlenecks
    • Multi-region: replicate metadata, origin-shield the blob store, CDN at the edge
    • First metrics: create latency, cache hit rate, 404-after-expiry (cleanup lag), key-pool depth
    • Analytics trap: a per-view UPDATE on the metadata row turns a viral paste into a hot-row write storm. Aggregate views in a log/stream and update counts asynchronously
    • If time: rate limiting on create via api_dev_key quotas (spam), private pastes (permission in metadata, checked on read — unguessable keys are not access control, links leak), malware scan of content, burn-after-read pastes

    Equally defensible variants

    Snowflake IDs encoded in Base62 instead of a KGS (must still break sequentiality). Random + collision-retry at this write rate (12/sec — the retry loop is not yet a hotspot). KV (DynamoDB/Cassandra) for metadata instead of SQL — billions of small rows with no joins favor KV at the high end. Memcached instead of Redis. Content-hash keys are the one variant that is not defensible without naming its costs: cross-user collisions, truncation collisions, precomputable URLs. What matters is that you can argue the tradeoff.

    More in System Design Interview Prep: AI Coach Edition

    • SD Coach · From One Server to Millions
    • SD Coach · Back-of-the-Envelope Estimation
    • SD Coach · Building Blocks
    • SD Coach · Building Blocks, One Server to Millions (Slides)
    • SD Coach · The Data Layer
    • SD Coach · Unique IDs and the URL Shortener
    • SD Coach · News Feeds and Typeahead
    • SD Mock · Design Facebook News Feed
    • SD Mock · Design Twitter
    • SD Mock · Design Typeahead Suggestion
    • SD Mock · Design Twitter Search
    • SD Coach · Rate Limiting
    View the full course (43 sessions) →