Scale & throughput · Fan-out on write (with a pull path for very large accounts)

Deliver one post to millions of followers’ timelines within seconds

When someone posts, store the post once and publish an event. Workers look up the followers and push the post id into each follower’s precomputed timeline in a fast cache, so reading a timeline is a single lookup. Accounts with very many followers are not fanned out: their recent posts are merged in at read time.

By Archipelego, version 1.

When to use it

  • Feeds read far more often than they are written
  • Timelines must open in milliseconds
  • Most accounts have a moderate number of followers

When not to use it

  • Strict ordering or consistency across all feeds is required
  • Writes are as frequent as reads
  • Storage for one copy per follower is too costly

Trade-offs

Gains

  • Reading a timeline is one cache lookup
  • Write spikes are absorbed by the event stream
  • Workers scale out independently

Costs

  • Write amplification: one post becomes many timeline entries
  • Very large accounts need the hybrid pull path
  • Eventual consistency: a post appears after a short delay

Ingredients

  • Author
  • Follower
  • Web and mobile app
  • Post API
  • Fan-out workers
  • Timeline API
  • Large-account merge
  • Posts store
  • Post events
  • Social graph
  • Timeline cache

How it is served

AWS

AWS · Single provider

  • Web and mobile app: Amplify + CloudFront
  • Post API: API Gateway + Lambda
  • Posts store: DynamoDB
  • Post events: Kinesis Data Streams
  • Fan-out workers: Lambda consumers
  • Social graph: Neptune
  • Timeline cache: ElastiCache for Redis
  • Timeline API: API Gateway + Lambda
  • Large-account merge: Lambda

Deploy

  1. Create the DynamoDB posts table and the Kinesis stream
  2. Deploy the post and timeline APIs behind API Gateway
  3. Attach the fan-out Lambda to the stream with a batch size of 100
  4. Store timelines as capped Redis lists (for example the last 800 ids)
  5. Route accounts above a follower threshold to the merge path

Google Cloud

Google Cloud · Single provider

  • Web and mobile app: Firebase Hosting
  • Post API: Cloud Run
  • Posts store: Firestore
  • Post events: Pub/Sub
  • Fan-out workers: Cloud Run workers
  • Social graph: Spanner Graph
  • Timeline cache: Memorystore for Redis
  • Timeline API: Cloud Run
  • Large-account merge: Cloud Run

Deploy

  1. Create the Pub/Sub topic with a push subscription to the fan-out service
  2. Deploy the APIs on Cloud Run
  3. Keep timelines as capped Redis lists in Memorystore
  4. Route accounts above a follower threshold to the merge path

Open source

Open source · On premises

  • Web and mobile app: Web and mobile clients
  • Post API: Go service on Kubernetes
  • Posts store: Apache Cassandra
  • Post events: Apache Kafka
  • Fan-out workers: Kafka Streams workers
  • Social graph: Neo4j
  • Timeline cache: Redis Cluster
  • Timeline API: Go service on Kubernetes
  • Large-account merge: Go service on Kubernetes

Deploy

  1. Run Kafka with the post topic partitioned by author id
  2. Scale the fan-out workers with the consumer group
  3. Keep timelines as capped Redis lists
  4. Route accounts above a follower threshold to the merge path

Tags: timeline, feed, social, notifications, cache, events

Open this recipe in Archipelego to pick a variant, follow the method and cook it into a board.