DEV Community

Cover image for How Social Media Feed Algorithms Work
Michael Gokey
Michael Gokey

Posted on

How Social Media Feed Algorithms Work

A social media feed algorithm acts like a personal content curator, choosing which posts you see and in what order. Imagine it as a music playlist generator. It collects all possible songs (or posts), removes ones you've already heard, scores how much you might enjoy each, ranks them, adds some variety, and then gives you a playlist tailored just for you.

Core Components

User Profiling System

The algorithm builds detailed profiles of each user by tracking:

  • Interaction History: What you like, share, comment on, and how long you view content
  • Social Connections: Who you follow, message, and interact with most
  • Content Preferences: Whether you prefer videos over text, news over memes
  • Usage Patterns: When you're most active, what device you use
  • Demographic Signals: Age, location, language preferences

Content Scoring Engine

Every potential post gets evaluated using machine learning models that consider hundreds of signals, but the main factors include:

  • Relevance to your interests and past behavior
  • Relationship strength with the content creator
  • Content quality and engagement from other users
  • Recency and trending status
  • Content type match to your preferences

The Algorithm Process (Step-by-Step)

1. Collect Candidate Posts

The system gathers potential content from multiple sources:

  • Your Network: Posts from friends, family, and accounts you follow
  • Trending Content: Popular posts in your region or globally
  • Interest-Based: Content matching your topics and hobbies
  • Recommended: Posts from accounts similar to ones you follow
  • Promoted: Advertisements and sponsored content

2. Filter Out Unwanted Content

Before scoring, the algorithm removes:

  • Duplicate posts you've already seen
  • Content flagged as spam or inappropriate
  • Posts from blocked or muted accounts
  • Content that violates platform policies
  • Posts outside your language/location preferences

3. Score Each Post

This is where the magic happens. For every remaining post, the algorithm calculates a "relevance score" using factors like:

User Engagement Signals: Did you like, share, or comment on similar posts before?
Relationship Strength: How close are you to the author (DMs, likes, tags)?
Content Type Preference: Do you watch more videos than read text posts?
Recency: Is this new or old?
Virality/Quality: Is the post popular with others?

The scoring formula looks something like:

score(post) = w1 × engagement_probability
            + w2 × relationship_strength  
            + w3 × content_type_match
            + w4 × recency_decay
            + w5 × global_quality
Enter fullscreen mode Exit fullscreen mode

Where each "w" represents how much weight the platform gives to that factor.

4. Apply Machine Learning Prediction

The algorithm uses trained neural networks or other ML models to predict:

  • Probability you'll engage (like, comment, share)
  • Time you'll spend viewing the content
  • Likelihood you'll find it valuable based on similar users' behavior

5. Business Logic & Final Adjustments

The raw ML scores get modified by:

  • Promoting paid content (ads get score boosts)
  • Ensuring diversity (penalizing too much similar content)
  • Boosting close friends (recent posts from best friends get priority)
  • Balancing content types (mixing videos, photos, text posts)

6. Rank & Sort Posts

  • Sort all posts by their final scores, highest first
  • Apply diversity rules so you don't see 10 posts from the same person
  • Insert promoted content at strategic intervals
  • Mix content types for visual variety

7. Build Final Feed

  • Take the top N posts (usually 50-100 for initial load)
  • Add slight randomization to prevent identical feeds
  • Log what was shown for future learning
  • Display the personalized feed to you
SOCIAL MEDIA FEED ALGORITHM - PSEUDOCODE

FUNCTION generate_personalized_feed(user_id):

    // Step 1: Get user profile and preferences
    user_profile = get_user_profile(user_id)
    user_interests = extract_user_interests(user_profile)
    user_connections = get_user_connections(user_id)

    // Step 2: Gather candidate posts from multiple sources
    candidate_posts = empty_list()

    // Get posts from friends and followed accounts
    friend_posts = get_posts_from_connections(user_connections, time_limit=24_hours)
    candidate_posts.add(friend_posts)

    // Get trending/popular posts
    trending_posts = get_trending_posts(user_location, time_limit=6_hours)
    candidate_posts.add(trending_posts)

    // Get posts based on user interests
    interest_posts = get_posts_by_topics(user_interests, time_limit=12_hours)
    candidate_posts.add(interest_posts)

    // Step 3: Initial filtering
    filtered_posts = empty_list()
    FOR each post in candidate_posts:
        IF post is not spam AND 
           post is not inappropriate AND 
           user has not seen post AND
           post is not blocked content:
            filtered_posts.add(post)

    // Step 4: Feature engineering - calculate signals for each post
    scored_posts = empty_list()
    FOR each post in filtered_posts:
        post_features = calculate_post_features(post, user_profile)
        // Features include:
        // - recency_score = calculate_time_decay(post.timestamp)
        // - engagement_score = (likes + comments + shares) / followers
        // - relevance_score = match_user_interests(post.content, user_interests)
        // - relationship_score = connection_strength(post.author, user_id)
        // - content_type_score = user_preference_for_type(post.type, user_profile)

        scored_posts.add({post: post, features: post_features})

    // Step 5: Apply machine learning model to predict engagement
    FOR each scored_post in scored_posts:
        predicted_engagement = ml_model.predict(scored_post.features)
        scored_post.prediction_score = predicted_engagement

    // Step 6: Apply business logic and final adjustments
    FOR each scored_post in scored_posts:
        // Boost promoted content
        IF scored_post.post.is_promoted:
            scored_post.prediction_score *= 1.5

        // Ensure diversity - penalize similar content
        IF too_many_similar_posts_already_shown:
            scored_post.prediction_score *= 0.8

        // Boost recent posts from close friends
        IF scored_post.post.author in user.close_friends AND post_age < 2_hours:
            scored_post.prediction_score *= 1.3

    // Step 7: Sort and select final posts
    sorted_posts = sort_by_prediction_score(scored_posts, descending=True)
    final_feed = take_top_N_posts(sorted_posts, N=50)

    // Step 8: Add some randomization to prevent staleness
    final_feed = add_slight_randomization(final_feed, randomization_factor=0.1)

    // Step 9: Log interactions for future learning
    log_feed_generation(user_id, final_feed, timestamp=now())

    RETURN final_feed

// Helper functions would include:
// - get_user_profile(user_id)
// - calculate_time_decay(timestamp)
// - match_user_interests(content, interests)
// - connection_strength(author, user)
// - ml_model.predict(features)
// - etc.
Enter fullscreen mode Exit fullscreen mode

Technical Architecture

Most platforms use a multi-stage pipeline:

  1. Candidate Generation → Pulls thousands of potential posts
  2. Initial Filtering → Reduces to hundreds of viable options
  3. Feature Engineering → Calculates scoring signals
  4. ML Ranking → Applies trained models for predictions
  5. Business Rules → Applies platform-specific adjustments
  6. Final Assembly → Creates the ordered feed

The system is always learning from what you do. Every like, scroll, and the time you spend viewing content helps improve the recommendations you see in the future.


Platform Differences

While the core approach is similar, different platforms emphasize different signals:

  • Instagram: Heavily weighs visual engagement and story interactions
  • TikTok: Prioritizes completion rates and rewatches for videos
  • Twitter/X: Focuses on recency, trending topics, and real-time engagement
  • Facebook: Emphasizes social connections and meaningful interactions
  • LinkedIn: Prioritizes professional relevance and network connections

The fundamental goal remains the same: show you content that keeps you engaged, satisfied, and coming back to the platform.

Top comments (0)