Video in progress

Strong Junior 🏗️ System design

Where does a single user with millions of followers cause problems in a social network?

They become a "hot key": their profile and posts overload one shard or one cache node, and each new post has to be written into millions of feeds. Accounts like this need a special path — merging their posts at read time, replicating the key, and a local cache.

📖 Answer

A scheme that works for an ordinary user breaks for a famous one.

Building the feed. In many systems, when a post is written it is added to the ready-made feed of every follower (fan-out on write). For 200 followers that's 200 writes — trivial. For 20 million followers it's 20 million writes — each post keeps the queue busy for hours. That's why a hybrid approach is used: posts from ordinary users are pushed out ahead of time, while the famous ones' posts are merged in when the feed is read.

Shard and cache. Their data sits on one shard and one Redis node by user_id. When everyone opens their page, that node overheats while the others sit idle. Solutions:

  • also hold these keys for a short time in a local in-process cache;
  • multiply the key into several copies (profile:42#1, profile:42#2...) and spread reads among them.

Counters. If a post gets 50,000 likes a second, doing UPDATE on a single row drowns in locks. Likes are accumulated in memory or in several sharded counters and written to the database periodically.

In an interview this question often comes right after the feed design: "now what happens if Ronaldo posts?"