Twitter / X
Microblogging · X
Escrito por Darjus Kazakevic · revisado el
Save each tweet once, then let five systems consume the same event.
Users post tweets of up to 280 characters with photos or video, follow each other and read a home timeline. They can search every tweet, see what is trending in their region and get notified when mentioned. Links are shortened to t.co.
About 250 million people use it daily, and reads outnumber posts more than 100 to 1. The timeline must load in under 200 ms, a new tweet should be searchable within about 10 seconds, and an account with 100 million followers must not take it down.
En esencia — Store each tweet once under a time-sortable ID, publish one event, and let timelines, search, trends, notifications and links each build their own view.
Cuándo recurrir a él
Recurre a él cuando
- Public posts that many people read through follows, search and trends
- Several features consume the same write in different ways
- Reads vastly outnumber writes, and a few accounts have huge audiences
Reconsidéralo cuando
- Private conversations between a few people: design a chat system
- Long-form content that is edited after posting: design a blog
- A small community: one database with a follows table is enough
The components
- Tweet service (uses unique ID generation)
- Validates and saves each tweet under a 64-bit Snowflake ID, which sorts by time with no central counter. Tweets are sharded by ID, with a per-user index for profiles.
- Home timelines (uses the News Feed design)
- A list of tweet IDs per user, filled by fan-out on write, with celebrity tweets merged at read time. The News Feed walkthrough designs this part.
- Real-time search (uses a search index)
- An inverted index of recent tweets, split by time, that a new tweet joins within about 10 seconds. Queries read the newest partitions first, because most searches want what is happening now.
- Trends
- Counts hashtags and phrases per region in 5-minute windows and compares each count with that term's usual level. A term trends when it rises far above its own baseline.
- Media pipeline (uses object storage)
- The app uploads photos and video straight to object storage with a signed URL. Workers transcode them into several sizes, a CDN serves them, and the tweet stores only media IDs.
Key decisions
- One event, many independent consumers
- Posting saves the tweet and emits one event. Timelines, search, trends, notifications and links each consume it, so a slow search index never delays a timeline.
- Trends are spikes, not totals
- Rank a term by how far it rises above its own usual level for this hour. Raw counts put the same everyday words on top every day.
- Count engagement approximately
- Likes and retweets go to sharded counters that are summed every few seconds. A count a few seconds stale is fine; a lock per like on a viral tweet is not.
- Keep media off the API servers
- Upload bytes straight to object storage and serve them from a CDN. API servers then handle only small JSON, and a video upload never ties up a request.
Errores habituales
- Deleting a tweet in one place. It lingers in timelines, the search index and caches. Publish a delete event and make every consumer act on it.
- Writing likes to the tweet row. A viral tweet takes thousands of locks a second on one row. Use sharded counters and sum them.
- Trending by raw counts. The same everyday words top the list every day. Compare each term with its own baseline.
- Indexing for search while posting. A slow index makes posting slow. Index from the event, within seconds.