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.

Video lesson · Chapter 1 of 5

A phone publishes a twenty-four-hour story

0:00 / 4:03

Theory

Read or listen first. About 2 min.

What it is

A story is a photo or short video that followers can see for 24 hours. A row of circles, the tray, shows which followed accounts have something to watch. Tapping a circle plays that person's items in order.

Two features make it different from a feed. Everything has an end time, which touches the database, every cache and the files themselves. And viewing is sequential, so the app knows what comes next and can fetch it early.

Why it exists

People share more casually when a post does not stay on their profile for ever. The end time is a promise to the author, so showing a story late is a broken promise, not a cache detail.

It is a good interview question because the first answer ("use a TTL") is only a quarter right. Expired rows, files, caches and half-watched videos each behave differently.

A short history

  1. 2007

    Flajolet, Fusy, Gandouet and Meunier publish HyperLogLog, a small sketch that estimates how many different items were seen.

  2. October 2013

    Snapchat introduces "My Story", snaps arranged in time order for friends.

  3. 2 August 2016

    Instagram announces Stories: photos and videos that disappear after 24 hours, shown in a bar above the feed with a coloured ring for something new.

  4. August 2017

    RFC 8216 documents HTTP Live Streaming, which splits video into short files a CDN can cache.

  5. January 2019

    Instagram Stories is reported to be used by 500 million people a day.

Words to know

Tray
The row of circles: followed accounts with at least one live story.
TTL
Time to live: a lifetime attached to stored data.
Expiry check
Comparing the end time with now on every read.
Active index
A small table of who has live stories, and until when.
Seen mark
The last item of an author this viewer watched.
Signed URL
A media address with an end time and a signature, so it cannot be shared for long.
Prefetch
Downloading the next item before it is asked for.
HyperLogLog
A sketch that estimates distinct viewers in up to 12 KB.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

Understand it

Picture it

Chalk drawings on the pavement

A square that is blank every morning
A drawing for one dayuntil 8 tomorrow

Closing and washing are two different jobs. Only closing has to be on time.

Scope

What are we building?

A service like Instagram Stories: upload, a personal tray, sequential viewing, a seen-by list, and an end after 24 hours. The company's real system is not described here.

Similar applications

ProductThe twist
Snapchat StoriesIntroduced the format in 2013
WhatsApp StatusShared with phone contacts instead of followers
Facebook StoriesThe same idea beside a feed
YouTube ShortsVertical video without an end time
TikTokVertical video chosen by recommendation, not by follows

Why it is interesting

  • Ending

    Five places hold a story. They must agree that it is over.

  • Tray

    Who has something new, out of 500 follows, in 200 ms.

  • Instant play

    Half a second from tap to first frame, on mobile networks.

  • Counting

    25 billion views a day, each person once.

The finished system

Where the design ends up

This is the last version. Design starts with one service and one database and shows which number forces each box.

The whole system

People reach a CDN for media and a gateway for everything else. The gateway leads to the story, tray and view services. The story service writes Cassandra with a lifetime; changes go to Kafka, which updates the active index in the tray service's memory. Media is uploaded to S3, processed by media workers and served by the CDN. Views go through Kafka to view workers, which write view rows to Cassandra and counts and seen marks to Redis.
Blue is viewing, pink is posting, orange happens later. Dashed means asynchronous or only on a miss.

Where to read what

QuestionInterview tabConcept page that owns the idea
What must it do?ScopeScoping
How big is it?NumbersBack of the envelope
What is the interface?API and dataFile uploads, idempotency
How does it grow?DesignObject storage, CDN
What is hard?Deep divesCache TTL, sharded counters
Why this choice?ExplainPush and pull
Can I do it aloud?Mock45 minutes

The neighbour problem is the news feed, where copying to followers wins. Here it does not, and the numbers show why.

Summary

The whole topic in one minute.

A story's end time is checked on every read. Lifetimes in databases, caches and object storage clean up later, at their own pace, and none of them is exact.

The tray is cheap because the list of accounts with live stories is small: 200 million entries of 40 bytes is 8 GB, so each tray server keeps it and looks up the 500 accounts a viewer follows. Media goes straight to object storage and out through a CDN. Views are events, made unique by key, counted exactly when few and estimated when many.

  • Check the end time on every read; let TTLs clean up.
  • The list of accounts with live stories is 8 GB. Keep it near and pull.
  • Media never passes through our services.
  • A view has the key (story, viewer), so repeats cost nothing.
  • Exact counts for small stories, HyperLogLog for a few huge ones.
  • Private media needs short-lived signed addresses, not secret names.

Sources