Frontend System Design9 min readPremium

Design Facebook Newsfeed – Frontend System Design

Facebook Newsfeed frontend system design architecture

 

Welcome to our in-depth exploration of building a Facebook-like NewsFeed feature from a frontend system design perspective. This comprehensive guide will walk you through the essential phases of creating an engaging and robust NewsFeed that users love interacting with daily.
We’ll design Facebook News Feed step by step, from requirements to client architecture.


Introduction

The News Feed is the heart of any social media platform, serving as the central hub for user interaction and content discovery. Designing such a feature involves a meticulous approach to understanding user requirements, crafting a scalable architecture, ensuring seamless API communication, and maintaining high performance. Moreover, we must not overlook crucial aspects such as security and accessibility to provide all users an inclusive and safe environment.

In this article, we’ll delve into the core components of system design for a NewsFeed, covering:

  • Clarifying Questions: Outlining the initial inquiries to shape our design approach.
  • Requirements: Detailing both functional and nonfunctional aspects that our NewsFeed must fulfill.
  • High-Level Architecture Design: Sketching the blueprint of our system’s structure.
  • API Design: Defining how our front end will communicate with the server and fetch data.
  • Optimizations: Identifying techniques to enhance performance and user experience.
  • Security and Accessibility: Addressing the safeguarding of user data and ensuring accessibility for a diverse user base.

By the end of this article, you’ll have a clear roadmap to designing a NewsFeed that not only functions effectively but also scales gracefully with your user base and stands the test of time.

Clarifying Questions for NewsFeed System Design

  • What are the core features to be supported?
  • What kind of posts are supported?
  • What pagination UX should be used for the feed?
  • Will the application be used on mobile devices?
  • Any UX we would like offline?

Requirements

Functional Requirements

The NewsFeed we are aiming to design will focus on the following user-centric functionalities:

  • Display a curated list of posts from the user and their friends, promoting social interaction and content engagement.
  • Facilitate liking and reacting to feed posts, allowing users to express their emotions and thoughts dynamically.
  • Enable the creation and publishing of new posts, empowering users to share their experiences and updates effortlessly.
  • Support primarily text and image-based posts, potentially expanding to other types of content as the application evolves.
  • Implement infinite scrolling for a fluid and uninterrupted browsing experience, loading content as the user scrolls.

Nonfunctional Requirements

To complement the functional aspects, our system will also adhere to key nonfunctional requirements:

  • Performance: The NewsFeed must be optimized for quick load times and responsive interactions, even under heavy user load.
  • Internationalization will support multiple languages and regional settings, catering to a global user base.
  • Accessibility (A11y): The design will be inclusive and meet accessibility standards to serve users with different abilities.
  • Observability: Incorporate comprehensive logging and monitoring to track system health and user activities.
  • Security: Robust security measures will be in place to protect user data and privacy.
  • Best Practices: The application will be developed using industry-standard code quality, maintainability, and scalability best practices.
  • Mobile Friendliness: NewsFeed will offer a seamless mobile experience responsive to various device sizes.
  • Offline Accessibility: Though not a requirement for basic functionality, the ability to access specific data offline will be considered a valuable feature.

High-Level Architecture Design

Facebook Newsfeed component architecture

The design of our NewsFeed system employs a classic MVC (Model-View-Controller) architectural pattern, which will be discussed in the context of both client-side and server-side operations.

Server-Side Architecture

The backend, depicted as a ‘Blackbox Server’, interacts with the frontend through a well-defined API, transmitting data in JSON format. This abstraction lets us focus on the frontend design, assuming the server can provide the necessary endpoints and handle business logic, database operations, and authentication securely and efficiently.

Client-Side Architecture

On the client side, our application is split into several key components:

  • Controller: Acts as the intermediary between the View layer and the Data Model, handling user interactions, communicating with the backend API, and managing the application’s state.
  • Data Model (Store): A centralized store for the application’s state, including the list of feed items, user data, and other relevant state information. This store reacts to actions dispatched by the Controller to update the state.
  • View Layer: Contains:
    • Feed Container View: The main container holds individual feed items and the post composer.
    • Post Composer: The interface through which users create new posts.
    • Feed Item View: This component is responsible for rendering individual feed items and is further broken down into:
      • Header View: Displays the author’s information and post metadata, including timestamps.
      • Body View: Displays the main content of the feed post, such as text, images, or videos.

The client architecture supports the dynamic addition of content through the Post Composer and the infinite scroll functionality, enabling a continuous flow of feed items as users scroll through the NewsFeed.

Client Data Model

Facebook Newsfeed data model

The client data model is integral to managing state within our NewsFeed application. It ensures data is organized, easily accessible, and maintains integrity throughout user interaction. Below are the code structures for the data models that represent the state of our application.

App State Model


  const App = {
    user: User,
    settings: UserSettings,
    feed: Feed
  };
  

User Model


  const User = {
    name: 'string',
    // ...other user properties
  };
  

User Settings Model


  const UserSettings = {
    timeZone: 'string',
    accessibilityPreferences: AccessibilityPreferences,
    // ...other settings
  };
  

Feed Model


  const Feed = {
    items: 'List[FeedItem]',
    page: 'int',
    size: 'int'
  };
  

Feed Item Model


  const FeedItem = {
    type: 'FeedItemType',
    creationDate: 'string',
    author: User,
    content: 'FeedItemContent',
    comments: 'List[FeedItemComments]'
  };
  

Feed Item Content Model


  const FeedItemContent = {
    title: 'StyledText',
    body: 'StyledText',
    media: 'Media',
    // ...other content properties
  };
  

Styled Text Model


  const StyledText = {
    // Rich text, colors, links, mentions...
    // Styled text structure driven by server UI
  };
  

These models will be utilized to store data related to the user, their settings, and the feed content, including individual feed items and associated comments. The StyledText The structure provides flexibility for rich text formatting, essential for a dynamic and engaging News Feed.

API Design and Communication Options

Choosing the Right API Communication Protocol

In the design of our News Feed’s API, selecting the appropriate communication protocol is crucial. The leading contenders include RESTful APIs, GraphQL, WebSockets, and Server-Side Events (SSE). Here’s a comparative overview:

Protocol Pros Cons Suitability
REST API Simple, HTTP/2 compatible, easy load balancing Longer latency, connection timeouts, and traffic overhead Basic CRUD operations
GraphQL Modern, fetch exactly what you need, type-safe Can have longer latency if not optimized Complex, interrelated data structures
WebSockets Real-time, bidirectional, single TCP connection Resource-intensive, can be overkill for simple operations Real-time features
Server-Side Events Efficient, HTTP/2 compatible, server-to-client only Unidirectional, limited to text data Real-time updates from server to client

For our News Feed, we chose GraphQL due to its ability to handle complex, interrelated data with efficiency and precision. It allows the client to request precisely what is needed, reducing data overfetching and ensuring type safety. While GraphQL may introduce some latency due to its complexity, proper optimization can mitigate these concerns, making it an ideal choice for our application’s needs.

API Endpoints

Our API will consist of the following endpoints, designed to support the functionality of the NewsFeed:

Get Feed


  // Endpoint to get the feed
  GET /api/v1/gql/feed
  input = {
    size: int,
    page: int,
    post_types: List[PostTypes]
  }
  headers = {
    user_id // Authentication and personalization
  }
  

Get Feed Item


  // Endpoint to get a specific feed item
  GET /api/v1/gql/feed-item
  input = {
    post_id: int
  }
  headers = {
    user_id // Authentication and personalization
  }
  

Create Post


  // Endpoint to create a new post
  POST /api/v1/gql/create-feed-post
  input = {
    title: str,
    media: List[Media]
  }
  

These endpoints facilitate the fetching of the feed, individual feed items, and the creation of new posts. The ‘Get Feed’ endpoint is optimized for paginated access, whereas ‘Get Feed Item’ allows retrieval of detailed data for a single item, essential for deep dives into specific content.

Data Store Strategy: Choosing Non-Normalization

When structuring our client-side data store, we’re presented with two predominant approaches: normalized and non-normalized (regular). A normalized data store is akin to a relational database where each data type has its own ‘table,’ and references are made via IDs, reducing data duplication. However, for our NewsFeed scenario, a regular data store structure appears to be more fitting due to specific characteristics of the feed.

Advantages of a Regular Data Store for NewsFeed

The decision to opt for a regular data store stems from several considerations specific to the nature of a NewsFeed:

  • Minimal Data Duplication: Except for user and author information, our feed data contains minimal repetition, thereby reducing the need for normalization.
  • Content Consumption: Since the feed primarily serves for content consumption with few updates, the benefits of a normalized store for data consistency and update facilitation are less critical.
  • Simple Interactions: User interactions such as likes typically affect only a single post, negating the need for complex state management across different entities.

Example of a Regular Data Store Structure


  const feedStore = {
    pagination: {
      size: 10,
      next_cursor: "encoded-cursor-string"
    },
    results: [
      {
        id: "123",
        author: {
          id: "456",
          name: "John Doe"
        },
        content: "Hello world",
        image: "https://www.example.com/feed-images.jpg",
        reactions: {
          likes: 20,
          haha: 15
        },
        created_time: "2020-05-10T09:46:23+0000"
      }
      // ...additional posts
    ]
  };
  

The structure above represents a segment of our feed store, illustrating how data would be organized in a standard format. This setup streamlines data access and manipulation within the client application, particularly suited for the mostly static nature of our NewsFeed content.

Performance Optimizations

Facebook Newsfeed API design

Our NewsFeed aims for a seamless user experience, necessitating a deep dive into performance optimization techniques. Here’s how we’re approaching some critical aspects:

Server-Side vs. Client-Side Rendering (SSR vs CSR)

We employ a hybrid rendering approach to ensure a fast initial page load with Server-Side Rendering (SSR) for ‘above-the-fold’ content, which may include the first set of posts. Subsequent content, as part of the infinite scrolling feature, will be loaded using CSR for a smooth user experience.

Infinite Scrolling and Pagination

Our infinite scrolling implementation utilizes the Intersection Observer API for its performance benefits and precision over traditional window scroll events. For pagination, we have opted for a cursor-based strategy due to the dynamic nature of the News Feed, where offset-based pagination could result in stale or inaccurate data representation.

Virtualization and Recycling Nodes

Virtualization techniques recycle DOM nodes to handle long lists efficiently, ensuring smooth scrolling without sacrificing performance.

Code Splitting and Lazy Loading

We leverage code splitting and lazy loading to reduce the initial load time, progressively loading features and content as needed. This extends to image loading, where images are lazy-loaded and prioritized based on their placement ‘above the fold’.

Loaders, Shimmers, and Feedback States

Shimmer effects are used as content placeholders during load times, providing a graceful transition until the content is fully loaded. Success and error states are handled to provide immediate feedback to the user.

Rich Text Handling

Rich text elements like mentions, hashtags, and stylized text are dynamically rendered to enable interactive and engaging posts.

Image Optimization

Images are served through a microservice that provides the appropriate sizes, and the srcset attribute lets browsers choose the most suitable image. Compression techniques and modern formats like WebP are utilized for optimal delivery.

The following code snippet showcases how these optimizations could be reflected in our NewsFeed’s frontend architecture:


  // Pseudo-code for image lazy-loading with srcset
  <img
    src="placeholder.jpg"
    data-srcset="image-320w.jpg 320w,
                 image-480w.jpg 480w,
                 image-800w.jpg 800w"
    data-sizes="(max-width: 320px) 280px,
                (max-width: 480px) 440px,
                800px"
    class="lazyload" />
  

This pseudo-code outlines the approach for responsive image loading, where different-sized images are provided for different viewport widths. The actual images are loaded only when they’re near the viewport, optimizing resource loading and rendering times.

Commitment to Security and Accessibility

Facebook Newsfeed optimizations

Accessibility Considerations

Our NewsFeed embraces the core principles of web accessibility, making sure it is usable by as many people as possible:

  • Cross-Device Compatibility: Through rigorous testing, we ensure that the News Feed performs seamlessly across various devices and screen sizes.
  • REM Font Sizes: We use scalable units for font sizes to support users’ preferences in text size and enhance readability.
  • Proper Contrast: We maintain high contrast ratios in compliance with WCAG guidelines to aid users with visual impairments.
  • Keyboard Navigation: Our NewsFeed is fully navigable via keyboard, a fundamental accessibility feature.
  • Semantic HTML: Semantic HTML elements enable screen readers and assistive technologies to interpret page content effectively.
  • ARIA Roles: Attributes like role="feed" and role="article" Provide additional context to assistive technologies, delineating the structure of the NewsFeed content.

Security Measures

We implement robust security protocols to protect our users and their data:

  • XSS Protection: Our system sanitizes input data to prevent Cross-Site Scripting attacks, ensuring that only secure content is rendered to the user.
  • CORS Policy: Proper Cross-Origin Resource Sharing (CORS) settings are in place to restrict and control resources shared between different domains.
  • HTTPS: Secure Hypertext Transfer Protocol (HTTPS) is enforced for all data in transit, safeguarding against interceptions and ensuring data integrity.

These accessibility and security practices are ingrained in every step of our development process, from design to deployment, ensuring that our NewsFeed is enjoyable and engaging and respects all users’ privacy and needs.

Conclusion: How to Design Facebook News Feed

From the initial clarifying questions to a meticulous requirement analysis, we dove deep into high-level architectural choices, API intricacies, and strategic optimizations. We balanced the technological prowess with a strong commitment to accessibility and security, ensuring that the NewsFeed caters to a diverse user base while safeguarding user interactions.

As we conclude, we emphasize the importance of creating a scalable frontend.