Notes
All notes

System design walkthroughs 2: social and feeds

Problem. Full interview answers for the bank’s social questions — requirements, estimation, API, core design, deep dives. Part 2 of 6. (1: foundations · 3: geo & marketplaces · 4: media & storage · 5: money · 6: AI era)

10. Twitter/X feed [Twitter, Meta, Google, Microsoft]

Requirements. Functional: post, follow, home timeline. Non-functional: timeline p99 under 200 ms, tolerate celebrity skew, never lose a post.

Estimation. 500M posts/day ≈ 6k writes/s; 100k timeline reads/s. Average 200 followers → naive fan-out is 1.2M cache writes/s — that number forces the hybrid below; compute it out loud.

API. POST /tweets, POST /follow, GET /timeline?cursor=.

Core design. Fan-out-on-write for normal users (push into followers’ home caches), fan-out-on-read for celebrities (pull at request time).

flowchart LR
    U[Post tweet] --> API[API]
    API --> DB[(Tweet store)]
    API --> Q[Queue]
    Q --> F1[Fan-out worker]
    F1 --> C[Home caches]

Deep dives. The follower-count threshold for “celebrity” (measure, don’t guess); ranking as a separate layer after fan-out; media on a different path; cursor pagination, never offsets.

11. Instagram [Meta, Snap, ByteDance, Pinterest, Microsoft, X]

Requirements. Feed plus media: upload photos/video, filters optional, Stories with 24h TTL, Explore ranking.

Estimation. 100M uploads/day × 3 MB ≈ 300 TB/day ingress; reads 100:1 over writes — the CDN bill dominates, say so.

API. POST /media (resumable upload), GET /feed, POST /stories with expiry, GET /explore.

Core design. Object storage + CDN for bytes, async transcode farm (thumbnails, compression ladders), feed core as Q10, follow-graph as its own store, Stories as TTL rows with read-fan-out.

12. WhatsApp / Messenger chat [Meta, Signal, Snap, Microsoft, Uber, TikTok, Amazon, Google, LinkedIn]

Requirements. Functional: 1:1 and group messages, delivery receipts, offline delivery. Non-functional: sub-second delivery, connection scale in the billions (cite WhatsApp’s Erlang gateways).

Estimation. 60B messages/day ≈ 700k/s average, 2M/s peak; 200 bytes each ≈ 140 GB/day before media (media bypasses this path entirely).

API. Persistent connection (CONNECT), SEND {to, id, payload}, ACK, RECV; plus POST /messages fallback for offline senders.

Core design. Stateful gateway servers hold connections; a connection registry maps user → gateway; messages persist, route to the recipient’s gateway, queue + push-notify when offline. Dedup ids give exactly-once display; per-conversation ordering by sequence number.

13. Notification service [Meta, Slack, Discord, Salesforce]

Requirements. Functional: one intake for push/email/SMS, templates, preferences. Non-functional: OTPs in seconds, marketing digestible in hours — priority lanes, not one queue.

Estimation. 500M sends/day ≈ 6k/s average with 10× event spikes; idempotency keys make retries safe at any volume.

API. POST /notify {user, channel, template, data, idempotency_key}, PUT /users/{id}/preferences.

Core design. Intake → preference + rate-limit checks → per-channel workers → provider with failover; dead-letter for provider failures; digest batching turns forty emails into one (the cost lever — say it).

14. TikTok / short video [TikTok, ByteDance]

Requirements. Functional: upload, watch feed, likes/comments. Non-functional: start playback under 2s on 4G, feed never stalls.

Estimation. 1B users, watch time dominates everything: bytes served per day dwarf uploads 1000:1 — design is a CDN + prefetch problem with a recommendation engine attached.

API. POST /videos (resumable), GET /feed?cursor=, POST /videos/{id}/like.

Core design. Upload → transcode farm → object storage + CDN; feed as candidate generation then ranking (always mention both, plus popularity-tiered caching and ML personalization — their engineering lore); aggressive prefetch of the next videos; live as a separate low-latency path.