WebSocket Support Has Evolved
WebSocket support in FastAPI has been solid for years, but production deployment patterns have evolved significantly in 2026. The naive approach — one WebSocket connection per server process — works for demos but falls apart the moment you need to scale beyond a single instance.
The fundamental challenge with WebSockets is state. An HTTP request arrives, gets processed, and the connection closes. A WebSocket stays open. If you have four server instances behind a load balancer and a message needs to go to a user connected to instance three, instance one cannot deliver it directly.
Redis Pub/Sub as the Scaling Solution
Redis Pub/Sub solves this elegantly. Each server instance subscribes to channels for its connected users. When any instance needs to send a message, it publishes to the user’s channel. The instance with the actual connection picks it up and delivers it over the WebSocket.
The connection manager pattern keeps the code clean. A class tracks active connections per user and handles broadcasting. Each user gets a unique channel name in Redis, and the connection manager handles the mapping between user IDs and active connections.
Client-Side Patterns
On the client side, the WebSocket API has been stable in browsers for years. The key pattern is a reconnection wrapper. WebSocket connections drop for all kinds of reasons — network changes, server restarts, load balancer timeouts. A wrapper that automatically reconnects with exponential backoff is essential for production use.
Authentication with WebSockets is trickier than HTTP. You cannot easily pass headers on the initial connection. The standard pattern is to authenticate via HTTP first, get a short-lived token, and pass it as a query parameter on the WebSocket upgrade request. FastAPI’s dependency injection works with WebSocket endpoints, so you can use the same auth logic as your REST endpoints.
Performance Characteristics
Performance-wise, FastAPI WebSockets handle thousands of concurrent connections per process on modest hardware. The bottleneck is usually the Redis round-trip for cross-instance messaging, not the WebSocket handling itself. For most applications, horizontal scaling with Redis Pub/Sub is sufficient. For lower latency requirements, consider switching to Redis Streams or a dedicated message queue.
When to Use WebSockets vs Alternatives
Server-Sent Events are simpler for one-way server-to-client streaming. HTTP/2 and HTTP/3 server push can handle some use cases without the complexity of persistent connections. But for true bidirectional real-time communication — chat, collaborative editing, live dashboards — WebSockets remain the right tool, and FastAPI makes them production-ready.
Discussion
Leave a comment
No comments yet
Be the first to start the conversation.