Guide

The social timeline case study

Picture a small social app where people post and follow each other. Your home timeline shows recent posts from the people you follow.

Functional vs nonfunctional

A functional requirement says what the app does: "show my timeline". A nonfunctional requirement says how well: fast, reliable, able to grow, easy to change, legal. They are often unwritten, but an app that takes a minute to load is as good as broken.

Timeline by query and polling

The simple design keeps users, posts, and follows in tables. To show a timeline, the database finds who you follow, fetches their recent posts, and sorts them by time. New posts should show up within about five seconds, so each app re-runs the query every five seconds. With 10 million people online, that is 2 million queries a second. If each person follows 200 accounts, that is 400 million post lookups a second.

Fan-out on write

Flip it around: keep a stored timeline for every user. When someone posts, look up their followers and add the post to each follower's timeline, like dropping a letter in each mailbox. One request causing many more is called fan-out. At about 5,800 posts a second and 200 followers each, that is just over 1 million timeline writes a second, far less than 400 million lookups.

Materialized views trade writes for reads

Keeping a query's result stored and updated is called materialization. The stored timeline is a materialized view. Reads are quick, and in a burst of posts the deliveries can wait in a queue. The price: each post now costs much more work.

Handling extreme users

Someone who follows thousands of busy accounts cannot read it all, so dropping some deliveries and showing a sample is fine. A celebrity post would need millions of writes, and dropping them is not fine. Instead, store celebrity posts apart and merge them in when a timeline is read.

Remember

  • Nonfunctional needs are real needs.
  • Fan-out on write: writes = post rate x followers.
  • Materialized views: cheap reads, costly writes.
  • Celebrities are merged in at read time.