Repository navigation
Description
Working with [email protected], @socket.io/[email protected] and node-cluster.
In low load scenarios, sometimes the first socket request fails with 400 'Session ID unknown'. The retry is successful though.
During high load, all the client connections keep failing continuously with the same error. None of the retries succeed.
Browser logs
/socket.io/?EIO=3&transport=polling&t=Nbmcdeq
97:0{"sid":"ssO51L53Emo8zXiyAAAA","upgrades":["websocket"],"pingInterval":20000,"pingTimeout":15000}2:40
/socket.io/?EIO=3&transport=polling&t=Nbmcd-l&sid=ssO51L53Emo8zXiyAAAA
ok
/socket.io/?EIO=3&transport=polling&t=Nbmcd-u&sid=ssO51L53Emo8zXiyAAAA
{"code":1,"message":"Session ID unknown"}
/socket.io/?EIO=3&transport=websocket&sid=ssO51L53Emo8zXiyAAAA
HTTP/1.1 400 Bad Request
Server logs with DEBUG=engine,socket.io*
2021-05-15T15:17:22.687Z engine handshaking client "ssO51L53Emo8zXiyAAAA"
2021-05-15T15:17:22.687Z engine:socket sending packet "open" ({"sid":"ssO51L53Emo8zXiyAAAA","upgrades":["websocket"],"pingInterval":20000,"pingTimeout":15000})
2021-05-15T15:17:22.687Z engine:socket sending packet "message" ([ '0' ])
2021-05-15T15:17:22.688Z engine:polling setting request
2021-05-15T15:17:22.688Z engine:socket flushing buffer to transport
2021-05-15T15:17:22.689Z engine:polling writing "97:0{"sid":"ssO51L53Emo8zXiyAAAA","upgrades":["websocket"],"pingInterval":20000,"pingTimeout":15000}2:40"
2021-05-15T15:17:22.689Z engine:socket executing batch send callback
2021-05-15T15:17:22.689Z socket.io:server incoming connection with id ssO51L53Emo8zXiyAAAA
2021-05-15T15:17:22.689Z socket.io:client connecting to namespace /
2021-05-15T15:17:22.690Z socket.io:namespace adding socket to nsp /
2021-05-15T15:17:22.690Z socket.io:socket socket connected - writing packet
2021-05-15T15:17:22.690Z socket.io:socket joining room ssO51L53Emo8zXiyAAAA
2021-05-15T15:17:22.690Z socket.io:socket packet already sent in initial handshake
2021-05-15T15:17:22.690Z socket.io:socket joined room [ 'ssO51L53Emo8zXiyAAAA' ]
2021-05-15T15:17:24.097Z engine intercepting request for path "/socket.io/"
2021-05-15T15:17:24.097Z engine handling "POST" http request "/socket.io/?EIO=3&transport=polling&t=Nbmcd-l&sid=ssO51L53Emo8zXiyAAAA"
2021-05-15T15:17:24.097Z engine setting new request for existing client
2021-05-15T15:17:24.098Z engine:polling received "7:40/api,"
2021-05-15T15:17:24.098Z engine:socket packet
2021-05-15T15:17:24.098Z engine intercepting request for path "/socket.io/"
2021-05-15T15:17:24.098Z socket.io-parser decoded 0/api, as {"type":0,"nsp":"/api"}
2021-05-15T15:17:24.098Z engine handling "GET" http request "/socket.io/?EIO=3&transport=polling&t=Nbmcd-u&sid=ssO51L53Emo8zXiyAAAA"
2021-05-15T15:17:24.098Z socket.io:client connecting to namespace /api
2021-05-15T15:17:24.098Z socket.io:namespace adding socket to nsp /api
2021-05-15T15:17:24.098Z engine unknown sid "ssO51L53Emo8zXiyAAAA"
2021-05-15T15:17:24.099Z socket.io:socket socket connected - writing packet
2021-05-15T15:17:24.099Z socket.io:socket joining room /api#ssO51L53Emo8zXiyAAAA
2021-05-15T15:17:24.099Z socket.io:client writing packet {"type":0,"nsp":"/api"}
2021-05-15T15:17:24.099Z socket.io-parser encoding packet {"type":0,"nsp":"/api"}
2021-05-15T15:17:24.099Z socket.io-parser encoded {"type":0,"nsp":"/api"} as 0/api,
2021-05-15T15:17:24.099Z engine:socket sending packet "message" (0/api,)
2021-05-15T15:17:24.100Z socket.io:socket joined room [ '/api#ssO51L53Emo8zXiyAAAA' ]
2021-05-15T15:17:24.308Z engine intercepting request for path "/socket.io/"
2021-05-15T15:17:24.308Z engine handling "POST" http request "/socket.io/?EIO=3&transport=polling&t=Nbmce1b&sid=ssO51L53Emo8zXiyAAAA"
2021-05-15T15:17:24.308Z engine setting new request for existing client
2021-05-15T15:17:24.308Z engine:polling received "1:1"
2021-05-15T15:17:24.308Z engine:polling got xhr close packet
2021-05-15T15:17:24.309Z socket.io:client client close with reason transport close
2021-05-15T15:17:24.309Z socket.io:socket closing socket - reason transport close
2021-05-15T15:17:24.309Z socket.io:socket closing socket - reason transport close
2021-05-15T15:17:24.379Z engine unknown sid "ssO51L53Emo8zXiyAAAA"
Activity
darrachequesne commentedon May 15, 2021
Hi! A few questions:
loadBalancingMethodare you using? Is itleast-connection, the default value?One possible explanation would be that the master process is overloaded and does not receive the
sticky:connectionmessage (sent here) in time, but that sounds weird.In the example above, are those the logs of a single child process? Or the logs of all child processes?
sagarjoshi-sct commentedon May 17, 2021
Hi @darrachequesne, thanks for the quick response!
Yes
About 10K connections in 30 mins (~5.55 new connections/sec) with Artillery
Yes with Artillery. Sporadic when testing manually.
I'm not sure. There were 4 node-cluster workers running at the time. How can I distinguish?
Can't share the entire code, but here's the relevant snippet.
index.js
app-server.js
sagarjoshi-sct commentedon May 17, 2021
Also, I noticed that socket.io mentions this package in its v3 documentation, whereas the v2 page only mentions
sticky-session.I'm still on socket.io v2.3.0. Should that matter ?
sagarjoshi-sct commentedon May 19, 2021
Update 1:
The node-cluster is deployed on an AWS EC2 instance, which is behind an AWS ALB. Earlier I thought this was irrelevant as there is just a single EC2 instance. Stickiness was also enabled, just in case.
Strangely, the 'Session ID unknown' errors go away if I use an AWS NLB instead, just for the socket.io connections (API requests continue flowing through the ALB).
However, this adds overhead of an additional component.
Is there any additional configuration required to make this work with ALB ?
darrachequesne commentedon May 19, 2021
No, it should be compatible with Socket.IO v2 too.
I don't think so. That's really weird, if there is a single EC2 instance, any load-balancer should route the traffic to this given VM. Could you please check if there are any difference for the HTTP requests (headers, cookie maybe?)?
sagarjoshi-sct commentedon May 26, 2021
Thanks @darrachequesne ,
I'll inspect the request headers and cookies to see if ALB is modifying them.
For now though, have discarded the ALB altogether, and the library works well 👍