socket.io-redis-streams-adapter icon indicating copy to clipboard operation
socket.io-redis-streams-adapter copied to clipboard

fetchSockets() is extremely slow when more than 1 server is connected

Open aureus1 opened this issue 8 months ago • 1 comments

Reproduces with this code:

import { Redis } from "ioredis";
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-streams-adapter";

const redisClient = new Redis();

const io = new Server({
  adapter: createAdapter(redisClient)
});

io.listen(parseInt(process.argv[2]));

const loopFn = async () => {
  console.time('fetchSockets');
  await io.fetchSockets();
  console.timeEnd('fetchSockets');    
}
setInterval(loopFn, 1000);

Execute a single instance (i.e. node server.js 3000), and timing on fetchSockets() is generally 0.05ms or quicker. Bringing up a second instance (i.e. node server.js 3001 in a new console) causes this to immediately spike to ~200ms.

Note that I do not see the same behavior when using @socket.io/redis-adapter

aureus1 avatar Mar 25 '25 19:03 aureus1

Hi!

Yes, that is expected, as the XREAD command blocks until either enough packets have been received or a timeout.

Source: https://github.com/socketio/socket.io-redis-streams-adapter/blob/fc03543383aadc584e32edea2505d6c88774303d/lib/util.ts#L88-L89

The classic Redis adapter does not have this behavior, as it relies on the pub/sub mechanism, which is faster but less reliable.

darrachequesne avatar Mar 28 '25 07:03 darrachequesne