question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Centralised session info to remove dependency of stickiness!

See original GitHub issue

You want to:

  • report a bug/enhancement

Current behaviour

When node clusters are used, it is required to maintain stickiness explicitly even though shared adapter is used socket.io-redis. When client sends multiple requests, if subsequent requests doesn’t go to the same node worker where initial request was made, socket.io throws an error: Session ID unknown. How about caching this session information in a centralised place (May be in a redis adapter itself) and make it work from any worker ?

Currently there are implementations available to make connections go to the same worker based of Client’s IP Address hashing. But these have to be managed explicitly and also might not help load balancing when users share common IP address. (Ex: An internal app being used within a same network).

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
johnmelacommented, Feb 8, 2018

I am also working through this problem and would prefer not to use sticky sessions. I am curious to know what the synchronization issues were pre 1.0.x that led to the deprecation of shared session support?

0reactions
darrachequesnecommented, Jun 28, 2021

For future readers:

We have added some explanation here: https://socket.io/docs/v4/using-multiple-nodes/#Why-is-sticky-session-required

Within a Node.js cluster, you can now use the cluster adapter and the @socket.io/sticky package:

const cluster = require("cluster");
const http = require("http");
const { Server } = require("socket.io");
const numCPUs = require("os").cpus().length;
const { setupMaster, setupWorker } = require("@socket.io/sticky");
const { createAdapter, setupPrimary } = require("@socket.io/cluster-adapter");

if (cluster.isMaster) {
  console.log(`Master ${process.pid} is running`);

  const httpServer = http.createServer();

  // setup sticky sessions
  setupMaster(httpServer, {
    loadBalancingMethod: "least-connection",
  });

  // setup connections between the workers
  setupPrimary();

  // needed for packets containing buffers (you can ignore it if you only send plaintext objects)
  // Node.js < 16.0.0
  cluster.setupMaster({
    serialization: "advanced",
  });
  // Node.js > 16.0.0
  // cluster.setupPrimary({
  //   serialization: "advanced",
  // });

  httpServer.listen(3000);

  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on("exit", (worker) => {
    console.log(`Worker ${worker.process.pid} died`);
    cluster.fork();
  });
} else {
  console.log(`Worker ${process.pid} started`);

  const httpServer = http.createServer();
  const io = new Server(httpServer);

  // use the cluster adapter
  io.adapter(createAdapter());

  // setup connection with the primary process
  setupWorker(io);

  io.on("connection", (socket) => {
    /* ... */
  });
}

Documentation:

Please reopen if needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sticky sessions for your Application Load Balancer
If a cookie expires, the session is no longer sticky and the client should remove the cookie from its cookie store. An HTTP/HTTPS...
Read more >
Configuring sticky session attribute name - JBoss.org
Hi, is it possible to configure the sticky session attribute name? I need to use a name different to JSESSIONID. Ive tried: ProxyPass...
Read more >
Using Replicated Cache vs LB sticky session - Stack Overflow
In such a scenario is it better to use a replicated/distributed cache like EhCache Or to use session stickiness of LB. If the...
Read more >
Implementing Sticky Session Through Load Balancing - 华为云
Session persistence is one of the most common while complex problems in load balancing.Session persistence is also called sticky sessions.
Read more >
What is Session Stickiness | Pros and Cons of Using ... - Imperva
With sticky sessions, a load balancer assigns an identifying attribute to a user, typically by issuing a cookie or by tracking their IP...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found