Skip to content
Manuj Dixit
Go back

How to build Algorithm for Social Media Feed — The Basics & System design (Part 1)

How to build Algorithm for Social Media Feed — The Basics & System design (Part 1)

Around a week ago I was assigned the task “we need a feed like Twitter or linkedin”, I had zero idea how this would be done.

Feeds look simple from the outside… until you try to make one that’s fast, personalized, doesn’t cost a fortune, and doesn’t die when someone goes viral.

This is Part 1 of how we actually built it .

Feeds are secretly one of the hardest parts of any social app

Everyone wants the same dream:

Turns out… you can’t get all of that with one naive solution. There are painful trade-offs everywhere.

The two classic ways people build feeds (and why both hurt alone)

1. Push everything when someone posts (“fan-out on write”)

When Maria posts a cute dog video → we immediately copy that post to the personal “timeline” of every single follower she has.

Pros
Reading the feed later is lightning fast — just read your own list!

Cons
If Maria suddenly gets 500,000 followers? You have to write the same post 500,000 times.
Servers scream. Database bills explode. 😭

2. Pull everything when the user opens the app (“pull on read”)

When you refresh the feed → the system goes:
“Okay… find the latest posts from the 312 people Anna follows + some popular stuff right now.”

Pros
Super safe for celebrities. One viral post = one write. No problem.

Cons
If you follow 500+ active people, every refresh becomes a heavy search. Latency kills the experience.

What we actually do: a hybrid push + pull system

Normal people → push
Celebrities → pull

Simple rule that changed everything.

high level architecture diagram

Why treat some users as “celebrity” ?

Here’s the thing that made my brain click:

We treat users completely differently depending on follower count.

<  ~1000 followers   → we push (copy) the post to all followers right now
≥ 1000 followers → we DO NOT copy. We just save it in the author’s own “outbox”

We call anyone ≥1000 followers a “celebrity” (yes, even if it’s just your friend who posts gym mirror selfies every day 😂)

Why 1000?
It’s the magic number we found after testing.
Small enough → pushing is still fast.
Big enough → we don’t murder the database the moment someone goes viral.

Where everything actually lives (mostly Redis)

We lean very heavily on Redis because it’s stupidly fast for this kind of workload.

Here are the most important keys we use every day:

What really happens when someone posts?

Case 1: Normal person posts (<1000 followers)

Maria posts cute cat → we get her followers (usually <300)
Then we do: ZADD timeline:john ... post-id
ZADD timeline:sarah ... post-id
ZADD timeline:mike ... post-id
...

Like sending the same postcard to 300 friends. Fast. Cheap. Done.

Case 2: Celebrity posts (≥1000 followers)

MrBeast posts → we ONLY do:
ZADD celebrity_posts:mrbeast timestamp post-id

One single write. Extremely cheap.
We save the heavy work for later — when people actually want to read his stuff.

Quick recap — what we covered in Part 1

Some References

In Part 2 we’ll get to the really fun part: How we actually decide what shows up first, how we mix friends + viral posts, how we avoid boring repetitive feeds, and how guest users get a feed without even logging in.

If you enjoyed this and excited for Part 2, feel free to follow me here on Medium — or come say hi on X/Twitter @manujdxt 😄 I usually post small dev tips, architecture breakdowns, and the occasional confused dev moment.

Thanks for reading — see you in Part 2 next week! 🚀


Share this post:

Previous Post
Scoring 100 on Lighthouse (Performance focused)
Next Post
How to Upgrade Prisma ORM version (in Expressjs)?