Day 23 Exercises
Social Feed Design
Apply fan-out strategies, timeline assembly, and Snowflake ID design to Twitter-scale social systems.
Fan-Out Strategy Selection
You're designing a Twitter-like system. User A has 500 followers. User B (a celebrity) has 50 million followers. You must decide: fan-out on write (push) or fan-out on read (pull) for each.
Tasks
- Apply fan-out on write to User A's tweet. How many Redis writes occur?
- Apply fan-out on write to User B's tweet. What's the problem?
- Design a threshold-based hybrid: when do you switch from push to pull?
- What is "list materialization" and why does it cap at 800 tweets?
Timeline Assembly for Hybrid System
Alice follows 3 regular users (push timelines in Redis) and 2 celebrities (pull at read time). Design the timeline read path that merges these 5 sources into a single sorted feed.
Tasks
- Describe the read path: what queries run when Alice opens Twitter?
- How do you merge 5 sorted lists into one sorted timeline efficiently?
- What's the latency impact of adding celebrity merge vs. pure push?
- How do you handle a celebrity who tweets 100 times/hour in the merged timeline?
Snowflake ID Design
Twitter generates 500M tweets/day across 10 data centers, each with 5 tweet servers. You need globally unique, time-ordered 64-bit IDs with no coordination overhead.
Tasks
- Design the 64-bit Snowflake ID bit layout (timestamp, datacenter, machine, sequence).
- Why must the timestamp be in the high bits?
- What happens if the system clock moves backward (NTP correction)?
- Calculate the max IDs per second per machine with your layout.
Trending Topics System
Twitter shows "Trending Now" — the top 10 hashtags by velocity (rate of increase in mentions in the last hour). Design a system that updates every 60 seconds and handles 100K tweets/minute.
Tasks
- Why can't you use a single sorted set (Redis ZADD) for real-time trending?
- Design a sliding window counter approach using Redis.
- How do you aggregate counts across multiple tweet ingestion servers?
- What's the difference between "trending" (velocity) and "popular" (total count), and why does Twitter use velocity?