Scaling
How Flock scales horizontally across multiple server instances using Redis pub/sub.
A single Flock server instance handles thousands of concurrent WebSocket connections without issue. Horizontal scaling (multiple instances behind a load balancer) becomes relevant when you need:
- More than one process for redundancy or failover
- Multi-region deployments to reduce latency for geographically distributed users
- More total connections than a single server can handle
How it works
When multiple Flock instances share the same Redis, they coordinate via a pub/sub channel per room.
When Client A sends a cursor move to Instance 1:
- Instance 1 delivers it to all local clients in that room (fast, in-process).
- Instance 1 publishes it to
flock:broadcast:{roomId}in Redis. - Instance 2 (and any other instances) receive it from Redis and deliver it to their local clients.
Each instance tags its own publishes with a unique instanceId and ignores messages it published itself, preventing double-delivery.
Enabling Redis mode
Set FLOCK_REDIS_URL on all instances. That's it.
WebSocket stickiness
WebSocket connections are stateful — once a client connects to an instance, it stays there for the duration of the connection. Your load balancer must use sticky sessions (also called session affinity) to route a client back to the same instance if it disconnects and reconnects during a rolling restart.
Most load balancers support this:
- Nginx:
ip_hashorsticky cookie - AWS ALB: sticky target groups
- Fly.io: sticky sessions are the default for WebSocket apps
Without stickiness, a reconnecting client may land on a different instance, which is fine (Flock handles it), but causes a brief visible re-join for other users.
Redis requirements
Any Redis 6+ instance works. Recommended managed options:
- Upstash — serverless Redis, has a generous free tier, one-click keyspace notifications
- Redis Cloud — managed Redis by Redis Inc., good free tier
- Railway Redis — simple setup, pairs well with Railway app deploys
Whichever you use, enable keyspace notifications with the Ex flag (expired events). Without this, idle user eviction won't work.
Multi-region on Fly.io
Fly.io makes multi-region deployment straightforward. Create a fly.toml that deploys to multiple regions and point all instances at the same Redis.
Example fly.toml for a two-region setup (US East + Germany):
Deploy to multiple regions:
Set the Redis URL as a secret:
Fly routes WebSocket connections to the nearest region. All instances share the same Redis so cursors and presence propagate across regions, with the Redis pub/sub round-trip as the only added latency.
Load testing
A single shared-cpu-1x Fly.io machine (1 vCPU, 256MB RAM) handles around 1,000 to 2,000 concurrent WebSocket connections with moderate cursor traffic. For higher volumes, increase vm.memory or run multiple machines per region.
The bottleneck in high-traffic scenarios is typically Redis pub/sub throughput. Upstash's free tier has a 1,000 commands/sec limit; paid tiers remove the cap.
See also
- Self-hosting guide — Docker Compose setup with Redis
- FlockServer options — programmatic configuration