Overview
Thunder is a high-performance in-memory post store that provides sub-millisecond access to recent posts from accounts a user follows (in-network content). It consumes post create/delete events from Kafka in real-time, maintains per-user post timelines, and serves as the primary source for in-network candidates in the For You feed.Why In-Memory?Thunder keeps recent posts in RAM to achieve sub-millisecond lookup latency, critical for the For You feed’s performance budget.
Architecture
PostStore: Core Data Structure
ThePostStore is Thunder’s central component, managing posts in memory with efficient lookup and automatic trimming.
Data Model
thunder/posts/post_store.rs
TinyPost: Memory-Efficient Reference
To minimize memory overhead, user timelines store minimal references:LightPost) is stored separately in the posts map, allowing multiple timelines to reference the same post without duplication.
Memory Optimization: Storing only post ID + timestamp in timelines reduces memory usage by ~90% compared to storing full post objects.
Real-Time Ingestion
Thunder consumes post events from Kafka and updates the in-memory store:Post Creation
thunder/posts/post_store.rs
Post Deletion
Serving In-Network Posts
Thunder exposes a gRPC service that Home Mixer calls to retrieve posts:thunder/thunder_service.rs
Post Retrieval Flow
1
Validate Request
Check semaphore permits to prevent overload
2
Fetch Following List
Get list of accounts the user follows (if not provided)
3
Lookup Posts
For each followed user, scan their timeline and collect posts
4
Filter and Deduplicate
Remove excluded posts, deleted posts, and apply reply filtering logic
5
Sort by Recency
Order posts by creation timestamp (newest first)
6
Truncate
Return top K posts (typically 1000)
Efficient Post Lookup
thunder/posts/post_store.rs
Performance: Lookups are O(1) for user timeline access + O(N) for scanning recent posts, where N is bounded by
MAX_TINY_POSTS_PER_USER_SCAN.Automatic Post Trimming
Thunder automatically removes old posts to maintain memory bounds:Background Tasks
Thunder runs periodic background tasks:Configuration
Key parameters controlling Thunder’s behavior:Integration with Home Mixer
home-mixer/sources/thunder_source.rs
Metrics and Monitoring
Thunder emits comprehensive metrics:Performance Characteristics
Typical Latencies
- Post lookup: < 1ms for typical following list (500 users)
- Post insertion: < 0.1ms per post
- Memory usage: ~2-5GB for 2-day retention window
- Throughput: 10,000+ RPS per instance
Scaling
Thunder can be horizontally scaled by sharding users across multiple instances:Related Components
Home Mixer
Orchestration layer that uses Thunder as the in-network candidate source
Phoenix
Complements Thunder with out-of-network content discovery
Candidate Pipeline
Framework that integrates Thunder into the overall recommendation flow