Hybrid Feed Fan-Out: Balancing Write Amplification and Tail Latency
How social and activity feeds combine push-on-write and pull-on-read strategies to keep p99 read latency low without melting caches on celebrity spikes.
Designing a timeline or activity feed looks deceptively simple until a single user with ten million followers publishes an update. Pure fan-out-on-write (pushing every post into every follower's Redis sorted set) collapses under write amplification, while pure fan-out-on-read (querying and merging followed timelines at request time) pushes tail latency past acceptable SLOs.
Production feed engines solve this with a hybrid fan-out model: push for normal accounts, pull for high-fanout accounts, and merge at read time.
The Cost Model
Suppose user has followers and posts at rate , while readers query their home timeline at rate while following accounts.
By capping push fan-out at a follower threshold (for example, ), write work per event is bounded by while read-time scatter-gather only touches the small subset of followed accounts that exceed .
Routing Writes via Kafka
When a post is created, the ingestion service writes the canonical record to primary storage and emits a compact event to Kafka. Consumers check the author's follower tier before deciding whether to materialize the post ID into Redis ZSET mailboxes.
interface PostCreatedEvent {
postId: string;
authorId: string;
followerCount: number;
publishedAtMs: number;
}
const PUSH_THRESHOLD = 10_000;
export async function routeFeedFanout(
event: PostCreatedEvent,
deps: {
getFollowerBatch: (authorId: string, cursor?: string) => Promise<{ ids: string[]; next?: string }>;
pushToMailbox: (followerIds: string[], postId: string, score: number) => Promise<void>;
appendCelebrityOutbox: (authorId: string, postId: string, score: number) => Promise<void>;
},
) {
if (event.followerCount > PUSH_THRESHOLD) {
// High-fanout path: write once to the author's outbox cache
await deps.appendCelebrityOutbox(event.authorId, event.postId, event.publishedAtMs);
return { mode: "pull" as const };
}
// Standard path: chunked pipeline fan-out to Redis ZSET mailboxes
let cursor: string | undefined;
do {
const batch = await deps.getFollowerBatch(event.authorId, cursor);
if (batch.ids.length > 0) {
await deps.pushToMailbox(batch.ids, event.postId, event.publishedAtMs);
}
cursor = batch.next;
} while (cursor);
return { mode: "push" as const };
}Read-Time K-Way Merge
On read, the timeline service fetches the pre-materialized page from the user's Redis mailbox alongside the top items from any followed celebrity outboxes, then performs an in-memory k-way merge on (publishedAtMs, postId) before hydrating post payloads in a single batch call.
-- Fetch latest 40 pushed post IDs from user mailbox
ZREVRANGEBYSCORE feed:mailbox:{userId} +inf -inf LIMIT 0 40
-- Fetch latest 40 posts from each followed high-fanout author
ZREVRANGEBYSCORE feed:outbox:{celebrityId} +inf -inf LIMIT 0 40Practical Guardrails
- Trim mailboxes asynchronously: Keep mailboxes capped (e.g.,
ZREMRANGEBYRANK key 0 -801) so inactive users do not accumulate unbounded Redis memory. - Idempotent scores: Encode a deterministic tie-breaker or use Snowflake IDs as
ZSETscores so Kafka consumer retries never duplicate entries. - Graceful degradation: If a celebrity outbox shard times out, serve the precomputed mailbox immediately rather than failing the entire timeline request.
Reactions & Discussion
Technical Dispatches
RSS Feed ↗