💬
✉️
📸
▶️

Loading…

Design Instagram: media upload, CDN, and feed ranking

InstagramGuided Mode

Redis Cache: Instagram's O(1) Feed Reads

The Feed Service writes pre-assembled timelines to Cache. Instagram reportedly stored 300M photos per day at peak — Redis timelines meant feed reads were entirely from memory, enabling 60M DAU without proportional DB scaling.

Request Flow

1

Feed Service writes to Redis

On fanout, the Feed Service pushes each new post's CDN URL to the front of every follower's timeline list in Cache. Each follower's timeline is a Redis sorted set or list capped at ~1000 entries.

LPUSH timeline:user456 '{"url":"cdn.example.com/photo.jpg","ts":1700000000}'
LTRIM timeline:user456 0 999  // keep latest 1000
2

Viewer fetches from Redis

GET /feed reads the first 20 entries from the viewer's timeline list in Cache. P99 latency < 2ms. No database involved.

GET /feed?page=0
→ LRANGE timeline:user456 0 19
3

Cache miss — fall back to DB

If the user's timeline isn't in Cache (cold start, eviction), the Feed Service rebuilds it from the Database and repopulates Redis.

if (!cache.exists("timeline:user456")) {
  const posts = db.getFollowedPosts(user, limit=1000);
  cache.lpush("timeline:user456", ...posts);
}

Why We Need This

Without Redis, every feed page requires a multi-table join: follows → posts → photos → users. With 500M users this is untenable. Redis reduces the fan-in problem to a single key lookup.

Instagram reportedly stored 300M photos per day at peak. Redis timelines meant feed reads were served entirely from memory — enabling 60M daily active users at the time without proportional DB scaling.

Key Insight

This is the complete Instagram read path: Viewer → Redis (timeline list) → CDN (image bytes). Two hops, both from cache. The database and S3 origin are only touched for writes and cold starts.

Step 7 of 7