TCP, UDP, and the need for push
Most designs need a request and a reply. Some need the server to speak first.
What you will be able to do
- Explain what TCP guarantees that UDP does not
- Pick a push mechanism for chat, live scores, and notifications
- Estimate the connection cost of keeping a million sockets open
- Describe how presence and reconnect logic change a design
What each transport promises
| TCP | UDP | |
|---|---|---|
| Delivery | Retries until acknowledged | Best effort, may vanish |
| Ordering | Guaranteed in order | None |
| Head-of-line blocking | Yes — one lost packet stalls the stream | No — later packets keep flowing |
| Connection state | Handshake, tracked per connection | Connectionless |
| Good for | APIs, databases, file transfer, chat messages | Voice, video, games, telemetry |
The interesting column is head-of-line blocking. TCP will not hand you packet 12 until packet 11 arrives, because your application asked for a stream. For a chat message that is exactly right. For a video call, waiting for a retransmitted frame produces a freeze, while skipping it produces a small glitch — so real-time media runs on UDP and tolerates loss.
Push: four ways to get data to a client
- Polling — the client asks every N seconds. Trivial to build, wasteful, and adds latency up to N seconds. Fine for a dashboard refreshing every minute.
- Long polling — the server holds the request open until there is something to say. Works through most proxies, but each message costs a new request.
- Server-sent events — one HTTP connection, server writes text events forever, client reads. Perfect for feeds, scores, and notifications where the client only listens.
- WebSocket — a single connection upgraded from HTTP where both sides send frames at any time. The default for chat, collaboration, and multiplayer.
Choose by direction and volume. If the client mostly listens, SSE is simpler than a socket and reconnects by itself. If both sides talk constantly, use a socket. If updates are rare, polling is honest and cheap.
What a million connections costs
The design consequence: you do not run one server for a million sockets. You run many WebSocket servers, each holding a bounded number, and you make routing explicit — a registry maps user to server, usually in Redis or a coordination service, so any node can deliver a message to the right connection. That routing table is the part interviewers want to hear about.
Worth remembering
- TCP gives ordered, reliable, congestion-controlled byte streams; UDP gives small messages with no promises.
- Use UDP when a late packet is worse than a lost one — voice, video, real-time games.
- Push usually means a long-lived connection: WebSocket for two-way, SSE for server-to-client only, polling as the fallback.
- A million idle connections is a file-descriptor and memory problem, not a bandwidth problem.