Chapter 11: Design a News Feed System

Loading audio…

ⓘ This audio and summary are simplified educational interpretations and are not a substitute for the original text.

If there is an issue with this chapter, please let us know → Contact Us

The architecture is detailed through two primary workflows: feed publishing and news feed building. The feed publishing workflow begins with a client request sent to web servers, which enforce authentication and rate limiting before the request is routed through a load balancer. A dedicated Post Service persists the content in a database and cache, while the crucial Fanout Service is responsible for efficiently distributing the new post to the friends' news feeds. To address the fundamental challenge of distribution scalability and the 'hotkey problem' associated with high-follower users, the design adopts a hybrid fanout model, utilizing the faster fanout on write (push model) for the majority of users, but employing fanout on read (pull model) for users with large follower counts to prevent system overload and unnecessary resource waste for inactive accounts. The Fanout Service relies on a Graph Database to retrieve friend IDs, a User Cache to filter posts based on user settings (such as muting), and a Message Queue to facilitate asynchronous processing by Fanout Workers who ultimately update the News Feed Cache with only post identification and user identification pairs, which keeps memory consumption low. The second flow, news feed retrieval, describes how a client fetches their feed: the News Feed Service initially retrieves a list of post IDs from the News Feed Cache and then performs a vital 'hydration' step, fetching the full post content, user profiles, and associated metadata from the Post and User Caches before compiling the fully rendered news feed in JSON format. The system heavily relies on a multi-layered cache architecture, segmented into five layers: News Feed IDs, Content (with a separate hot cache for popular posts), Social Graph data, Action tracking, and various Counters. Finally, the chapter addresses critical scalability considerations, including database scaling techniques like sharding and replication (master-slave), emphasizing the need to keep the web tier stateless, maximize caching, and utilize messaging queues for loose coupling of components.