Real-time features like live chats or live stock updates are no longer "optional"—they are expected. Socket.io makes this easy, but scaling it requires Redis.
The Scaling Problem
When you have multiple server instances, a user connected to Server A cannot "talk" to a user on Server B.
The Redis Adapter
By using the Redis Adapter, your Socket.io servers publish messages to Redis, which then broadcasts them to all other server instances.
const { createAdapter } = require("@socket.io/redis-adapter");
const { createClient } = require("redis");
const pubClient = createClient({ url: "redis://localhost:6379" });
const subClient = pubClient.duplicate();
io.adapter(createAdapter(pubClient, subClient));Performance Tips:
- Namespace and Rooms: Only send data to the users who need it.
- Binary Data: Use MessagePack or Protobuf to reduce payload size.
- Rate Limiting: Don't let a single client overwhelm your server with messages.
Real-time at scale is about distributed state, and Redis is the perfect tool for the job.