Back to blog

How do new likes appear without hitting refresh?

Thắng Đoàn
Thắng Đoàn

FotoFlow's notifications arrive whenever users pull to refresh, because the app and server speak plain HTTP: the app asks, the server answers, the conversation ends. A like lands on the server and there is no way to call the phone and say "you've got one." At 200,000 users, likes feel dead.

The server cannot call you first

HTTP is request and response: only the client can open a conversation. Phones sit behind networks that block incoming connections, so the client always has to reach out first. Every push technique is really a held-open client request, dressed differently.

The naive fix is polling: ask "anything new?" every 2 seconds. It works, and most answers are "no." Multiply it out and a million users polling every 2 seconds is half a million requests per second, 95% of them empty. Halving the delay doubles the load: polling trades freshness for traffic.

Hold the door open

Long polling answers nothing, immediately. The server holds the request open until a message arrives or about 30 seconds pass, then responds, and the app asks again. Messages now arrive almost instantly and empty answers are rare, and it works anywhere plain HTTP does.

Server-Sent Events go further: one request, and the server never finishes the response, writing each event down the stream as it happens. One-way, plain HTTP, browsers reconnect automatically. Alice's likes arrive the moment they happen; her own actions still go out as normal requests. This is also how AI assistants stream tokens to your screen.

WebSockets are the two-way version. The connection starts as an HTTP request, then upgrades into a permanent socket where either side talks any time, with tiny overhead per message. Use them when the client talks back constantly: chat, games, collaborative editing.

Which server has Alice?

Each open connection costs memory, so a million connected users need many servers. Bob's socket is on server 2, Alice's on server 1, and server 2 cannot deliver to her directly. The servers relay through pub/sub, the previous lesson's topic: server 2 publishes to Alice's channel, and whichever server holds her socket is subscribed and forwards.

What this does not solve

Alice's phone goes into the subway and the socket drops with it. Bob's messages still need somewhere to land. So every message is first saved to a store, then pushed if she is connected; on reconnect her app says "I last saw message 1041" and fetches the rest. If she stays offline, a mobile push notification wakes her phone. Real-time delivery is an optimisation. The stored message, fetched on reconnect, is the source of truth.

Presence, the green dot, is its own load: each app sends a heartbeat every 30 seconds, and no heartbeat for a minute means offline. A million users is about 33,000 heartbeats a second, so presence lives in a fast store with expiring keys, and changes only push to people actually looking at that contact. It is approximate by design, and that is fine.

Your app polls every 5 seconds and 9 answers in 10 are empty. What would holding the request open buy you, and what would it cost?
Chat history lives in the database, but delivery lives in sockets. Which one would you rather lose for a minute?

Notifications now land on their own. Next in the series: The server dies at 3am: who notices, and who takes over?

Sources

MDN: WebSockets API

MDN: Server-Sent Events

MDN: HTTP overview

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.