Skip to main content

2 posts tagged with "distributed-systems"

View All Tags

Building a Production-Grade WebSocket Server in Go

· 11 min read
Author of wshub

Every Go WebSocket server starts the same way. You pull in gorilla/websocket or nhooyr.io/websocket, write an Upgrade call, spin up a goroutine that reads frames in a loop, and it works. You broadcast a message by iterating a map[*Client]bool and calling conn.WriteMessage on each one. Twenty lines, a working demo, and a reasonable belief that the hard part is behind you.

It isn't. The hard part starts at the traffic your demo never saw: a client that stops reading, a deploy that needs to happen without dropping connections, a second replica that has no idea the first one exists. None of that is in the WebSocket RFC, and none of it is optional once real users are on the other end of the socket.

Scaling WebSockets Across Multiple Go Servers with Redis

· 7 min read
Author of wshub

A stateless HTTP handler scales the boring way: put a load balancer in front, add replicas, done — any instance can answer any request because no instance holds anything the others need. A WebSocket server breaks that model by definition. The connection is the state. If Alice is connected to node B and your billing webhook fires on node A and calls hub.SendToUser("alice", ...), node A's hub has no idea Alice exists. The message doesn't error, doesn't queue, doesn't retry — it just isn't delivered, because from node A's point of view there's no client called Alice.