AlgoViz

Design disappearing stories like Instagram's

hard 50 min

People post photos and short videos that vanish after 24 hours, and viewers tap through them from a row of circles.

See it run

Follow one story: posted, listed, watched, counted, ended. Then order the circles and start a tray server.

Operations

Post a story

The file goes straight to object storage. A small record with an end time follows.

Step 1 of 11

Capture

Our systemAuthor appStory serviceS3media filesMedia workersCassandrastoriesKafkaTray serversactive index

clip

12 s

size

45 MB

Text, stickers and a poll are added on the phone. Why: instant feedback, no server work.

Step 1: Capture

def upload_address(author, kind, size):    limiter.check(author, "stories", 100, per="day")    return s3.sign_put(kind, size, seconds=3600)def create(author, media_id, audience, now):    media.require_owner(media_id, author)    ends = now + 86_400    db.insert(author, new_id(), media_id, audience,              status="processing", expires_at=ends, ttl=ends - now)    return 201, endsdef on_media_ready(story, now):    db.set_status(story, "live", ttl=story.expires_at - now)
Latency
3 s photo, 10 s video
Load
11,600/s
Peak (3x)
35,000/s
Media in
3 PB a day

The second write uses the remaining time. A fresh 86,400 would let that column live longer than the row.

In the real world

The same disappearing stories doing real jobs. Pick one and step through it.

One story, enormous interest.

Step 1 of 5

Post

Our systemAuthor appStory serviceS3media filesMedia workersCassandrastoriesKafkaTray serversactive index

tray writes

0

One row, one event. Each tray server changes one entry. Not one follower's tray is written.

Step 1: Post

Common mistakes

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

  • Trusting TTL for the rule

    critical

    Files, caches and addresses keep their own time. S3 alone can be a day behind.

    Wrong
    db.insert(story, ttl=86400)   # done?
    Right
    if item.expires_at <= now: return 410
  • A fixed day on every copy

    critical

    A copy made late outlives the story.

    Wrong
    redis.set(key, item, ex=86400)
    Right
    redis.set(key, item, ex=item.expires_at - now)
  • Time inside the view key

    major

    Watching twice becomes two rows and two viewers.

    Wrong
    PRIMARY KEY ((story_id), viewed_at, viewer_id)
    Right
    PRIMARY KEY ((story_id, bucket), viewer_id)
  • A sketch for every item

    major

    A billion sketches of up to 12 KB are up to 12 TB, for items with 25 viewers.

    Wrong
    redis.pfadd(f"uniq:{story}", viewer)
    Right
    exact set to 1,000, then the sketch
  • Removing an author from all trays

    major

    There is no way to find the trays except to scan them all.

    Wrong
    ZREM tray:* author      # no such command
    Right
    keep one index entry; check ends on read

Interview tips

  • Say the rule first

    • The read decides, the TTL cleans.
    • List the places a story lives.
    • Copies get the remaining time.
  • Compare push and pull

    • 5.8M writes against 29M lookups.
    • Then: 8 GB, in memory.
    • Say why a feed is different.
  • Count like an engineer

    • Distinct by key.
    • Exact to 1,000, sketch above.
    • 81,000 error at 10 million.
  • Finish with trust

    • Short signed addresses.
    • Safety check before others see it.
    • A monitor for late reads.