Feed en Vivo
Push del servidor · "N posts nuevos"
Escrito por Darjus Kazakevic · revisado el
Push a small hint to every open page; let the page fetch what changed.
A user has their feed open. When someone they follow posts, or someone likes or comments on a post on screen, the page should show it within about 2 seconds, without a reload. Nothing may jump while they read.
At peak, 10 million pages are open at once, and most are idle. An update is useful but not precious: missing one must never lose data, because the feed itself is still the source of truth.
En esencia — Keep one push connection per open page, subscribe it to what the page shows, and push small hints that the page turns into normal fetches.
Cuándo recurrir a él
Recurre a él cuando
- Readers keep a page open and expect it to change without a refresh
- Updates are small and frequent: counts, new items, typing, status
- A missed update is harmless, because a normal fetch still works
Reconsidéralo cuando
- Updates can wait a minute: poll every 60 s and keep it simple
- Every message must be stored and ordered: design a chat system
- One event goes to 100,000 viewers of one room: that is a broadcast
The components
- Push gateways (uses real-time delivery)
- Hold one Server-Sent Events connection per open page, about 100,000 per server. They hold connections and subscriptions, not data, so they scale out like web servers.
- Subscriptions
- A registry from each topic to the gateways that have a page listening. A page subscribes to its own feed topic and to the posts it shows, and unsubscribes as they scroll away.
- Dispatcher (uses pub/sub)
- Reads events from the bus (feed 42 changed, post 981 was liked), looks up the gateways that subscribe, and forwards a hint. It coalesces counts: a post's like count goes out at most once a second.
- Client merge
- The page keeps new posts in a buffer and shows "3 new posts" at the top. Counts update in place. Nothing above the reader's position moves until they tap the banner.
Key decisions
- Push a hint, not the post
- Send "feed 42 has 3 new items" or "post 981 has 120 likes", and let the page fetch through the normal API. Privacy checks, blocks and ranking then run in one place, not in the push path.
- Subscribe to what is on screen
- A page subscribes to its own feed topic and to the 10–20 posts it shows, and drops them as they scroll away. Subscribing to everything costs gateway memory for events nobody reads.
- Never move content under the reader
- Buffer new posts behind a "3 new posts" banner. Inserting them above the post being read pushes it down the screen, and the reader taps the wrong thing.
- Coalesce counts to one update a second
- A viral post gets thousands of likes a second. Send its count at most once a second per post. Nobody can read faster, and the gateways stay quiet.
Errores habituales
- Pushing whole posts. Every push must re-check who may see the post, and the push path quietly becomes a second, slightly different read API.
- Inserting new posts at the top while someone reads. The post under their thumb jumps, and they like or open the wrong one.
- Forgetting idle timeouts. A load balancer closes a quiet connection after 60 s, and pages stop updating with no error. Send a heartbeat every 25 s.
- Reconnecting without resuming. A page that drops for 10 s misses what happened. Send event IDs and replay from Last-Event-ID, or refetch the feed head.