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.
| Dimension | This application |
|---|---|
| Writes | Posts are moderate. Fan-out multiplies each post by the number of followers. |
| Reads | Very high. Opening the app reads a timeline. |
| Read to write | High, often more than 100 to 1. |
| Consistency | Loose. A post can appear a few seconds late. |
| Latency | The first screen of the timeline in well under a second. |
| Growth | Fast, driven by user count times follows. |
These incidents are why the choices below matter. Each one is written up with its numbers and sources.
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.
| Data | Where it goes | Why |
|---|---|---|
| Users, posts, follows | PostgreSQL or MySQL | The record of who posted what and who follows whom. |
| Precomputed timelines | Redis lists or sorted sets, or Cassandra and ScyllaDB at large scale | A 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 media | The database for text, object storage and a CDN for media | Media is large and read often, which is a CDN job. |
How each family of store behaves under traffic: Database families.
-- 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 fan-out on write, then the large-account case. Press Play, or use the arrows.
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.
| What happens | What you see | What I do |
|---|---|---|
| A celebrity posts | The fan-out queue backs up for minutes | Do not fan out for accounts above a follower threshold. Merge at read time. |
| Pull queries at scale | A big IN list of followees sorted by time | Precompute. |
| Timelines that never expire | Memory grows with inactive users | Cap each timeline, and drop timelines of users who have not opened the app in weeks. |
Cost. Memory for timelines is the big line. Cap timeline length and drop inactive users' timelines.
| Stage | Where it runs |
|---|---|
| Where I would start | Managed relational, plus managed Redis |
| When it grows | Managed 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.
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.
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.