Eventual Consistency and Out-of-Order Tweets
Fan-out is asynchronous, so a timeline is eventually correct, not instantly correct. Two followers can see the same celebrity's tweet seconds apart, and tweets can land out of order. Understanding why requires understanding Snowflake IDs and delivery latency.
The intuition first
Home delivery isn't instantaneous. When a popular newsletter publishes, the delivery trucks take time to reach every mailbox — the subscriber on the first street gets it in seconds; the one on the far edge of town gets it a minute later. Same edition, different arrival times. And if two newsletters publish near-simultaneously, the order they land in your mailbox depends on which truck reached you first, not strictly on which was published first. That arrival-time jitter is eventual consistency in the timeline.
Why the timeline is eventually consistent
Recall the fan-out on write part: fan-out happens in the background, after the tweet is durably stored, so the poster isn't blocked. The consequence is that delivery to followers' timelines is not atomic and not instantaneous:
t=0ms celebrity-adjacent author posts tweet T; T durably stored; OK returned to poster
t=? fan-out service begins spraying ZADDs to follower timelines
t=20ms follower #1's timeline has T ◄── sees it early
t=300ms follower #50,000's timeline has T ◄── sees it later
t=2s follower #900,000's timeline has T ◄── the tail of a large fan-out
So at any instant, some followers have T and some don't yet. The system is converging to the correct state ("everyone who follows the author has T") but isn't there instantly. That is the definition of eventual consistency: given no new writes, all replicas/timelines will eventually reflect the write — but there's a window where they disagree.
Sources of the delay:
- Fan-out queue depth — fan-out is processed by workers pulling from a queue; under load there's lag.
- Large fan-outs take longer — a 1M-follower fan-out simply has more inserts to complete; the last followers wait longest.
- Replication of timeline shards — each timeline is replicated for availability; replicas converge asynchronously.
- Read-time pull half (see hybrid model) — celebrity tweets aren't even delivered; they appear only when you read and the merge pulls them, so two viewers refreshing at different moments naturally see them at different times.
Snowflake IDs: how order is even defined
To talk about "out of order," you need a global notion of tweet order. Twitter assigns each tweet a Snowflake ID: a 64-bit, roughly time-sortable integer generated without a central coordinator.
64-bit Snowflake ID layout (conceptually):
┌─┬──────────────────────────────┬───────────────┬───────────────┐
│0│ timestamp (ms since epoch) │ machine id │ sequence │
└─┴──────────────────────────────┴───────────────┴───────────────┘
1 ~41 bits ~10 bits ~12 bits
unused time-ordered which generator per-ms counter
Why this design:
- Time-ordered: the high bits are a millisecond timestamp, so larger ID ≈ later tweet. Sorting by ID ≈ sorting by time. This is what makes the timeline
ZSET"score = ID" work and what makes merges (see hybrid model) a clean k-way merge. - Distributed / no coordinator: the machine-id bits let many generators mint IDs in parallel without colliding and without a central sequence server — critical at thousands of tweets/sec. The per-ms sequence bits disambiguate multiple tweets in the same millisecond on the same machine.
- Compact: 64 bits = 8 bytes, which is exactly why timeline entries (see Timeline Service) are so cheap.
Why tweets can appear out of order
Here's the subtle part. Snowflake IDs define a total order by creation time, but your timeline is assembled from asynchronously delivered and read-time merged pieces. So:
- Delivery-order ≠ creation-order. Tweet A (created at t=0) might be fanned out by a backlogged worker and land in your timeline after tweet B (created at t=1) that went through a fast path. If your client renders by arrival, you'd see B then A.
- Push/pull merge timing. A celebrity tweet (pull half) created earlier may surface on a later refresh than a normal tweet (push half) created later, because the two halves are gathered at different times.
- Clock skew across generators. Snowflake timestamps come from many machines' clocks. Small skew means two tweets' relative ID order may not perfectly match true wall-clock order — usually negligible, but real.
- Late-arriving large fan-outs. The tail of a big fan-out can deposit an "old" tweet into your timeline well after newer tweets already arrived.
The mitigation is to sort the assembled candidate set by Snowflake ID at read time before display (the hybrid model part step 3), rather than trusting arrival order. That gives a consistent ordering by creation time for whatever tweets are present — but it can't conjure tweets that haven't been delivered yet. So you get correct order over an eventually-complete set.
What this means for the user (and why it's acceptable)
| Symptom | Cause | Why it's tolerated |
|---|---|---|
| Friend says "did you see X's tweet?" before it's in your feed | You're in the tail of a large fan-out, or haven't refreshed the pull half | Timelines aren't a transactional system of record; a few seconds' skew is invisible to most users |
| A tweet "pops in" above ones you already saw | Late delivery + read-time re-sort by ID | Correct order is restored on refresh; only timing differed |
| Two devices show slightly different feeds momentarily | Replicas/timelines converging | Converges within seconds |
The staff-level point: Twitter deliberately chose availability and latency over strong consistency for the timeline. A timeline is not a bank ledger. "Your tweet shows up for everyone within a few seconds, in the right order once present" is a perfectly good guarantee, and it's vastly cheaper than the synchronous, globally-consistent alternative. Eventual consistency here is a feature of the cost structure, not a bug.