Skip to the page

gRPC, real-time APIs and webhooks · mid

Choosing and scaling real-time: polling, SSE and WebSocket

Pick the simplest tool that fits: cached polling for one shared slow-changing value, SSE for one-way updates, WebSocket for two-way or very frequent updates. Scaling out then needs sticky sessions or WebSocket-only transport, plus pub/sub between servers.

The notes

What you must know

  • Data that is the same for everyone and changes slowly (a cricket score) fits polling a small JSON file cached at a CDN. Clients poll every few seconds with jitter, the CDN caches for a couple of seconds, and the origin sees a few requests a second.
  • WebSocket: two-way, one long-lived connection. Worth it for frequent, personal or two-way data such as chat. Millions of connections need a fleet of connection servers, pub/sub fan-out and handling of reconnect storms.
  • SSE: one-way, plain HTTP, the browser reconnects by itself. Simpler than WebSocket, but still one open connection per viewer.
  • Socket.IO starts with HTTP long-polling and then upgrades. The polling requests must reach the same server, so you need sticky sessions (a cookie on a load balancer, ip_hash in Nginx) or transports: ["websocket"] only. Without it you see 400 Session ID unknown.
  • Sticky sessions have costs: uneven load, hot servers, and lost sessions when a server goes away.
  • Fan-out across servers: if every server reads events from Kafka in one consumer group, each event reaches only one server. Use a pub/sub layer (Redis pub/sub, a Socket.IO adapter) so every server gets every event, or one consumer group per server, and each server emits to its own connected clients.
  • Plan for reconnect storms: when a server restarts, thousands of clients return at once. Use jittered backoff on the client.

The red pen

Common traps: mistake → fix

  • Mistake: WebSockets are always better than polling

    Fix: For shared, slowly changing data, CDN-cached polling is cheaper and simpler.

  • Mistake: Adding servers behind a load balancer scales Socket.IO

    Fix: Without stickiness or WebSocket-only transport the handshake fails, and without pub/sub events miss users.

  • Mistake: One Kafka consumer group is enough to broadcast to all users

    Fix: A group delivers each event to one member only.

Easy to hard

Questions interviewers ask, easy to hard

  1. When would you use polling, SSE or WebSocket?
  2. How would you serve a live score to 50 lakh viewers?
  3. After scaling to 4 pods, only some users get live updates. List the likely causes.
  4. What are sticky sessions and what are their costs?
  5. Design fan-out of order events to the right connected users across many servers.

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview