Real-time & fan-out

Delivering live updates to millions of connected users instantly.

The idea

When a celebrity tweets, it needs to appear on millions of screens instantly. This is the Fan-out problem. Do you push it to everyone's inbox right now (Fan-out on Write), or do you just save it once and make users fetch it when they open the app (Fan-out on Read)?

Pushing to 10 followers is fast. Pushing to 10,000,000 followers takes a huge amount of computing power and causes delays. Large systems use a hybrid approach: Fan-out on Write for normal users, and Fan-out on Read for celebrities.

Author Global DB: 0 posts Follower 1 Follower 2 Follower 3 ... 10M+
Author is ready to post.

How it works (Hybrid approach)

def post_message(author, message):
    if author.followers > 100_000:
        # Celebrity (Fan-out on Read)
        # Just save it once. Do not push.
        global_db.save(message)
    else:
        # Normal User (Fan-out on Write)
        # Push into every follower's personal cache
        for follower in author.get_followers():
            follower.inbox.push(message)
            
def get_feed(user):
    # Merge personal inbox with celebrity posts at read time!
    inbox_posts = user.inbox.get_all()
    celeb_posts = global_db.get_posts_for(user.following_celebs)
    return merge_and_sort(inbox_posts, celeb_posts)