gRPC, real-time APIs and webhooks · mid
Long-lived connections: heartbeats and resuming
Every proxy and load balancer on the path has an idle timeout, so connections need heartbeats. After a drop, clients must be able to resume from the last event they saw.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- A WebSocket that closes every 60 seconds usually meets an idle timeout. Nginx proxy_read_timeout defaults to 60 seconds, and the default idle timeout of an AWS Application Load Balancer is also 60 seconds. Locally there is no proxy, so it works.
- Each hop has its own timeout (browser, CDN, cloud load balancer, Nginx, app). The shortest one wins.
- Fix: heartbeats. The server sends a WebSocket ping or a small app message every 25 to 30 seconds and closes connections that miss their pong. This also cleans up dead clients. Raising proxy_read_timeout and the load balancer idle timeout is a second guard.
- SSE resume: put id: <eventId> on each event. The browser sends Last-Event-ID when it reconnects. The server keeps recent events (a Redis stream or the database) and replays those after that id. Also send the current state when a client connects.
- SSE through Nginx: proxy buffering holds small writes. Send X-Accel-Buffering: no or turn buffering off for that location, skip compression for text/event-stream, set Cache-Control: no-cache and flush headers.
- Leak: on req.on('close') remove the subscription. Otherwise every closed tab leaves a listener writing to a dead response.
- Delivery is at most once without replay. If a missed update matters, design for resume or for a refetch of the state after reconnect.
The red pen
Common traps: mistake → fix
Mistake: It works on my laptop, so the code is fine
Fix: Locally there is no proxy. Production adds idle timeouts and buffering.
Mistake: EventSource reconnects, so no events are lost
Fix: Events sent during the gap are lost unless you send ids and replay.
Mistake: Raising the timeout fixes dropped connections
Fix: Heartbeats are the real fix, because dead clients also need detection.
Easy to hard
Questions interviewers ask, easy to hard
- Why does a WebSocket drop after exactly 60 seconds in production only?
- How do heartbeats work and who sends them?
- How does an SSE client resume after a disconnect?
- Why does SSE through Nginx deliver events in bursts?
- Design a live order-tracking stream that never shows a customer stale status after a reconnect.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.