Realtime (WebSocket) server
API Gateway WebSocket APIs / ECS behind ALB
Keeps long-lived connections open to push messages instantly (chat, presence, live updates).
In a monolith
WebSocket endpoint in the app
The app holds the sockets. Works until there are two instances: a user connected to server A can’t receive a message sent on server B.
In a distributed system
Connection tier + pub/sub
A fleet of connection servers holds sockets; a pub/sub channel fans each message out to whichever server holds the recipient’s connection.
On AWS
API Gateway WebSocket APIs / ECS behind ALB
Managed WebSocket APIs that call your backend per message, or your own socket servers on ECS behind an ALB.
- API Gateway WebSocket API$$Managed connections; backend pushes via the @connections API
- ECS socket servers behind ALB$$Full control; needs pub/sub for cross-server delivery
- AWS AppSync subscriptions$$Managed realtime for GraphQL apps
- Reach for it when
- Users must see updates within ~100 ms without polling: chat, presence, collaborative editing, live scores.
- What it costs you
- Stateful: each server owns specific connections, so deploys must drain them
- Needs a fan-out channel between servers
- Many idle connections cost memory
- When it dies
- Clients on that server disconnect, reconnect to another one, and must catch up on missed messages from storage.
Interview follow-ups
Two users on different socket servers — how is a message delivered?
The receiving server publishes to a pub/sub channel; every socket server subscribes and pushes to its local connections (or you look up which server holds the user).
WebSockets vs SSE vs long polling?
WebSockets: two-way, best for chat. SSE: server→client stream over plain HTTP, simpler. Long polling: works everywhere, most overhead.
How do clients avoid losing messages during reconnects?
Persist messages with sequence numbers; on reconnect the client asks for everything after the last sequence it saw.
Connection rules
It talks to
Gets traffic from
- DNS
- Load balancer
- API gateway
- App server
- Service
- Worker
- Pub/sub topic
- ClientsOnly when: There is exactly one instance (early stage: DNS points at the server). With two or more instances, put a load balancer or gateway in front so clients don’t need to know which server to hit.
Never
- Realtime server → WorkerDon’t call workers directly — enqueue a job.
- Realtime server → Realtime serverSocket servers coordinate through pub/sub, not direct calls.