socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

balancer without sticky sessions and more than 1 process

Open kof opened this issue 14 years ago • 19 comments

It turns out socket.io has never worked behind balancer without sticky sessions support if you scale to multiple processes. This is the case on heroku.

node 0.6.18 socket.io 0.9.6 (tested both the version from master and npm)

I have modified the chat example to demostrate the issue, it is 100% reproducible.

https://github.com/kof/socket.io/tree/master/examples/chat

node cluster

use the server on port 3000.

cluster will use proxy and spawn 3 nodejs instances which will listen on different portrs. It will forward every request to a different server.

You will see that .emit callback mostly does not work, same is without a callback, just listening to events on the client. Messages from server does not reach the client.

Thanks.

kof avatar Jul 03 '12 13:07 kof

I have tried also 0.8.x versions, it has never worked this way. Are you using balancer which redirects always to the same server? This is main problem currently.

kof avatar Jul 03 '12 16:07 kof

Correct me if I am wrong. If using xhr-polling, when I emit a message, xhr request is closed directly after sending it. If I emit multiple messages with distance of some miliseconds, the first message will be send, connection closed, other messages buffered locally in the memory of the current server. After that the client reconnects to another server, which hasn't buffered messages. At this way if messages sent in a sequence with some delay, only the first one is delivered.

kof avatar Jul 03 '12 19:07 kof

I have tried to fix it, the reason messages were not delivered is that buffered messages were dropped in onClose. It was assummed, that if a client disconnects another node, messages should be sent, which is not always the case. Now I drop buffered packets only on "dispatched" event, which is published when a message is really delivered.

kof avatar Jul 06 '12 21:07 kof

After this fix worked for me I started to test my mongodb storage https://github.com/kof/socket.io-mongo

There is another bug, which is never happens to redis, but happens to my mongo store always. If I abort current connection (ESC in firefox), client start to do handshakes and connects in a loop.

I spent some days debugging this and found the reason. For whatever reason pubsub implementation via mongo often needs more time than expected to publish "handshake" event. So the client comes back after handshake faster than handshake event was propagated to all servers and it lands on the server, which hasn't handshake data and triggers an error.

To fix this, we need to wait util all servers got handshake before to end the xhr request. And for this we need implement heartbeat between servers and always track which servers are connected, to know how much servers have to publish 'handshake received' event and after that end the handshake.

kof avatar Jul 06 '12 21:07 kof

This will add even more complexity and overhead, so probably the best way is just to save handshakes on the storage and get them every time from there.

kof avatar Jul 06 '12 21:07 kof

A timeout of 3000 ms clientside between recieving handshake data and callback call which will lead to "connect", fixes the issue. But it is really dirty, because you never know how much it takes to propagate handshake to all servers.

https://github.com/LearnBoost/socket.io-client/blob/master/lib/socket.js#L125

In case handshake was not received by the server connect happens to, error will happen again, handshake and than the next try, which can be successfull. Using this timeout will mostly avoid endless reshake.

kof avatar Jul 07 '12 13:07 kof

Any news on this issue, anybody?

hannesgassert avatar Aug 16 '12 07:08 hannesgassert

socket.io is currently undergoing a major rewrite (the core is being factored out into engine.io), which will change things a lot, and make things easier to change. In any event, I guess that rewrite is the reason why there isn't so much news on issues relating to the current code base.

EugenDueck avatar Aug 16 '12 09:08 EugenDueck

Sooo, has there been any movement on this issue? The last post was 6 months ago.

electrichead avatar Feb 25 '13 18:02 electrichead

Any movement? does this work in the release of socket.io?

BrandonCopley avatar Apr 27 '13 20:04 BrandonCopley

If this is not going to be fixed prior to v1.0, are there any ETA for socket.io v1.0 ?

glesperance avatar May 02 '13 18:05 glesperance

I am working on socket.io replacement ... which will be released probably next week.

kof avatar May 02 '13 18:05 kof

The whole replacement :P. sockjs still isn't 100%. For sticky sessions get behind a load balancer and set the key for the session to "jsessionid".

Nodejitsu has sticky session support

On May 2, 2013, at 1:47 PM, Oleg Slobodskoi [email protected] wrote:

I am working on socket.io replacement ... which will be released probably next week.

— Reply to this email directly or view it on GitHubhttps://github.com/LearnBoost/socket.io/issues/939#issuecomment-17356703 .

BrandonCopley avatar May 02 '13 18:05 BrandonCopley

How does this work on clustered node instances? It seems that this is impossible to do with the cluster API

"When multiple processes are all accept()ing on the same underlying resource, the operating system load-balances across them very efficiently. There is no routing logic in Node.js, or in your program, and no shared state between the workers. Therefore, it is important to design your program such that it does not rely too heavily on in-memory data objects for things like sessions and login." [1]

[1] http://nodejs.org/api/cluster.html

glesperance avatar May 02 '13 18:05 glesperance

Unfortunately I've found it's not smart to cluster. I use redisstore for sessions and socket.io, but no clusters. I am running socket.io across multiple servers though for scale.

On Thu, May 2, 2013 at 1:55 PM, Gabriel Lesperance <[email protected]

wrote:

How does this work on clustered node instances? It seems that this is impossible to do with the cluster API

"When multiple processes are all accept()ing on the same underlying resource, the operating system load-balances across them very efficiently. There is no routing logic in Node.js, or in your program, and no shared state between the workers. Therefore, it is important to design your program such that it does not rely too heavily on in-memory data objects for things like sessions and login." [1]

[1] http://nodejs.org/api/cluster.html

— Reply to this email directly or view it on GitHubhttps://github.com/LearnBoost/socket.io/issues/939#issuecomment-17357251 .

http://novationmobile.com/

Brandon Copley

CHIEF OPERATING OFFICER

PHONE::: 512.784.6060 EMAIL::: [email protected] [email protected] WEBSITE::: http://novationmobile.com/ http://www.novationmobile.com

BrandonCopley avatar May 02 '13 18:05 BrandonCopley

Some information about it, I am trying with sockets-cluster...

Juankia avatar Aug 05 '13 17:08 Juankia

I'm using node's builtin cluster module and listen on a different port in each worker, i.e. I'm not doing the port sharing, as it wouldn't work for sticky sessions. A load balancer chooses the target port by hashing the incoming IP address.

Additionally, I scale this out to multiple boxes, and only have to add to the target host:port list of the load balancer.

Of course you don't have to load balance using the IP address, a 'jsessionid' approach as suggested above will work as well. The crucial point here is that I use a different port for each worker process. It's running in production and works pretty well.

I also do master <-> worker IPC using cluster's facilities like worker.send.

EugenDueck avatar Aug 06 '13 01:08 EugenDueck

sounds familiar: https://github.com/LearnBoost/socket.io/issues/1284

justinTNT avatar Aug 09 '13 06:08 justinTNT

can you please provide sample code snippet for this. We are not able to get ipadress behind haproxy

vijayaa avatar Jul 20 '16 07:07 vijayaa