Home›Design›Social feed
Course · Whole systemsLesson 32 of 330 read
Design doc · Social feed

Social feed and timelines

How do I build a home timeline that shows posts from everyone I follow?

Applications like this: social networks, activity feeds, notifications. This page is written the way I write a database design doc: the workload first, then where each kind of data goes and why, then how it grows, what breaks, and what I rejected.

The short answerPosts and the follow graph in a relational database. Timelines precomputed per user in Redis or a wide-column store (fan-out on write), except for accounts with huge followings, whose posts are merged in at read time.
1 · The workload

What the application asks of the database

DimensionThis application
WritesPosts are moderate. Fan-out multiplies each post by the number of followers.
ReadsVery high. Opening the app reads a timeline.
Read to writeHigh, often more than 100 to 1.
ConsistencyLoose. A post can appear a few seconds late.
LatencyThe first screen of the timeline in well under a second.
GrowthFast, driven by user count times follows.
Connected

Real failures behind this design

These incidents are why the choices below matter. Each one is written up with its numbers and sources.

2 · What goes where

Each kind of data, its store, and why

ApplicationUsers, posts, followsPostgreSQL or MySQLSOURCE OF TRUTHPrecomputed timelinesRedis lists or sorted sets, or Cassandra and ScyllaDB atlarge scaleCOPYPost content and mediaThe database for text, object storage and a CDN formediaSOURCE OF TRUTH

Solid arrows: the application writes there. Dashed arrows: a copy fed from the source of truth (change data capture, an outbox, or a cache fill), which can be rebuilt from it.

DataWhere it goesWhy
Users, posts, followsPostgreSQL or MySQLThe record of who posted what and who follows whom.
Precomputed timelinesRedis lists or sorted sets, or Cassandra and ScyllaDB at large scaleA timeline is a list of post IDs per user, read as one key. Writes are many and small, which suits memory or an LSM store.
Post content and mediaThe database for text, object storage and a CDN for mediaMedia is large and read often, which is a CDN job.

How each family of store behaves under traffic: Database families.

3 · Data model

The core tables and the queries they serve

-- record of truth
CREATE TABLE follows (follower_id bigint, followee_id bigint,
                      PRIMARY KEY (follower_id, followee_id));
CREATE INDEX ON follows (followee_id);

# timeline per user, newest first, capped at 800 entries
ZADD timeline:42 1763452800 post:9001
ZREMRANGEBYRANK timeline:42 0 -801
ZREVRANGE timeline:42 0 49
Step through it

One request, followed through the design

Step through fan-out on write, then the large-account case. Press Play, or use the arrows.

4 · How it grows

Start simple, change one thing at a time

  1. Pull modelAt small scale, query the posts of everyone I follow, newest first, with a good index. No precomputation.
  2. Reads get expensivePrecompute timelines in Redis with fan-out on write.
  3. Memory gets expensiveMove timelines to a wide-column store partitioned by user_id, with only the hot ones in Redis.

Scale it: watch this design grow

Pick a stage, then throw something at it. The numbers are a model to show the shape of each change, not a measurement; the measured numbers are in the lab notebooks.

5 · What breaks first

The failures to plan for

What happensWhat you seeWhat I do
A celebrity postsThe fan-out queue backs up for minutesDo not fan out for accounts above a follower threshold. Merge at read time.
Pull queries at scaleA big IN list of followees sorted by timePrecompute.
Timelines that never expireMemory grows with inactive usersCap each timeline, and drop timelines of users who have not opened the app in weeks.
6 · Alternatives

What I considered and did not pick

Cost. Memory for timelines is the big line. Cap timeline length and drop inactive users' timelines.

How this page was writtenFrom the engines' documentation and common practice. Measured on real servers: Redis sorted sets, the structure behind precomputed timelines, in the notebooks. Before you build: check the anti-patterns, size it with the capacity calculator, and write your own version from the design doc template. Want a second opinion? Submit it for review.
7 · Where to run it

On-prem, managed, EC2 or Kubernetes

StageWhere it runs
Where I would startManaged relational, plus managed Redis
When it growsManaged or self-managed wide-column store on VMs with local NVMe and replicas

For a team without a dedicated database team. Who does which job in each option, and the full-cost comparison: Where to run the database.

8 · Estimate it

The numbers, worked live

The back-of-the-envelope numbers I would want before choosing anything. Move the assumptions to match your application. The link keeps your numbers, so a colleague sees the same ones.

Part 3 of 4 · Design · 24 pagesAll pages

Which database does my application need? What kind of application data goes in which database, and why, as design docs, plus the anti-patterns and the decisions behind them.