Skip to main content
We’ll build a real news feed using the fan-out-on-write pattern. With fan-out-on-write, when an author publishes a post, a worker proactively copies that post into each follower’s personal feed storage. The benefit: follower reads are pre-populated and return instantly. The trade-off: increased write volume and duplicated storage. This article describes the end-to-end flow, provides a simple worker implementation (with batching and idempotency), compares storage choices, and lists operational best practices and scaling patterns.

How fan-out-on-write works (high level)

  1. A user creates a post. The post is written to the canonical posts table/stream.
  2. The publish event is appended to a durable message stream or queue (Kafka, Pulsar, Kinesis, Redis Streams, etc.).
  3. A fan-out worker consumes the stream and computes the follower set for the author.
  4. The worker writes a per-follower feed entry (often a small summary or pointer to the canonical post) into each follower’s personal feed store.
  5. Followers read their personal feed directly; reads are fast because feeds were pre-populated.
This is the simplest end-to-end flow for achieving very low read latency for user feeds.

Example worker (pseudocode)

The following Python-like pseudocode demonstrates a fan-out worker loop. It emphasizes batching, idempotency, and retry behavior.
Notes about the pseudocode:
  • Use entry_id as an idempotency key so retries do not produce duplicates.
  • Keep per-follower entries small (summary or pointer) to limit write amplification and storage costs.
  • Batch follower writes to reduce RPC overhead and increase throughput.
  • Persist a durable marker (or offset) of processed events so the consumer can resume safely.
  • For very large follower lists, shard the fan-out work across multiple workers by author, by post, or by follower ranges.

Storage options for per-follower feeds

Choose the per-follower feed store based on your read-latency requirements, write throughput, storage cost, and operational familiarity.

Important operational considerations

  • Write amplification: one post may be written once per follower. Expect higher IOPS and storage usage.
  • Hot authors: accounts with millions of followers create hot shards and cause large write spikes.
  • Ordering: preserve timestamps; use monotonic counters or server-side timestamps if strict ordering is required.
  • Consistency: fan-out is typically eventually consistent — followers may see a brief delay while writes propagate.
  • Idempotency: use stable entry keys to make retries safe.
  • Backpressure: handle spikes using batching, sharding, worker autoscaling, and circuit breakers.
  • TTL and compaction: enforce retention (TTL) or compaction to bound per-user storage.
  • Privacy and deletes: define a strategy for deletes and privacy changes (cascade deletes, tombstones, or access checks at read time).
  • Monitoring: track consumer lag, queue depth, fan-out latency, write error rates, and per-shard throughput.
Accounts with extremely large follower counts (celebrities) are a major operational risk: they can cause sustained high write volumes and create hot partitions. Plan special handling (hybrid fan-out, rate-limiting, separate shards, or read-time fetch for followers) to protect the rest of the system.

Scaling patterns and hybrids

  • Fan-out on write (proactive): precompute and store each follower’s feed. Best: read latency. Trade-off: highest write cost and storage duplication.
  • Fan-out on read (lazy): store canonical posts and follower lists; assemble feed at read time. Best: reduced write amplification. Trade-off: higher read latency and more CPU at read time.
  • Hybrid approaches:
    • Fan-out to active followers (heavy readers) and fan-in on read for inactive followers.
    • Treat super-followed authors specially: store their posts in a shared high-throughput timeline that is merged at read time.
    • Use tiered storage: keep recent items in Redis for low latency; older items in cold storage (wide-column DB).

Summary

Fan-out-on-write pre-populates followers’ feeds at write time to deliver instant reads. It is ideal when read latency is the priority and extra write/storage cost is acceptable. To operate this pattern at scale, pay attention to batching, idempotency, hot-key handling, retention policies, and observability.
Fan-out-on-write delivers excellent read performance but increases write amplification and storage. Use hybrid strategies and special-case hot authors to balance cost and latency.

Example checklist before production rollout

  • Implement idempotent writes (entry_id per follower).
  • Add batching and worker sharding.
  • Define TTL/retention policy and compaction strategy.
  • Add monitoring for consumer lag, write failures, and hot keys.
  • Design special handling for high-follower-count accounts.
  • Test recovery and resume semantics for the consumer stream (offset management).

Watch Video

Practice Lab