How Instagram’s Distributed System Handles a Simple Follow Action

Listen to this Post

Featured Image
When a user clicks “Follow” on Instagram, a complex distributed system springs into action. Here’s a breakdown of what happens behind the scenes:

1. Load Balancer & API Gateway

  • The request first hits Instagram’s load balancers (e.g., AWS ALB, Nginx).
  • The API Gateway (Kong, AWS API Gateway) authenticates the request and routes it to the Follow Service.

2. Follow Service & Message Queue

  • The Follow Service validates the request (checks if the user is blocked, rate limits, etc.).
  • It generates a Following Event and pushes it to a Message Queue (Kafka, RabbitMQ) for async processing.

3. Social Graph Database Update

  • The Follow Service updates the Social Graph Database (Neo4j, Amazon Neptune).
  • It creates an edge between the two users and updates follower/following counts.

4. Cache Invalidation

  • The Cache Layer (Redis, Memcached) is invalidated to reflect the new relationship.

5. Push Notification & Feed Update

  • The Notification Service sends an alert: “X started following you!”
  • The Feed Service adjusts the follower’s feed to include posts from the followed account.

6. Recommendation Engine

  • The Recommendation Service analyzes the new connection and suggests similar accounts.

You Should Know: Key Commands & Code Snippets

  1. Load Balancer & API Gateway (Nginx, AWS ALB)
    Nginx load balancing example 
    upstream backend {
    server 10.0.0.1;
    server 10.0.0.2;
    }</li>
    </ol>
    
    server {
    listen 80;
    location / {
    proxy_pass http://backend;
    }
    }
    

    2. Message Queue (Kafka, RabbitMQ)

     Kafka: Produce a follow event 
    kafka-console-producer --broker-list localhost:9092 --topic follow_events 
    {"follower_id": "123", "followed_id": "456", "timestamp": "2024-05-15T12:00:00Z"}
    

    3. Social Graph Database (Neo4j)

    // Create a follow relationship 
    MATCH (a:User {id: "123"}), (b:User {id: "456"}) 
    CREATE (a)-[:FOLLOWS]->(b)
    

    4. Cache Invalidation (Redis)

     Invalidate cached follower count 
    redis-cli DEL "user:456:followers"
    

    5. Push Notification (Firebase, APNs)

    // Firebase Cloud Messaging (FCM) 
    admin.messaging().send({
    token: "device_token",
    notification: {
    title: "New Follower",
    body: "Kamran started following you!"
    }
    });
    

    6. Feed Update (Elasticsearch, Cassandra)

     Update feed ranking with new follow 
    curl -XPOST 'http://localhost:9200/user_feed/_update/123' -d '{
    "script": "ctx._source.ranking += 10"
    }'
    

    What Undercode Say

    Instagram’s distributed system efficiently handles millions of follow actions daily. Key takeaways:
    – Idempotency is crucial (retries shouldn’t duplicate actions).
    – Event-driven architecture decouples services (Kafka, RabbitMQ).
    – Caching strategies (Redis) reduce database load.
    – Graph databases (Neo4j) efficiently manage relationships.

    For engineers, mastering these concepts ensures scalable, fault-tolerant systems.

    Prediction

    Future social networks may use AI-driven feed ranking (LLMs predicting engagement) and decentralized graph databases (Blockchain-based follows).

    Expected Output:

    • A scalable follow system using Kafka + Neo4j + Redis.
    • Idempotent APIs to prevent duplicate follows.
    • Real-time notifications via WebSockets or FCM.

    Would you like a deeper dive into any component? 🚀

    References:

    Reported By: Systemdesignengineer Kamrans – Hackers Feeds
    Extra Hub: Undercode MoN
    Basic Verification: Pass ✅

    Join Our Cyber World:

    💬 Whatsapp | 💬 Telegram