The real cost of a social feed: fan-out on write vs read
Feed architectures on a per-operation database: query-time merge, per-follower inboxes and the hybrid. With the cost arithmetic and the Firestore code.
Every social product builds the same screen: a vertical list of posts from the accounts you follow, newest first, infinite scroll. It looks like a query. It is really a choice about when to pay: when someone posts, or when someone reads. On a per-operation database such as Firestore, where every document read, write and delete is a line item, that choice is most of the bill.
There are three designs, and the right one depends on three numbers you can measure: how often people read compared with how often they post, how follower counts are distributed, and how stale a feed may be.
Three ways to build the screen#
Fan-out on read (pull, or query-time merge). Each post is stored once in posts. To build a feed you look up whom the reader follows, query those authors' posts, merge by time and return the top page. Writes are trivial. Reads do the work.
Fan-out on write (push, or an inbox per follower). When someone posts, a job writes a small entry into every follower's feed collection. A feed page is one query on the reader's own inbox. Reads are trivial. Writes and storage do the work.
Hybrid. Push for ordinary authors, pull for authors above a follower threshold. The reader's page merges their inbox with a few live queries for the high-follower accounts they follow. An ordinary post costs a bounded number of writes, and a celebrity post costs one.
| Approach | Cost of one post | Cost of one feed page | How it fails | Fits |
|---|---|---|---|---|
| Fan-out on read | 1 write | Grows with accounts followed | Reads multiply with follow count | Small graphs, early products |
| Fan-out on write | 1 write plus one per follower | One query on the reader's inbox | One huge author becomes millions of writes | Bounded follower counts, read-heavy traffic |
| Hybrid | As push for ordinary authors, 1 write for large ones | One inbox query plus a few author queries | Two code paths to keep correct | A real follower long tail |
The arithmetic#
Before a large-scale social platform I'm building had meaningful traffic, I made myself a rule: no feature ships until I can write down on one page what it costs per user per day at a million users, and the feed was the first to go through it. I put both shapes into a spreadsheet with the assumptions written out: average follows per user, posts per active user per day, feed opens per day and page size. The numbers below are a hypothetical product, with round numbers you should replace with your own:
- 100,000 readers open the feed per day, 5 times each, 20 posts per page.
- Each reader follows 200 accounts, and each author has 200 followers.
- 50,000 new posts per day.
Fan-out on read. A Firestore in filter accepts a limited number of values; at the time of writing the documented cap is 30 disjunctions, so plan for 30 author ids per query. Following 200 accounts means 7 queries. Each has to fetch a full page of 20 candidates, because any one chunk could own the entire page. That is 140 document reads to show 20 posts, and 120 of them are discarded. Per day: 100,000 x 5 x 140 = 70 million reads.
Fan-out on write. Each post is copied to 200 inboxes: 50,000 x 200 = 10 million writes per day. A page is 20 inbox reads plus, because inbox entries should be references (more on that below), 20 reads to hydrate the posts: 40 reads. 100,000 x 5 x 40 = 20 million reads. Pruning old inbox entries adds deletes, up to roughly another 10 million a day at steady state.
So push trades 50 million reads for about 10 million writes and 10 million deletes. With R, W and D standing for the per-operation prices of a read, a write and a delete in your region, push is cheaper while
10M x W + 10M x D < 50M x R which is W + D < 5 x RTake the three prices from the Firestore pricing page for the region you use and the inequality answers itself. Storage for the inbox entries is a further cost on the push side that the formula leaves out. Writes are priced above reads, so the margin is not wide, and it is the follower and read-ratio assumptions that move the result, not the arithmetic.
Push copies posts to followers who may never open the app again. If a third of followers were active in the last month, two thirds of the writes are pure cost, so fan out to recently active users and backfill the rest when they return. And the mean follower count lies: one account with 5 million followers is 5 million writes for a single post, the same volume as 25,000 posts from accounts with 200 followers. Plan from the distribution.
What tips the decision each way#
The most useful output of my own model was a sensitivity rather than a winner. Once I assumed even a modest skew, with a small number of accounts followed by very many people, pure push grew a hot tail: one popular account posting a few times a day dominated the write bill. Pure pull punished the behaviour I wanted more of, people opening the app often. Start with fan-out on read, and mean it. For a few thousand users it is the least code, nothing needs repairing when a rule changes, and the costs above are small in absolute terms. Move to push when one of two things hurts: the read bill, or the latency of a page for people who follow many accounts. Move to the hybrid when the first author reaches a follower count where one post means an unreasonable burst of writes, which your own metrics will show well before it becomes a problem.
Three conditions favour push: reads outnumber posts by an order of magnitude, follower counts are bounded or can be bounded, and a delay of seconds to minutes in a follower's feed is acceptable. Two conditions rule it out or limit it: a ranked feed (an inbox ordered by time is the wrong primitive for relevance scoring, which needs candidate generation) and a very heavy tail of followers, which is what the hybrid exists for.
The design I would build toward has four rules:
- The post document is the only source of truth. Inbox entries are references, never copies.
- Fan-out is asynchronous and idempotent, driven by a queue, so the post request returns quickly and retries are safe.
- The push-or-pull threshold is a per-author flag evaluated at fan-out time, so it can be tuned without migrating data.
- One opaque cursor drives pagination across both sources.
The data model#
posts/{postId}
authorId, text, createdAt, sortKey, deleted
follows/{followerId}_{authorId}
followerId, authorId, createdAt
users/{uid}/feed/{sortKey}
postId, authorId, sortKey
users/{authorId}
followerCount, fanout # "push" or "pull"sortKey is a string that sorts the way you paginate: a fixed-width millisecond timestamp followed by the post id, such as 0001789012345678_p_8f2c. It is unique, so it works as a cursor on its own and avoids paginating on a timestamp two posts can share. It is also the inbox document id, which is what makes fan-out idempotent: a retried write lands on the same document instead of creating a duplicate.
The follow edge lives in a top-level follows collection with a composite id, so following twice is the same document. Both directions are single-field queries.
Fan-out in pages#
Never fan out inside the request that creates the post, and never in a single loop. Publish a job carrying the post id and a page cursor, and let a worker handle one page of followers per invocation. A page of 500 keeps each job short. The handler must be idempotent, since a redelivered job rewrites the same inbox documents.
import { getFirestore, FieldPath } from "firebase-admin/firestore";
const db = getFirestore();
const PAGE = 500;
type Job = { postId: string; after?: string };
export async function fanOutPage({ postId, after }: Job): Promise<Job | null> {
const post = await db.doc(`posts/${postId}`).get();
if (!post.exists || post.get("deleted")) return null;
const { authorId, sortKey } = post.data()!;
let q = db
.collection("follows")
.where("authorId", "==", authorId)
.orderBy(FieldPath.documentId())
.limit(PAGE);
if (after) q = q.startAfter(after);
const followers = await q.get();
let failed = 0;
const writer = db.bulkWriter();
writer.onWriteError((err) => {
if (err.failedAttempts < 5) return true; // let BulkWriter retry
failed++;
console.error(
JSON.stringify({ event: "fanout_write_failed", postId, path: err.documentRef.path, code: err.code }),
);
return false;
});
for (const f of followers.docs) {
const ref = db.doc(`users/${f.get("followerId")}/feed/${sortKey}`);
// Failures are counted and logged in onWriteError; the catch only
// stops the rejected promise from becoming an unhandled rejection.
writer.set(ref, { postId, authorId, sortKey }).catch(() => {});
}
await writer.close();
// Throw so the queue redelivers this page; the writes are idempotent.
if (failed > 0) throw new Error(`fan-out page failed for ${failed} followers of ${postId}`);
return followers.size === PAGE ? { postId, after: followers.docs.at(-1)!.id } : null;
}The caller enqueues the returned job when more followers may remain. Export the failed count as a metric as well as throwing, so a rising failure rate is visible before a job gives up.
Use BulkWriter rather than hand-rolled batches. It retries through onWriteError and ramps its rate up gradually following the 500/50/5 rule: start at 500 operations per second and raise by 50 percent every five minutes, which exists because sudden traffic to a new key range can cause hotspots. The ramp has a consequence for big authors. A 5-million-follower fan-out starting at 500 per second takes on the order of half an hour to reach full speed and finish. That is fine for an asynchronous job, and it is the reason not to promise instant delivery for the largest accounts. The wider story of ramp-up and hot keys is in Firestore data modelling for a million users.
The threshold, and why it is sticky#
The threshold is a dial, not a constant. Mine currently sits at 10,000 followers. I do not claim my split is universal and I expect to retune it with real data, but I know which direction to turn it because the model exists. The decision to push or pull sits at the top of the fan-out handler, before any follower page is read:
const PUSH_LIMIT = 10_000; // a placeholder; tune from your own write budget
export async function shouldPush(authorId: string): Promise<boolean> {
const user = await db.doc(`users/${authorId}`).get();
if (user.get("fanout") === "pull") return false;
return (user.get("followerCount") ?? 0) < PUSH_LIMIT;
}Once an author is marked pull, a dip in followers does not flip them back. An account hovering around the limit would otherwise have posts pushed to some followers and not others. The merge still works, because every post exists in posts anyway: the inbox is an acceleration, not the truth. As long as the merge de-duplicates by postId, getting this wrong costs duplicate work, never a missing post.
Do not keep followerCount as a single document incremented on every follow. A popular account can exceed the sustained limit of about one write per second to one document; use a sharded or periodically recomputed counter, as described in the data modelling article. An approximate count is enough for a threshold.
Reading one page across both sources#
A page is the reader's inbox merged with the live posts of the pull authors they follow. Store the list of followed pull authors on the user and refresh it when they follow or unfollow a large account. It stays small because the pull set is, by construction, the handful of accounts above the threshold.
import { getFirestore } from "firebase-admin/firestore";
const db = getFirestore();
const SIZE = 20;
type Item = { postId: string; sortKey: string };
export async function feedPage(uid: string, cursor?: string, pullAuthors: string[] = []) {
const inboxQ = db
.collection(`users/${uid}/feed`)
.orderBy("sortKey", "desc")
.limit(SIZE);
const inbox = cursor ? inboxQ.startAfter(cursor) : inboxQ;
const pulls: Promise<FirebaseFirestore.QuerySnapshot>[] = [];
for (let i = 0; i < pullAuthors.length; i += 30) {
let q = db
.collection("posts")
.where("authorId", "in", pullAuthors.slice(i, i + 30))
.orderBy("sortKey", "desc")
.limit(SIZE);
if (cursor) q = q.startAfter(cursor);
pulls.push(q.get());
}
const [inboxSnap, ...pullSnaps] = await Promise.all([inbox.get(), ...pulls]);
const items: Item[] = [
...inboxSnap.docs.map((d) => d.data() as Item),
...pullSnaps.flatMap((s) => s.docs.map((d) => ({ postId: d.id, sortKey: d.get("sortKey") }))),
];
items.sort((a, b) => (a.sortKey < b.sortKey ? 1 : -1));
const seen = new Set<string>();
const page = items.filter((i) => !seen.has(i.postId) && seen.add(i.postId)).slice(0, SIZE);
const refs = page.map((i) => db.doc(`posts/${i.postId}`));
const posts = refs.length ? await db.getAll(...refs) : [];
const visible = posts.filter((p) => p.exists && !p.get("deleted"));
return {
posts: visible.map((p) => ({ id: p.id, ...p.data() })),
nextCursor: page.length === SIZE ? page.at(-1)!.sortKey : null,
};
}The cursor is the last sortKey of the merged page, and every source resumes from it with startAfter. That works because the key is globally unique and consistently ordered, so it means the same thing to the inbox and to posts. A reader never sees an item twice and never skips one even though the items came from different queries. Offsets are out for the usual reason: you are billed for the documents you skip.
The pull query combines an in filter on authorId with an order on sortKey, so it needs a composite index on posts (authorId ascending, sortKey descending). The inbox query uses an automatic single-field index. How to declare and deploy indexes is in the data modelling article.
Deletes, unfollows and backfill#
These are the cases that make push feeds hard. Because inbox entries are references, most stay cheap:
- Deleting a post. Set
deleted: true. Readers drop it at hydration, so no fan-out of deletes is needed for correctness, and dangling inbox entries are pruned later. A page can come back with fewer than 20 items; accept that, or fetch the next page and top up. - Hard deletion. When content must be removed and not just hidden, you need every copy. A collection group query finds inbox entries wherever they live, and needs a single-field index with collection group scope on
postId, set as a field override because automatic indexes are collection-scoped. The legal side is in deleting data in distributed systems. - Unfollowing. Delete the
followsdocument, then purge that author's entries from the reader's inbox in the background. Until the purge finishes, filter at read time against the current follow set so the reader never sees posts from someone they just unfollowed. - Backfill on follow. A new follower should see something immediately. Copy the author's last 20 or so posts into the follower's inbox with the same
setcalls, which are idempotent because the id is the sort key. For a pull author there is nothing to backfill; the next read finds their posts. - Cap the inbox. Nobody scrolls back through 50,000 entries. Keep the newest few hundred per user, prune the rest, and fall back to author queries if a reader scrolls past the cap. TTL policies can do the pruning, at the cost of billed deletes and a deletion delay of up to about a day.
What goes wrong in the hybrid#
Eventual consistency is the price of push. A follower can see a post seconds or minutes after the author does, longer for large accounts. Show the author's own post immediately from local state or the post document, so the delay is invisible to the one person who would notice.
The pull set grows. Reader-side cost in the hybrid is proportional to the number of large accounts followed, in chunks of 30. A reader following 300 popular accounts pays 10 extra queries per page. Cap the pull set, or cache it and merge only the top few posts from each author.
A viral account queues millions of writes behind normal traffic. Run large-author fan-out on its own queue with its own concurrency limit so it cannot starve ordinary posts.
A job that dies at 40 percent leaves a partial fan-out. Because writes are idempotent and the job is paged, a retry from the last cursor finishes it. Alert on job age, not only errors, so a stuck fan-out is noticed before followers complain.
A recommendation per situation#
- A few thousand users, or a feed people open rarely: fan-out on read, indexed properly, with a cached follow list. The inbox pipeline is code, queues, indexes and repair jobs you would maintain for no gain.
- Read-heavy, follower counts bounded: fan-out on write with reference inbox entries, paged idempotent fan-out, and fan-out to recently active followers only.
- A real long tail of followers: the hybrid, with a sticky per-author flag and one cursor across both sources. Do not build it before an actual account is large enough that one post is a painful burst of writes.
- Whichever you pick, at this stage choose the cheaper design even if it is slower. A feed that assembles in 400ms instead of 150ms does not kill an early product, whereas a cost shape you only notice after a growth spike needs a migration under load. Latency can be bought back later with caching, and a bad cost shape is structural.
- A ranked rather than chronological feed: none of these on their own. Ranking needs candidate generation and scoring.
- Feed questions that are really graph questions, such as posts liked by friends of friends: look at a second store, which is the subject of documents and graphs.
Related articles
All articles →The Firestore limits that bite as a database grows, and the schema choices that avoid them: hot documents, index fan-out, ramp-up, pagination, rules and TTL.
13 min
Firestore is superb at keyed reads and poor at friends-of-friends. When a graph database earns its place, and how to keep two stores honest.
12 min
Which Postgres schema changes lock a table, which are safe, and the three-release pattern I use to change a live column without an outage or a long window.
12 min