In this lesson · topic 2 of 7
Topic 2
Requirements, numbers, API and data model
4 min read
Requirements
Users post short items. Users follow and unfollow accounts. A reader gets a feed of recent posts from followed accounts, newest first, in pages. The feed should load fast. It may be a few seconds behind. Users accept that a post appears a little late. They do not accept that it never appears.
Leave out ranking, ads and search at first, and say that you did. A ranked feed adds a scoring step on top of the same candidates.
Back of the envelope
These are assumptions for the exercise.
- 10 million daily users, each opening the feed 10 times. That is 100 million reads a day, or about 1,200 a second.
- One post per user a day on average. That is about 115 posts a second.
- 200 follows per user on average. A push feed then makes about 23,000 inserts a second.
- A celebrity with 10 million followers makes 10 million inserts from one post.
- Keep the newest 1,000 post ids for each reader. At 8 bytes each and 10 million users, that is about 80 gigabytes. It fits in memory across a few machines.
Reads are about ten times as frequent as posts, so a design that makes reads cheap looks right. The celebrity post is the case that tests it.
The API
POST /postscreates a post. It returns the post id.POST /followsandDELETE /follows/{account}change who a user follows.GET /feed?cursor=...&limit=20returns the next page of the reader’s feed and a cursor for the page after it.
The data model
Three stores.
- Posts, keyed by post id: author, text, created time. Large and write-once, so it suits a partitioned store. See partitioning.
- The follow graph: for each account, its followers and its follows. You need both directions. Followers drive push, follows drive pull.
- Feed inboxes, keyed by reader: a capped list of post ids, newest first. This is the cache of precomputed feeds.
The feed holds ids, not posts. A read fetches the ids and then fetches the posts. This keeps the inbox small and means an edited or deleted post changes in one place.