🧮 Back-of-the-Envelope Capacity Estimations
📐 High-Level Architecture Topology
flowchart TD
C[Client] --> GW[API Gateway]
GW --> Auth[Auth Service]
Auth --> FeedSvc[Feed Service]
FeedSvc --> Redis[Redis Cluster: Sorted Sets]
FeedSvc --> PostSvc[Post Service]
PostSvc --> Kafka[Kafka: Event Bus]
Kafka --> FanOut[Fan-Out Worker Pool]
FanOut --> Redis
FanOut --> DB[(PostgreSQL: User Graph)]
FeedSvc --> Rank[Ranking Service: ML Model]
Rank --> FeedSvc
FeedSvc --> GW
🏛️ Architecture Deep Dive & Component Topology
Ingestion & Hybrid Fan-Out Strategy
The core challenge is the power-law distribution of followers. A pure push model (fan-out-on-write) causes write amplification for celebrities with 100M+ followers, saturating disk I/O and network. A pure pull model (fan-out-on-read) incurs high latency for users with many friends. We adopt a Hybrid Model: Users with 10K followers use Pull (fan-out-on-read). The threshold is dynamic based on cluster load.
Timeline Storage & Pagination
We use Redis Sorted Sets for timelines. Each user has a key timeline:{user_id}. Members are post_id, scores are timestamp. This allows O(log N) insertion and O(1) range queries for pagination. For Pull users, the Feed Service fetches the last N posts from each followed celebrity in parallel, merges them in memory, and sorts by timestamp. This is computationally expensive, so it is cached aggressively.
Ranking Pipeline
Raw chronological feeds are insufficient. A Ranking Service intercepts the merged feed, applies an ML model (e.g., XGBoost or DeepFM) to score posts based on user engagement history, recency, and content type. Features are pre-computed and stored in a feature store (Redis). The ranking step adds ~50ms latency, fitting within the 500ms SLA.
Cache Invalidation
Cache invalidation is handled via Event-Driven Invalidation. When a post is deleted or edited, a Kafka event triggers cache eviction for all affected timelines. For high-fanout users, we use TTL-based expiration (e.g., 60s) to avoid cascading failures. Stale-while-revalidate is used for celebrity timelines to handle cache stampedes.
💾 Data Models (DDL/Schemas) & API Contracts
SQL DDL (PostgreSQL)
CREATE TABLE users (
user_id BIGINT PRIMARY KEY,
username VARCHAR(50) UNIQUE,
follower_count INT DEFAULT 0,
following_count INT DEFAULT 0,
is_celebrity BOOLEAN DEFAULT FALSE
);
CREATE TABLE posts (
post_id BIGINT PRIMARY KEY,
author_id BIGINT REFERENCES users(user_id),
content TEXT,
created_at TIMESTAMP DEFAULT NOW(),
likes_count INT DEFAULT 0,
comments_count INT DEFAULT 0
);
CREATE TABLE follows (
follower_id BIGINT REFERENCES users(user_id),
following_id BIGINT REFERENCES users(user_id),
PRIMARY KEY (follower_id, following_id)
);
CREATE INDEX idx_posts_author ON posts(author_id, created_at DESC);
CREATE INDEX idx_follows_follower ON follows(follower_id);
Redis Schema
-- Timeline for user 123
ZADD timeline:123 1698765432 post_456
ZADD timeline:123 1698765431 post_789
-- Fetch last 20 posts
ZRANGE timeline:123 0 19 WITHSCORES
-- Invalidation
DEL timeline:123
gRPC API
service FeedService {
rpc GetFeed(GetFeedRequest) returns (GetFeedResponse);
}
message GetFeedRequest {
int64 user_id = 1;
int32 limit = 2; // Default 20
int64 cursor = 3; // Timestamp for pagination
}
message GetFeedResponse {
repeated Post posts = 1;
int64 next_cursor = 2;
bool has_more = 3;
}
🛡️ Extreme Failure Scenarios, Stampede Defense & Self-Healing
Split-Brain & Consistency
In a multi-region setup, split-brain can occur if a celebrity posts in Region A while a follower reads in Region B. We use Active-Active with CRDTs for follower counts and Eventual Consistency for feeds. Reads are served from the local region; writes are replicated asynchronously via Kafka MirrorMaker. Conflict resolution uses Last-Write-Wins (LWW) for metadata and Union for sets.
Cache Stampede
When a celebrity’s timeline cache expires, thousands of concurrent requests can hit the database. We mitigate this with Request Coalescing: only one request computes the new timeline; others wait for the result. We also use Probabilistic Early Expiration (Jitter) to spread out cache misses.
Disaster Recovery
If Redis fails, we fall back to Database-Backed Reads with aggressive query optimization (pre-computed views). For Kafka, we use Idempotent Producers to prevent duplicate fan-outs. Circuit breakers isolate the Ranking Service; if it fails, we serve chronological feeds. Multi-region failover uses Geo-Redundant DNS with health checks. RTO < 5min, RPO < 1min.
Hotspot Mitigation
Celebrity timelines are hotspots. We shard Redis by user_id % N and use Read Replicas for high-fanout users. For extreme cases (100M+ followers), we use Approximate Timelines: only the top 1000 most recent posts are cached; older posts are fetched on demand.
🎯 Interview Evaluation Rubric: L4/Senior vs L6/Staff Architect
🌐 AirSOTA Cross-Domain Ecosystem Synergy
Empowering North American tech talent & families with integrated decision-making across Visa, Career, Wealth, Housing & Education.
Maintain long-term career resilience: real-time DOL PERM processing queues, EB-2/3 movements & AC21 portability.
Convert high software engineering salary into permanent tax-free wealth via annual $70,000 Mega Backdoor rollovers.
High-throughput rate limiters, distributed consensus, and Staff-level trade-off rubrics with interactive diagrams.




