AlgoViz

Design a Pastebin service

easy 45 min

Store a piece of text, hand back a short link, and delete the text on time.

See it run

Every flow, one hop at a time: create, read, make keys, filter through caches, sweep, and choose a home for the text.

Operations

Create a paste

A 40 KB paste: checked, stored as an object, recorded, and then worked on in the background.

Step 1 of 15

The client sends text

Our systemClientGatewayPaste serviceObject storagePostgreSQLRedisQueuescan, highlight

size

40 KB

visibility

unlisted

expiry

1 day

With optional title, language, expiry, visibility and password.

Step 1: The client sends text

def create(req):    limiter.check(req.client)                  # 429 if over    check(1 <= len(req.content) <= 10 * MB)    # 400 or 413    key = random_key(8 if req.public else 12)    packed = gzip(req.content)    path = None    if len(req.content) > 8 * KB:        path = f"content/{key[:2]}/{key}"        objects.put(path, packed)              # object first    db.insert_if_free(key, path, packed if not path else None)    queue.send("scan", key); queue.send("highlight", key)    return 201, key, new_delete_key(key)
Latency
p99 < 500 ms
Load
12/s
Peak (3x)
35/s
Anonymous limit
10 a minute

The paste exists from the moment the row is committed. Everything after that may fail and be repeated.

In the real world

The same paste service doing real jobs. Pick one and step through it.

Both press the button in the same second.

Step 1 of 4

Both see a button

Our systemReader AReader BPaste servicePostgreSQLburn note

GET reveals nothing, so a chat preview is harmless.

Step 1: Both see a button

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Serving until the sweeper comes

    critical

    The paste lives minutes or hours past its time.

    Wrong
    row = db.get(key)return row.text
    Right
    row = db.get(key)if row.deleted or now() >= row.expires_at:    return 410
  • Burning on GET

    critical

    A link preview opens it first and the person finds nothing.

    Wrong
    GET /KEY    # shows the text and deletes it
    Right
    GET  /KEY         # shows a buttonPOST /KEY/reveal  # one conditional update
  • Row before object

    major

    A crash leaves a link with no text.

    Wrong
    db.insert(key, path)objects.put(path, text)
    Right
    objects.put(path, text)db.insert(key, path)
  • private and a CDN hit

    major

    private forbids shared caches from storing it.

    Wrong
    # public pasteCache-Control: private, max-age=300
    Right
    # public pasteCache-Control: public, max-age=60, s-maxage=300
  • One lifecycle rule for all

    critical

    Pastes that should live for ever disappear after 90 days.

    Wrong
    expire everything after 90 days
    Right
    expire prefix exp/ after 90 dayskeep prefix keep/
  • The password in the address

    major

    Addresses are written to logs and histories.

    Wrong
    GET /v1/pastes/KEY?password=...
    Right
    POST /v1/pastes/KEY/unlock{ "password": "..." }

Interview tips

  • Start simple

    • One server and one database, said with numbers.
    • 1M pastes, 10 KB, 12 writes a second.
    • Then storage, cache, edge, front door.
  • Clarify early

    • Largest size? Default expiry?
    • Public, unlisted or private?
    • May the server read the text?
  • Talk about trade-offs

    • Object storage adds delay and requests.
    • Caches add copies to delete.
    • Longer keys add characters.
  • Mention safety

    • Escape, second domain, nosniff.
    • Limits, scan, report, takedown.
    • Passwords hashed with Argon2id.