Design disappearing stories like Instagram's
hard 50 minPeople 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
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
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
criticalFiles, caches and addresses keep their own time. S3 alone can be a day behind.
Wrongdb.insert(story, ttl=86400) # done?Rightif item.expires_at <= now: return 410A fixed day on every copy
criticalA copy made late outlives the story.
Wrongredis.set(key, item, ex=86400)Rightredis.set(key, item, ex=item.expires_at - now)Time inside the view key
majorWatching twice becomes two rows and two viewers.
WrongPRIMARY KEY ((story_id), viewed_at, viewer_id)RightPRIMARY KEY ((story_id, bucket), viewer_id)A sketch for every item
majorA billion sketches of up to 12 KB are up to 12 TB, for items with 25 viewers.
Wrongredis.pfadd(f"uniq:{story}", viewer)Rightexact set to 1,000, then the sketchRemoving an author from all trays
majorThere is no way to find the trays except to scan them all.
WrongZREM tray:* author # no such commandRightkeep 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.