Overview
Tweetypie is the core Tweet service that handles the reading and writing of Tweet data. It is called by:- Twitter clients (through GraphQL)
- Internal Twitter services
Architecture
Tweetypie acts as the central orchestration layer for Tweet operations:Read Path
The read path fetches Tweet data from storage/cache and hydrates it with data from various backend services.Tweet Retrieval
TweetResultRepository fetches Tweet data:
- First checks Twemcache (distributed cache)
- Falls back to Manhattan (distributed database) if cache miss
Hydration Pipeline
Raw Tweet data passes through the hydration pipeline to enrich it with:
- Expanded URLs
- Media metadata
- User mentions
- Cards and other entities
Relevant Packages
Backends
Wrappers around Thrift services that Tweetypie calls:Backends are located at:
tweetypie/server/src/main/scala/com/twitter/tweetypie/backends/Repositories
Provide structured interfaces for retrieving data from backends:Repositories are located at:
tweetypie/server/src/main/scala/com/twitter/tweetypie/repository/Hydrators
Enrich raw Tweet data with additional information:Hydrators are located at:
tweetypie/server/src/main/scala/com/twitter/tweetypie/hydrator/Handlers
Functions that handle requests to Tweetypie endpoints:Handlers are located at:
tweetypie/server/src/main/scala/com/twitter/tweetypie/handler/Through the Read Path
Detailed flow of aget_tweets request:
- GetTweetsHandler receives the request
- Uses TweetResultRepository (defined in LogicalRepositories.scala:301)
- TweetResultRepository uses:
- ManhattanTweetRepository - fetches from Manhattan storage
- Wrapped in CachingTweetRepository - adds Twemcache caching layer
- Wrapped in hydration layer - applies all hydrators
- Raw Tweet data flows through TweetHydration pipeline
- Fully hydrated Tweet returned to caller
The hydration pipeline is described in:
tweetypie/server/src/main/scala/com/twitter/tweetypie/hydrator/TweetHydration.scala:789Write Path
The write path creates or modifies Tweets and updates various backend stores.Tweet Building
TweetBuilder creates a Tweet from the request:
- Text processing and validation
- URL shortening via Talon
- Media processing
- Duplicate detection
Write Path Hydration
WritePathHydration.hydrateInsertTweet hydrates the Tweet before storage to ensure all required fields are populated.
Relevant Packages
Stores
Define logic for updating backends on write:Stores are located at:
tweetypie/server/src/main/scala/com/twitter/tweetypie/store/Store Modules
Define logic for handling write endpoints and coordinate which stores to call:Store modules are located at:
tweetypie/server/src/main/scala/com/twitter/tweetypie/store/InsertTweet.scala:84Through the Write Path
Detailed flow of apost_tweet request:
- PostTweet.scala handles the request (line 338)
- TweetBuilder creates Tweet:
- Validates text and media
- Shortens URLs via Talon
- Processes media uploads
- Checks for duplicates
- WritePathHydration.hydrateInsertTweet enriches Tweet (WritePathHydration.scala:54)
- InsertTweet module writes to stores (InsertTweet.scala:84):
- Manhattan (primary storage)
- Cache (Twemcache)
- Timeline Service
- Search Index
- TFlock (for fanout)
- EventBus (for streaming)
Key Operations
Creating Tweets
Reading Tweets
Deleting Tweets
Editing Tweets
Data Storage
Manhattan
Twitter’s distributed key-value store:- Primary storage for Tweet data
- Highly available and scalable
- Optimized for low-latency reads
Twemcache
Twitter’s distributed caching layer:- Caching frequently accessed Tweets
- Reduces load on Manhattan
- Maintains consistency with write-through pattern
Hydration Details
Tweetypie hydrates various Tweet components:URLs
Expand t.co shortened URLs via Talon
Media
Fetch media metadata and thumbnails
Mentions
Hydrate user mentions with profile data
Cards
Fetch Twitter Card metadata
Places
Hydrate location/place information
Quotes
Fetch quoted Tweet data
Hydration Pipeline
Safety and Visibility
Tweetypie enforces various safety and visibility rules:Visibility Filtering
- Blocked/muted users
- Protected accounts
- NSFW content filtering
- Age-gated content
- Country-specific takedowns
Safety Levels
Different safety levels for different contexts:TimelineHome- Strictest filtering for Home timelineRecommendations- Balanced for recommendation surfacesSearch- Search-appropriate filteringMinimal- Minimal filtering for moderation tools
Performance Optimization
Caching Strategy
Batch Operations
Async Hydration
Some hydrators run asynchronously to reduce latency:- Non-critical data fetched in background
- Progressive hydration for incremental responses
- Parallel hydration where possible
Monitoring
Key Metrics
- Request Rate: Requests per second by endpoint
- Latency: p50, p99, p999 for reads and writes
- Success Rate: Non-error responses
- Cache Hit Rate: Twemcache effectiveness
- Hydration Success: Per-hydrator success rates
Alerts
- High latency (p99 > 200ms)
- Low success rate (< 99.9%)
- Cache performance degradation
- Backend service failures
- Data consistency issues
Related Services
- Home Mixer - Primary consumer for timeline tweets
- Timeline Ranker - Uses Tweetypie for tweet hydration
- Pushservice - Fetches tweet data for notifications
- CR Mixer - Uses tweet data for candidate generation